@@ -86,83 +86,98 @@ static int local_tzoffset(unsigned long time)
86
86
return offset * eastwest ;
87
87
}
88
88
89
- const char * show_date_relative (unsigned long time , int tz ,
89
+ void show_date_relative (unsigned long time , int tz ,
90
90
const struct timeval * now ,
91
- char * timebuf ,
92
- size_t timebuf_size )
91
+ struct strbuf * timebuf )
93
92
{
94
93
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
+ }
97
98
diff = now -> tv_sec - time ;
98
99
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 ;
101
103
}
102
104
/* Turn it into minutes */
103
105
diff = (diff + 30 ) / 60 ;
104
106
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 ;
107
110
}
108
111
/* Turn it into hours */
109
112
diff = (diff + 30 ) / 60 ;
110
113
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 ;
113
117
}
114
118
/* We deal with number of days from here on */
115
119
diff = (diff + 12 ) / 24 ;
116
120
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 ;
119
124
}
120
125
/* Say weeks for the past 10 weeks or so */
121
126
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 ;
124
131
}
125
132
/* Say months for the past 12 months or so */
126
133
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 ;
129
138
}
130
139
/* Give years and months for 5 years or so */
131
140
if (diff < 1825 ) {
132
141
unsigned long totalmonths = (diff * 12 * 2 + 365 ) / (365 * 2 );
133
142
unsigned long years = totalmonths / 12 ;
134
143
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 ;
145
156
}
146
157
/* 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 );
149
161
}
150
162
151
163
const char * show_date (unsigned long time , int tz , enum date_mode mode )
152
164
{
153
165
struct tm * tm ;
154
- static char timebuf [ 200 ] ;
166
+ static struct strbuf timebuf = STRBUF_INIT ;
155
167
156
168
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 ;
159
172
}
160
173
161
174
if (mode == DATE_RELATIVE ) {
162
175
struct timeval now ;
176
+
177
+ strbuf_reset (& timebuf );
163
178
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 ;
166
181
}
167
182
168
183
if (mode == DATE_LOCAL )
@@ -171,31 +186,33 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
171
186
tm = time_to_tm (time , tz );
172
187
if (!tm )
173
188
return NULL ;
189
+
190
+ strbuf_reset (& timebuf );
174
191
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 ,
176
193
tm -> tm_mon + 1 , tm -> tm_mday );
177
194
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" ,
179
196
tm -> tm_year + 1900 ,
180
197
tm -> tm_mon + 1 ,
181
198
tm -> tm_mday ,
182
199
tm -> tm_hour , tm -> tm_min , tm -> tm_sec ,
183
200
tz );
184
201
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" ,
186
203
weekday_names [tm -> tm_wday ], tm -> tm_mday ,
187
204
month_names [tm -> tm_mon ], tm -> tm_year + 1900 ,
188
205
tm -> tm_hour , tm -> tm_min , tm -> tm_sec , tz );
189
206
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" ,
191
208
weekday_names [tm -> tm_wday ],
192
209
month_names [tm -> tm_mon ],
193
210
tm -> tm_mday ,
194
211
tm -> tm_hour , tm -> tm_min , tm -> tm_sec ,
195
212
tm -> tm_year + 1900 ,
196
213
(mode == DATE_LOCAL ) ? 0 : ' ' ,
197
214
tz );
198
- return timebuf ;
215
+ return timebuf . buf ;
199
216
}
200
217
201
218
/*
0 commit comments