@@ -160,18 +160,27 @@ void show_date_relative(unsigned long time, int tz,
160160 (diff + 183 ) / 365 );
161161}
162162
163- const char * show_date (unsigned long time , int tz , enum date_mode mode )
163+ struct date_mode * date_mode_from_type (enum date_mode_type type )
164+ {
165+ static struct date_mode mode ;
166+ if (type == DATE_STRFTIME )
167+ die ("BUG: cannot create anonymous strftime date_mode struct" );
168+ mode .type = type ;
169+ return & mode ;
170+ }
171+
172+ const char * show_date (unsigned long time , int tz , const struct date_mode * mode )
164173{
165174 struct tm * tm ;
166175 static struct strbuf timebuf = STRBUF_INIT ;
167176
168- if (mode == DATE_RAW ) {
177+ if (mode -> type == DATE_RAW ) {
169178 strbuf_reset (& timebuf );
170179 strbuf_addf (& timebuf , "%lu %+05d" , time , tz );
171180 return timebuf .buf ;
172181 }
173182
174- if (mode == DATE_RELATIVE ) {
183+ if (mode -> type == DATE_RELATIVE ) {
175184 struct timeval now ;
176185
177186 strbuf_reset (& timebuf );
@@ -180,7 +189,7 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
180189 return timebuf .buf ;
181190 }
182191
183- if (mode == DATE_LOCAL )
192+ if (mode -> type == DATE_LOCAL )
184193 tz = local_tzoffset (time );
185194
186195 tm = time_to_tm (time , tz );
@@ -190,17 +199,17 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
190199 }
191200
192201 strbuf_reset (& timebuf );
193- if (mode == DATE_SHORT )
202+ if (mode -> type == DATE_SHORT )
194203 strbuf_addf (& timebuf , "%04d-%02d-%02d" , tm -> tm_year + 1900 ,
195204 tm -> tm_mon + 1 , tm -> tm_mday );
196- else if (mode == DATE_ISO8601 )
205+ else if (mode -> type == DATE_ISO8601 )
197206 strbuf_addf (& timebuf , "%04d-%02d-%02d %02d:%02d:%02d %+05d" ,
198207 tm -> tm_year + 1900 ,
199208 tm -> tm_mon + 1 ,
200209 tm -> tm_mday ,
201210 tm -> tm_hour , tm -> tm_min , tm -> tm_sec ,
202211 tz );
203- else if (mode == DATE_ISO8601_STRICT ) {
212+ else if (mode -> type == DATE_ISO8601_STRICT ) {
204213 char sign = (tz >= 0 ) ? '+' : '-' ;
205214 tz = abs (tz );
206215 strbuf_addf (& timebuf , "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d" ,
@@ -209,19 +218,21 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
209218 tm -> tm_mday ,
210219 tm -> tm_hour , tm -> tm_min , tm -> tm_sec ,
211220 sign , tz / 100 , tz % 100 );
212- } else if (mode == DATE_RFC2822 )
221+ } else if (mode -> type == DATE_RFC2822 )
213222 strbuf_addf (& timebuf , "%.3s, %d %.3s %d %02d:%02d:%02d %+05d" ,
214223 weekday_names [tm -> tm_wday ], tm -> tm_mday ,
215224 month_names [tm -> tm_mon ], tm -> tm_year + 1900 ,
216225 tm -> tm_hour , tm -> tm_min , tm -> tm_sec , tz );
226+ else if (mode -> type == DATE_STRFTIME )
227+ strbuf_addftime (& timebuf , mode -> strftime_fmt , tm );
217228 else
218229 strbuf_addf (& timebuf , "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d" ,
219230 weekday_names [tm -> tm_wday ],
220231 month_names [tm -> tm_mon ],
221232 tm -> tm_mday ,
222233 tm -> tm_hour , tm -> tm_min , tm -> tm_sec ,
223234 tm -> tm_year + 1900 ,
224- (mode == DATE_LOCAL ) ? 0 : ' ' ,
235+ (mode -> type == DATE_LOCAL ) ? 0 : ' ' ,
225236 tz );
226237 return timebuf .buf ;
227238}
@@ -759,28 +770,31 @@ int parse_date(const char *date, struct strbuf *result)
759770 return 0 ;
760771}
761772
762- enum date_mode parse_date_format (const char * format )
773+ void parse_date_format (const char * format , struct date_mode * mode )
763774{
764775 if (!strcmp (format , "relative" ))
765- return DATE_RELATIVE ;
776+ mode -> type = DATE_RELATIVE ;
766777 else if (!strcmp (format , "iso8601" ) ||
767778 !strcmp (format , "iso" ))
768- return DATE_ISO8601 ;
779+ mode -> type = DATE_ISO8601 ;
769780 else if (!strcmp (format , "iso8601-strict" ) ||
770781 !strcmp (format , "iso-strict" ))
771- return DATE_ISO8601_STRICT ;
782+ mode -> type = DATE_ISO8601_STRICT ;
772783 else if (!strcmp (format , "rfc2822" ) ||
773784 !strcmp (format , "rfc" ))
774- return DATE_RFC2822 ;
785+ mode -> type = DATE_RFC2822 ;
775786 else if (!strcmp (format , "short" ))
776- return DATE_SHORT ;
787+ mode -> type = DATE_SHORT ;
777788 else if (!strcmp (format , "local" ))
778- return DATE_LOCAL ;
789+ mode -> type = DATE_LOCAL ;
779790 else if (!strcmp (format , "default" ))
780- return DATE_NORMAL ;
791+ mode -> type = DATE_NORMAL ;
781792 else if (!strcmp (format , "raw" ))
782- return DATE_RAW ;
783- else
793+ mode -> type = DATE_RAW ;
794+ else if (skip_prefix (format , "format:" , & format )) {
795+ mode -> type = DATE_STRFTIME ;
796+ mode -> strftime_fmt = xstrdup (format );
797+ } else
784798 die ("unknown date format %s" , format );
785799}
786800
0 commit comments