@@ -86,83 +86,98 @@ static int local_tzoffset(unsigned long time)
8686 return offset * eastwest ;
8787}
8888
89- const char * show_date_relative (unsigned long time , int tz ,
89+ void show_date_relative (unsigned long time , int tz ,
9090 const struct timeval * now ,
91- char * timebuf ,
92- size_t timebuf_size )
91+ struct strbuf * timebuf )
9392{
9493 unsigned long diff ;
95- if (now -> tv_sec < time )
96- return "in the future" ;
94+ if (now -> tv_sec < time ) {
95+ strbuf_addstr (timebuf , _ ("in the future" ));
96+ return ;
97+ }
9798 diff = now -> tv_sec - time ;
9899 if (diff < 90 ) {
99- snprintf (timebuf , timebuf_size , "%lu seconds ago" , diff );
100- return timebuf ;
100+ strbuf_addf (timebuf ,
101+ Q_ ("%lu second ago" , "%lu seconds ago" , diff ), diff );
102+ return ;
101103 }
102104 /* Turn it into minutes */
103105 diff = (diff + 30 ) / 60 ;
104106 if (diff < 90 ) {
105- snprintf (timebuf , timebuf_size , "%lu minutes ago" , diff );
106- return timebuf ;
107+ strbuf_addf (timebuf ,
108+ Q_ ("%lu minute ago" , "%lu minutes ago" , diff ), diff );
109+ return ;
107110 }
108111 /* Turn it into hours */
109112 diff = (diff + 30 ) / 60 ;
110113 if (diff < 36 ) {
111- snprintf (timebuf , timebuf_size , "%lu hours ago" , diff );
112- return timebuf ;
114+ strbuf_addf (timebuf ,
115+ Q_ ("%lu hour ago" , "%lu hours ago" , diff ), diff );
116+ return ;
113117 }
114118 /* We deal with number of days from here on */
115119 diff = (diff + 12 ) / 24 ;
116120 if (diff < 14 ) {
117- snprintf (timebuf , timebuf_size , "%lu days ago" , diff );
118- return timebuf ;
121+ strbuf_addf (timebuf ,
122+ Q_ ("%lu day ago" , "%lu days ago" , diff ), diff );
123+ return ;
119124 }
120125 /* Say weeks for the past 10 weeks or so */
121126 if (diff < 70 ) {
122- snprintf (timebuf , timebuf_size , "%lu weeks ago" , (diff + 3 ) / 7 );
123- return timebuf ;
127+ strbuf_addf (timebuf ,
128+ Q_ ("%lu week ago" , "%lu weeks ago" , (diff + 3 ) / 7 ),
129+ (diff + 3 ) / 7 );
130+ return ;
124131 }
125132 /* Say months for the past 12 months or so */
126133 if (diff < 365 ) {
127- snprintf (timebuf , timebuf_size , "%lu months ago" , (diff + 15 ) / 30 );
128- return timebuf ;
134+ strbuf_addf (timebuf ,
135+ Q_ ("%lu month ago" , "%lu months ago" , (diff + 15 ) / 30 ),
136+ (diff + 15 ) / 30 );
137+ return ;
129138 }
130139 /* Give years and months for 5 years or so */
131140 if (diff < 1825 ) {
132141 unsigned long totalmonths = (diff * 12 * 2 + 365 ) / (365 * 2 );
133142 unsigned long years = totalmonths / 12 ;
134143 unsigned long months = totalmonths % 12 ;
135- int n ;
136- n = snprintf (timebuf , timebuf_size , "%lu year%s" ,
137- years , (years > 1 ? "s" : "" ));
138- if (months )
139- snprintf (timebuf + n , timebuf_size - n ,
140- ", %lu month%s ago" ,
141- months , (months > 1 ? "s" : "" ));
142- else
143- snprintf (timebuf + n , timebuf_size - n , " ago" );
144- return timebuf ;
144+ if (months ) {
145+ struct strbuf sb = STRBUF_INIT ;
146+ strbuf_addf (& sb , Q_ ("%lu year" , "%lu years" , years ), years );
147+ /* TRANSLATORS: "%s" is "<n> years" */
148+ strbuf_addf (timebuf ,
149+ Q_ ("%s, %lu month ago" , "%s, %lu months ago" , months ),
150+ sb .buf , months );
151+ strbuf_release (& sb );
152+ } else
153+ strbuf_addf (timebuf ,
154+ Q_ ("%lu year ago" , "%lu years ago" , years ), years );
155+ return ;
145156 }
146157 /* Otherwise, just years. Centuries is probably overkill. */
147- snprintf (timebuf , timebuf_size , "%lu years ago" , (diff + 183 ) / 365 );
148- return timebuf ;
158+ strbuf_addf (timebuf ,
159+ Q_ ("%lu year ago" , "%lu years ago" , (diff + 183 ) / 365 ),
160+ (diff + 183 ) / 365 );
149161}
150162
151163const char * show_date (unsigned long time , int tz , enum date_mode mode )
152164{
153165 struct tm * tm ;
154- static char timebuf [ 200 ] ;
166+ static struct strbuf timebuf = STRBUF_INIT ;
155167
156168 if (mode == DATE_RAW ) {
157- snprintf (timebuf , sizeof (timebuf ), "%lu %+05d" , time , tz );
158- return timebuf ;
169+ strbuf_reset (& timebuf );
170+ strbuf_addf (& timebuf , "%lu %+05d" , time , tz );
171+ return timebuf .buf ;
159172 }
160173
161174 if (mode == DATE_RELATIVE ) {
162175 struct timeval now ;
176+
177+ strbuf_reset (& timebuf );
163178 gettimeofday (& now , NULL );
164- return show_date_relative (time , tz , & now ,
165- timebuf , sizeof ( timebuf )) ;
179+ show_date_relative (time , tz , & now , & timebuf );
180+ return timebuf . buf ;
166181 }
167182
168183 if (mode == DATE_LOCAL )
@@ -171,31 +186,33 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
171186 tm = time_to_tm (time , tz );
172187 if (!tm )
173188 return NULL ;
189+
190+ strbuf_reset (& timebuf );
174191 if (mode == DATE_SHORT )
175- sprintf ( timebuf , "%04d-%02d-%02d" , tm -> tm_year + 1900 ,
192+ strbuf_addf ( & timebuf , "%04d-%02d-%02d" , tm -> tm_year + 1900 ,
176193 tm -> tm_mon + 1 , tm -> tm_mday );
177194 else if (mode == DATE_ISO8601 )
178- sprintf ( timebuf , "%04d-%02d-%02d %02d:%02d:%02d %+05d" ,
195+ strbuf_addf ( & timebuf , "%04d-%02d-%02d %02d:%02d:%02d %+05d" ,
179196 tm -> tm_year + 1900 ,
180197 tm -> tm_mon + 1 ,
181198 tm -> tm_mday ,
182199 tm -> tm_hour , tm -> tm_min , tm -> tm_sec ,
183200 tz );
184201 else if (mode == DATE_RFC2822 )
185- sprintf ( timebuf , "%.3s, %d %.3s %d %02d:%02d:%02d %+05d" ,
202+ strbuf_addf ( & timebuf , "%.3s, %d %.3s %d %02d:%02d:%02d %+05d" ,
186203 weekday_names [tm -> tm_wday ], tm -> tm_mday ,
187204 month_names [tm -> tm_mon ], tm -> tm_year + 1900 ,
188205 tm -> tm_hour , tm -> tm_min , tm -> tm_sec , tz );
189206 else
190- sprintf ( timebuf , "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d" ,
207+ strbuf_addf ( & timebuf , "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d" ,
191208 weekday_names [tm -> tm_wday ],
192209 month_names [tm -> tm_mon ],
193210 tm -> tm_mday ,
194211 tm -> tm_hour , tm -> tm_min , tm -> tm_sec ,
195212 tm -> tm_year + 1900 ,
196213 (mode == DATE_LOCAL ) ? 0 : ' ' ,
197214 tz );
198- return timebuf ;
215+ return timebuf . buf ;
199216}
200217
201218/*
0 commit comments