| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
/* |
|
2
|
|
|
|
|
|
|
* GIT - The information manager from hell |
|
3
|
|
|
|
|
|
|
* |
|
4
|
|
|
|
|
|
|
* Copyright (C) Linus Torvalds, 2005 |
|
5
|
|
|
|
|
|
|
*/ |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
#include "common.h" |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
#ifndef GIT_WIN32 |
|
10
|
|
|
|
|
|
|
#include |
|
11
|
|
|
|
|
|
|
#endif |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#include "util.h" |
|
14
|
|
|
|
|
|
|
#include "cache.h" |
|
15
|
|
|
|
|
|
|
#include "posix.h" |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#include |
|
18
|
|
|
|
|
|
|
#include |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
typedef enum { |
|
21
|
|
|
|
|
|
|
DATE_NORMAL = 0, |
|
22
|
|
|
|
|
|
|
DATE_RELATIVE, |
|
23
|
|
|
|
|
|
|
DATE_SHORT, |
|
24
|
|
|
|
|
|
|
DATE_LOCAL, |
|
25
|
|
|
|
|
|
|
DATE_ISO8601, |
|
26
|
|
|
|
|
|
|
DATE_RFC2822, |
|
27
|
|
|
|
|
|
|
DATE_RAW |
|
28
|
|
|
|
|
|
|
} date_mode; |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
/* |
|
31
|
|
|
|
|
|
|
* This is like mktime, but without normalization of tm_wday and tm_yday. |
|
32
|
|
|
|
|
|
|
*/ |
|
33
|
0
|
|
|
|
|
|
static git_time_t tm_to_time_t(const struct tm *tm) |
|
34
|
|
|
|
|
|
|
{ |
|
35
|
|
|
|
|
|
|
static const int mdays[] = { |
|
36
|
|
|
|
|
|
|
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
|
37
|
|
|
|
|
|
|
}; |
|
38
|
0
|
|
|
|
|
|
int year = tm->tm_year - 70; |
|
39
|
0
|
|
|
|
|
|
int month = tm->tm_mon; |
|
40
|
0
|
|
|
|
|
|
int day = tm->tm_mday; |
|
41
|
|
|
|
|
|
|
|
|
42
|
0
|
0
|
|
|
|
|
if (year < 0 || year > 129) /* algo only works for 1970-2099 */ |
|
|
|
0
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
return -1; |
|
44
|
0
|
0
|
|
|
|
|
if (month < 0 || month > 11) /* array bounds */ |
|
|
|
0
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
return -1; |
|
46
|
0
|
0
|
|
|
|
|
if (month < 2 || (year + 2) % 4) |
|
|
|
0
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
day--; |
|
48
|
0
|
0
|
|
|
|
|
if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0) |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
return -1; |
|
50
|
0
|
|
|
|
|
|
return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + |
|
51
|
0
|
|
|
|
|
|
tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
static const char *month_names[] = { |
|
55
|
|
|
|
|
|
|
"January", "February", "March", "April", "May", "June", |
|
56
|
|
|
|
|
|
|
"July", "August", "September", "October", "November", "December" |
|
57
|
|
|
|
|
|
|
}; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
static const char *weekday_names[] = { |
|
60
|
|
|
|
|
|
|
"Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" |
|
61
|
|
|
|
|
|
|
}; |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
/* |
|
66
|
|
|
|
|
|
|
* Check these. And note how it doesn't do the summer-time conversion. |
|
67
|
|
|
|
|
|
|
* |
|
68
|
|
|
|
|
|
|
* In my world, it's always summer, and things are probably a bit off |
|
69
|
|
|
|
|
|
|
* in other ways too. |
|
70
|
|
|
|
|
|
|
*/ |
|
71
|
|
|
|
|
|
|
static const struct { |
|
72
|
|
|
|
|
|
|
const char *name; |
|
73
|
|
|
|
|
|
|
int offset; |
|
74
|
|
|
|
|
|
|
int dst; |
|
75
|
|
|
|
|
|
|
} timezone_names[] = { |
|
76
|
|
|
|
|
|
|
{ "IDLW", -12, 0, }, /* International Date Line West */ |
|
77
|
|
|
|
|
|
|
{ "NT", -11, 0, }, /* Nome */ |
|
78
|
|
|
|
|
|
|
{ "CAT", -10, 0, }, /* Central Alaska */ |
|
79
|
|
|
|
|
|
|
{ "HST", -10, 0, }, /* Hawaii Standard */ |
|
80
|
|
|
|
|
|
|
{ "HDT", -10, 1, }, /* Hawaii Daylight */ |
|
81
|
|
|
|
|
|
|
{ "YST", -9, 0, }, /* Yukon Standard */ |
|
82
|
|
|
|
|
|
|
{ "YDT", -9, 1, }, /* Yukon Daylight */ |
|
83
|
|
|
|
|
|
|
{ "PST", -8, 0, }, /* Pacific Standard */ |
|
84
|
|
|
|
|
|
|
{ "PDT", -8, 1, }, /* Pacific Daylight */ |
|
85
|
|
|
|
|
|
|
{ "MST", -7, 0, }, /* Mountain Standard */ |
|
86
|
|
|
|
|
|
|
{ "MDT", -7, 1, }, /* Mountain Daylight */ |
|
87
|
|
|
|
|
|
|
{ "CST", -6, 0, }, /* Central Standard */ |
|
88
|
|
|
|
|
|
|
{ "CDT", -6, 1, }, /* Central Daylight */ |
|
89
|
|
|
|
|
|
|
{ "EST", -5, 0, }, /* Eastern Standard */ |
|
90
|
|
|
|
|
|
|
{ "EDT", -5, 1, }, /* Eastern Daylight */ |
|
91
|
|
|
|
|
|
|
{ "AST", -3, 0, }, /* Atlantic Standard */ |
|
92
|
|
|
|
|
|
|
{ "ADT", -3, 1, }, /* Atlantic Daylight */ |
|
93
|
|
|
|
|
|
|
{ "WAT", -1, 0, }, /* West Africa */ |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
{ "GMT", 0, 0, }, /* Greenwich Mean */ |
|
96
|
|
|
|
|
|
|
{ "UTC", 0, 0, }, /* Universal (Coordinated) */ |
|
97
|
|
|
|
|
|
|
{ "Z", 0, 0, }, /* Zulu, alias for UTC */ |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
{ "WET", 0, 0, }, /* Western European */ |
|
100
|
|
|
|
|
|
|
{ "BST", 0, 1, }, /* British Summer */ |
|
101
|
|
|
|
|
|
|
{ "CET", +1, 0, }, /* Central European */ |
|
102
|
|
|
|
|
|
|
{ "MET", +1, 0, }, /* Middle European */ |
|
103
|
|
|
|
|
|
|
{ "MEWT", +1, 0, }, /* Middle European Winter */ |
|
104
|
|
|
|
|
|
|
{ "MEST", +1, 1, }, /* Middle European Summer */ |
|
105
|
|
|
|
|
|
|
{ "CEST", +1, 1, }, /* Central European Summer */ |
|
106
|
|
|
|
|
|
|
{ "MESZ", +1, 1, }, /* Middle European Summer */ |
|
107
|
|
|
|
|
|
|
{ "FWT", +1, 0, }, /* French Winter */ |
|
108
|
|
|
|
|
|
|
{ "FST", +1, 1, }, /* French Summer */ |
|
109
|
|
|
|
|
|
|
{ "EET", +2, 0, }, /* Eastern Europe */ |
|
110
|
|
|
|
|
|
|
{ "EEST", +2, 1, }, /* Eastern European Daylight */ |
|
111
|
|
|
|
|
|
|
{ "WAST", +7, 0, }, /* West Australian Standard */ |
|
112
|
|
|
|
|
|
|
{ "WADT", +7, 1, }, /* West Australian Daylight */ |
|
113
|
|
|
|
|
|
|
{ "CCT", +8, 0, }, /* China Coast */ |
|
114
|
|
|
|
|
|
|
{ "JST", +9, 0, }, /* Japan Standard */ |
|
115
|
|
|
|
|
|
|
{ "EAST", +10, 0, }, /* Eastern Australian Standard */ |
|
116
|
|
|
|
|
|
|
{ "EADT", +10, 1, }, /* Eastern Australian Daylight */ |
|
117
|
|
|
|
|
|
|
{ "GST", +10, 0, }, /* Guam Standard */ |
|
118
|
|
|
|
|
|
|
{ "NZT", +12, 0, }, /* New Zealand */ |
|
119
|
|
|
|
|
|
|
{ "NZST", +12, 0, }, /* New Zealand Standard */ |
|
120
|
|
|
|
|
|
|
{ "NZDT", +12, 1, }, /* New Zealand Daylight */ |
|
121
|
|
|
|
|
|
|
{ "IDLE", +12, 0, }, /* International Date Line East */ |
|
122
|
|
|
|
|
|
|
}; |
|
123
|
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
static size_t match_string(const char *date, const char *str) |
|
125
|
|
|
|
|
|
|
{ |
|
126
|
0
|
|
|
|
|
|
size_t i = 0; |
|
127
|
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
for (i = 0; *date; date++, str++, i++) { |
|
129
|
0
|
0
|
|
|
|
|
if (*date == *str) |
|
130
|
0
|
|
|
|
|
|
continue; |
|
131
|
0
|
0
|
|
|
|
|
if (toupper(*date) == toupper(*str)) |
|
132
|
0
|
|
|
|
|
|
continue; |
|
133
|
0
|
0
|
|
|
|
|
if (!isalnum(*date)) |
|
134
|
0
|
|
|
|
|
|
break; |
|
135
|
0
|
|
|
|
|
|
return 0; |
|
136
|
|
|
|
|
|
|
} |
|
137
|
0
|
|
|
|
|
|
return i; |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
static int skip_alpha(const char *date) |
|
141
|
|
|
|
|
|
|
{ |
|
142
|
0
|
|
|
|
|
|
int i = 0; |
|
143
|
|
|
|
|
|
|
do { |
|
144
|
0
|
|
|
|
|
|
i++; |
|
145
|
0
|
0
|
|
|
|
|
} while (isalpha(date[i])); |
|
146
|
0
|
|
|
|
|
|
return i; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
/* |
|
150
|
|
|
|
|
|
|
* Parse month, weekday, or timezone name |
|
151
|
|
|
|
|
|
|
*/ |
|
152
|
0
|
|
|
|
|
|
static size_t match_alpha(const char *date, struct tm *tm, int *offset) |
|
153
|
|
|
|
|
|
|
{ |
|
154
|
|
|
|
|
|
|
unsigned int i; |
|
155
|
|
|
|
|
|
|
|
|
156
|
0
|
0
|
|
|
|
|
for (i = 0; i < 12; i++) { |
|
157
|
0
|
|
|
|
|
|
size_t match = match_string(date, month_names[i]); |
|
158
|
0
|
0
|
|
|
|
|
if (match >= 3) { |
|
159
|
0
|
|
|
|
|
|
tm->tm_mon = i; |
|
160
|
0
|
|
|
|
|
|
return match; |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
|
|
164
|
0
|
0
|
|
|
|
|
for (i = 0; i < 7; i++) { |
|
165
|
0
|
|
|
|
|
|
size_t match = match_string(date, weekday_names[i]); |
|
166
|
0
|
0
|
|
|
|
|
if (match >= 3) { |
|
167
|
0
|
|
|
|
|
|
tm->tm_wday = i; |
|
168
|
0
|
|
|
|
|
|
return match; |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { |
|
173
|
0
|
|
|
|
|
|
size_t match = match_string(date, timezone_names[i].name); |
|
174
|
0
|
0
|
|
|
|
|
if (match >= 3 || match == strlen(timezone_names[i].name)) { |
|
|
|
0
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
int off = timezone_names[i].offset; |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
/* This is bogus, but we like summer */ |
|
178
|
0
|
|
|
|
|
|
off += timezone_names[i].dst; |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
/* Only use the tz name offset if we don't have anything better */ |
|
181
|
0
|
0
|
|
|
|
|
if (*offset == -1) |
|
182
|
0
|
|
|
|
|
|
*offset = 60*off; |
|
183
|
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
return match; |
|
185
|
|
|
|
|
|
|
} |
|
186
|
|
|
|
|
|
|
} |
|
187
|
|
|
|
|
|
|
|
|
188
|
0
|
0
|
|
|
|
|
if (match_string(date, "PM") == 2) { |
|
189
|
0
|
|
|
|
|
|
tm->tm_hour = (tm->tm_hour % 12) + 12; |
|
190
|
0
|
|
|
|
|
|
return 2; |
|
191
|
|
|
|
|
|
|
} |
|
192
|
|
|
|
|
|
|
|
|
193
|
0
|
0
|
|
|
|
|
if (match_string(date, "AM") == 2) { |
|
194
|
0
|
|
|
|
|
|
tm->tm_hour = (tm->tm_hour % 12) + 0; |
|
195
|
0
|
|
|
|
|
|
return 2; |
|
196
|
|
|
|
|
|
|
} |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
/* BAD */ |
|
199
|
0
|
|
|
|
|
|
return skip_alpha(date); |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) |
|
203
|
|
|
|
|
|
|
{ |
|
204
|
0
|
0
|
|
|
|
|
if (month > 0 && month < 13 && day > 0 && day < 32) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
205
|
0
|
|
|
|
|
|
struct tm check = *tm; |
|
206
|
0
|
0
|
|
|
|
|
struct tm *r = (now_tm ? &check : tm); |
|
207
|
|
|
|
|
|
|
time_t specified; |
|
208
|
|
|
|
|
|
|
|
|
209
|
0
|
|
|
|
|
|
r->tm_mon = month - 1; |
|
210
|
0
|
|
|
|
|
|
r->tm_mday = day; |
|
211
|
0
|
0
|
|
|
|
|
if (year == -1) { |
|
212
|
0
|
0
|
|
|
|
|
if (!now_tm) |
|
213
|
0
|
|
|
|
|
|
return 1; |
|
214
|
0
|
|
|
|
|
|
r->tm_year = now_tm->tm_year; |
|
215
|
|
|
|
|
|
|
} |
|
216
|
0
|
0
|
|
|
|
|
else if (year >= 1970 && year < 2100) |
|
|
|
0
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
r->tm_year = year - 1900; |
|
218
|
0
|
0
|
|
|
|
|
else if (year > 70 && year < 100) |
|
|
|
0
|
|
|
|
|
|
|
219
|
0
|
|
|
|
|
|
r->tm_year = year; |
|
220
|
0
|
0
|
|
|
|
|
else if (year < 38) |
|
221
|
0
|
|
|
|
|
|
r->tm_year = year + 100; |
|
222
|
|
|
|
|
|
|
else |
|
223
|
0
|
|
|
|
|
|
return 0; |
|
224
|
0
|
0
|
|
|
|
|
if (!now_tm) |
|
225
|
0
|
|
|
|
|
|
return 1; |
|
226
|
|
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
|
specified = tm_to_time_t(r); |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
/* Be it commit time or author time, it does not make |
|
230
|
|
|
|
|
|
|
* sense to specify timestamp way into the future. Make |
|
231
|
|
|
|
|
|
|
* sure it is not later than ten days from now... |
|
232
|
|
|
|
|
|
|
*/ |
|
233
|
0
|
0
|
|
|
|
|
if (now + 10*24*3600 < specified) |
|
234
|
0
|
|
|
|
|
|
return 0; |
|
235
|
0
|
|
|
|
|
|
tm->tm_mon = r->tm_mon; |
|
236
|
0
|
|
|
|
|
|
tm->tm_mday = r->tm_mday; |
|
237
|
0
|
0
|
|
|
|
|
if (year != -1) |
|
238
|
0
|
|
|
|
|
|
tm->tm_year = r->tm_year; |
|
239
|
0
|
|
|
|
|
|
return 1; |
|
240
|
|
|
|
|
|
|
} |
|
241
|
0
|
|
|
|
|
|
return 0; |
|
242
|
|
|
|
|
|
|
} |
|
243
|
|
|
|
|
|
|
|
|
244
|
0
|
|
|
|
|
|
static size_t match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm) |
|
245
|
|
|
|
|
|
|
{ |
|
246
|
|
|
|
|
|
|
time_t now; |
|
247
|
|
|
|
|
|
|
struct tm now_tm; |
|
248
|
|
|
|
|
|
|
struct tm *refuse_future; |
|
249
|
|
|
|
|
|
|
long num2, num3; |
|
250
|
|
|
|
|
|
|
|
|
251
|
0
|
|
|
|
|
|
num2 = strtol(end+1, &end, 10); |
|
252
|
0
|
|
|
|
|
|
num3 = -1; |
|
253
|
0
|
0
|
|
|
|
|
if (*end == c && isdigit(end[1])) |
|
|
|
0
|
|
|
|
|
|
|
254
|
0
|
|
|
|
|
|
num3 = strtol(end+1, &end, 10); |
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
/* Time? Date? */ |
|
257
|
0
|
|
|
|
|
|
switch (c) { |
|
258
|
|
|
|
|
|
|
case ':': |
|
259
|
0
|
0
|
|
|
|
|
if (num3 < 0) |
|
260
|
0
|
|
|
|
|
|
num3 = 0; |
|
261
|
0
|
0
|
|
|
|
|
if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
262
|
0
|
|
|
|
|
|
tm->tm_hour = num; |
|
263
|
0
|
|
|
|
|
|
tm->tm_min = num2; |
|
264
|
0
|
|
|
|
|
|
tm->tm_sec = num3; |
|
265
|
0
|
|
|
|
|
|
break; |
|
266
|
|
|
|
|
|
|
} |
|
267
|
0
|
|
|
|
|
|
return 0; |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
case '-': |
|
270
|
|
|
|
|
|
|
case '/': |
|
271
|
|
|
|
|
|
|
case '.': |
|
272
|
0
|
|
|
|
|
|
now = time(NULL); |
|
273
|
0
|
|
|
|
|
|
refuse_future = NULL; |
|
274
|
0
|
0
|
|
|
|
|
if (p_gmtime_r(&now, &now_tm)) |
|
275
|
0
|
|
|
|
|
|
refuse_future = &now_tm; |
|
276
|
|
|
|
|
|
|
|
|
277
|
0
|
0
|
|
|
|
|
if (num > 70) { |
|
278
|
|
|
|
|
|
|
/* yyyy-mm-dd? */ |
|
279
|
0
|
0
|
|
|
|
|
if (is_date(num, num2, num3, refuse_future, now, tm)) |
|
280
|
0
|
|
|
|
|
|
break; |
|
281
|
|
|
|
|
|
|
/* yyyy-dd-mm? */ |
|
282
|
0
|
0
|
|
|
|
|
if (is_date(num, num3, num2, refuse_future, now, tm)) |
|
283
|
0
|
|
|
|
|
|
break; |
|
284
|
|
|
|
|
|
|
} |
|
285
|
|
|
|
|
|
|
/* Our eastern European friends say dd.mm.yy[yy] |
|
286
|
|
|
|
|
|
|
* is the norm there, so giving precedence to |
|
287
|
|
|
|
|
|
|
* mm/dd/yy[yy] form only when separator is not '.' |
|
288
|
|
|
|
|
|
|
*/ |
|
289
|
0
|
|
|
|
|
|
if (c != '.' && |
|
290
|
0
|
|
|
|
|
|
is_date(num3, num, num2, refuse_future, now, tm)) |
|
291
|
0
|
|
|
|
|
|
break; |
|
292
|
|
|
|
|
|
|
/* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ |
|
293
|
0
|
0
|
|
|
|
|
if (is_date(num3, num2, num, refuse_future, now, tm)) |
|
294
|
0
|
|
|
|
|
|
break; |
|
295
|
|
|
|
|
|
|
/* Funny European mm.dd.yy */ |
|
296
|
0
|
|
|
|
|
|
if (c == '.' && |
|
297
|
0
|
|
|
|
|
|
is_date(num3, num, num2, refuse_future, now, tm)) |
|
298
|
0
|
|
|
|
|
|
break; |
|
299
|
0
|
|
|
|
|
|
return 0; |
|
300
|
|
|
|
|
|
|
} |
|
301
|
0
|
|
|
|
|
|
return end - date; |
|
302
|
|
|
|
|
|
|
} |
|
303
|
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
/* |
|
305
|
|
|
|
|
|
|
* Have we filled in any part of the time/date yet? |
|
306
|
|
|
|
|
|
|
* We just do a binary 'and' to see if the sign bit |
|
307
|
|
|
|
|
|
|
* is set in all the values. |
|
308
|
|
|
|
|
|
|
*/ |
|
309
|
0
|
|
|
|
|
|
static int nodate(struct tm *tm) |
|
310
|
|
|
|
|
|
|
{ |
|
311
|
0
|
|
|
|
|
|
return (tm->tm_year & |
|
312
|
0
|
|
|
|
|
|
tm->tm_mon & |
|
313
|
0
|
|
|
|
|
|
tm->tm_mday & |
|
314
|
0
|
|
|
|
|
|
tm->tm_hour & |
|
315
|
0
|
|
|
|
|
|
tm->tm_min & |
|
316
|
0
|
|
|
|
|
|
tm->tm_sec) < 0; |
|
317
|
|
|
|
|
|
|
} |
|
318
|
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
/* |
|
320
|
|
|
|
|
|
|
* We've seen a digit. Time? Year? Date? |
|
321
|
|
|
|
|
|
|
*/ |
|
322
|
0
|
|
|
|
|
|
static size_t match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) |
|
323
|
|
|
|
|
|
|
{ |
|
324
|
|
|
|
|
|
|
size_t n; |
|
325
|
|
|
|
|
|
|
char *end; |
|
326
|
|
|
|
|
|
|
unsigned long num; |
|
327
|
|
|
|
|
|
|
|
|
328
|
0
|
|
|
|
|
|
num = strtoul(date, &end, 10); |
|
329
|
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
/* |
|
331
|
|
|
|
|
|
|
* Seconds since 1970? We trigger on that for any numbers with |
|
332
|
|
|
|
|
|
|
* more than 8 digits. This is because we don't want to rule out |
|
333
|
|
|
|
|
|
|
* numbers like 20070606 as a YYYYMMDD date. |
|
334
|
|
|
|
|
|
|
*/ |
|
335
|
0
|
0
|
|
|
|
|
if (num >= 100000000 && nodate(tm)) { |
|
|
|
0
|
|
|
|
|
|
|
336
|
0
|
|
|
|
|
|
time_t time = num; |
|
337
|
0
|
0
|
|
|
|
|
if (p_gmtime_r(&time, tm)) { |
|
338
|
0
|
|
|
|
|
|
*tm_gmt = 1; |
|
339
|
0
|
|
|
|
|
|
return end - date; |
|
340
|
|
|
|
|
|
|
} |
|
341
|
|
|
|
|
|
|
} |
|
342
|
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
/* |
|
344
|
|
|
|
|
|
|
* Check for special formats: num[-.:/]num[same]num |
|
345
|
|
|
|
|
|
|
*/ |
|
346
|
0
|
0
|
|
|
|
|
switch (*end) { |
|
347
|
|
|
|
|
|
|
case ':': |
|
348
|
|
|
|
|
|
|
case '.': |
|
349
|
|
|
|
|
|
|
case '/': |
|
350
|
|
|
|
|
|
|
case '-': |
|
351
|
0
|
0
|
|
|
|
|
if (isdigit(end[1])) { |
|
352
|
0
|
|
|
|
|
|
size_t match = match_multi_number(num, *end, date, end, tm); |
|
353
|
0
|
0
|
|
|
|
|
if (match) |
|
354
|
0
|
|
|
|
|
|
return match; |
|
355
|
|
|
|
|
|
|
} |
|
356
|
|
|
|
|
|
|
} |
|
357
|
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
/* |
|
359
|
|
|
|
|
|
|
* None of the special formats? Try to guess what |
|
360
|
|
|
|
|
|
|
* the number meant. We use the number of digits |
|
361
|
|
|
|
|
|
|
* to make a more educated guess.. |
|
362
|
|
|
|
|
|
|
*/ |
|
363
|
0
|
|
|
|
|
|
n = 0; |
|
364
|
|
|
|
|
|
|
do { |
|
365
|
0
|
|
|
|
|
|
n++; |
|
366
|
0
|
0
|
|
|
|
|
} while (isdigit(date[n])); |
|
367
|
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
/* Four-digit year or a timezone? */ |
|
369
|
0
|
0
|
|
|
|
|
if (n == 4) { |
|
370
|
0
|
0
|
|
|
|
|
if (num <= 1400 && *offset == -1) { |
|
|
|
0
|
|
|
|
|
|
|
371
|
0
|
|
|
|
|
|
unsigned int minutes = num % 100; |
|
372
|
0
|
|
|
|
|
|
unsigned int hours = num / 100; |
|
373
|
0
|
|
|
|
|
|
*offset = hours*60 + minutes; |
|
374
|
0
|
0
|
|
|
|
|
} else if (num > 1900 && num < 2100) |
|
|
|
0
|
|
|
|
|
|
|
375
|
0
|
|
|
|
|
|
tm->tm_year = num - 1900; |
|
376
|
0
|
|
|
|
|
|
return n; |
|
377
|
|
|
|
|
|
|
} |
|
378
|
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
/* |
|
380
|
|
|
|
|
|
|
* Ignore lots of numerals. We took care of 4-digit years above. |
|
381
|
|
|
|
|
|
|
* Days or months must be one or two digits. |
|
382
|
|
|
|
|
|
|
*/ |
|
383
|
0
|
0
|
|
|
|
|
if (n > 2) |
|
384
|
0
|
|
|
|
|
|
return n; |
|
385
|
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
/* |
|
387
|
|
|
|
|
|
|
* NOTE! We will give precedence to day-of-month over month or |
|
388
|
|
|
|
|
|
|
* year numbers in the 1-12 range. So 05 is always "mday 5", |
|
389
|
|
|
|
|
|
|
* unless we already have a mday.. |
|
390
|
|
|
|
|
|
|
* |
|
391
|
|
|
|
|
|
|
* IOW, 01 Apr 05 parses as "April 1st, 2005". |
|
392
|
|
|
|
|
|
|
*/ |
|
393
|
0
|
0
|
|
|
|
|
if (num > 0 && num < 32 && tm->tm_mday < 0) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
394
|
0
|
|
|
|
|
|
tm->tm_mday = num; |
|
395
|
0
|
|
|
|
|
|
return n; |
|
396
|
|
|
|
|
|
|
} |
|
397
|
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
/* Two-digit year? */ |
|
399
|
0
|
0
|
|
|
|
|
if (n == 2 && tm->tm_year < 0) { |
|
|
|
0
|
|
|
|
|
|
|
400
|
0
|
0
|
|
|
|
|
if (num < 10 && tm->tm_mday >= 0) { |
|
|
|
0
|
|
|
|
|
|
|
401
|
0
|
|
|
|
|
|
tm->tm_year = num + 100; |
|
402
|
0
|
|
|
|
|
|
return n; |
|
403
|
|
|
|
|
|
|
} |
|
404
|
0
|
0
|
|
|
|
|
if (num >= 70) { |
|
405
|
0
|
|
|
|
|
|
tm->tm_year = num; |
|
406
|
0
|
|
|
|
|
|
return n; |
|
407
|
|
|
|
|
|
|
} |
|
408
|
|
|
|
|
|
|
} |
|
409
|
|
|
|
|
|
|
|
|
410
|
0
|
0
|
|
|
|
|
if (num > 0 && num < 13 && tm->tm_mon < 0) |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
411
|
0
|
|
|
|
|
|
tm->tm_mon = num-1; |
|
412
|
|
|
|
|
|
|
|
|
413
|
0
|
|
|
|
|
|
return n; |
|
414
|
|
|
|
|
|
|
} |
|
415
|
|
|
|
|
|
|
|
|
416
|
0
|
|
|
|
|
|
static size_t match_tz(const char *date, int *offp) |
|
417
|
|
|
|
|
|
|
{ |
|
418
|
|
|
|
|
|
|
char *end; |
|
419
|
0
|
|
|
|
|
|
int hour = strtoul(date + 1, &end, 10); |
|
420
|
0
|
|
|
|
|
|
size_t n = end - (date + 1); |
|
421
|
0
|
|
|
|
|
|
int min = 0; |
|
422
|
|
|
|
|
|
|
|
|
423
|
0
|
0
|
|
|
|
|
if (n == 4) { |
|
424
|
|
|
|
|
|
|
/* hhmm */ |
|
425
|
0
|
|
|
|
|
|
min = hour % 100; |
|
426
|
0
|
|
|
|
|
|
hour = hour / 100; |
|
427
|
0
|
0
|
|
|
|
|
} else if (n != 2) { |
|
428
|
0
|
|
|
|
|
|
min = 99; /* random stuff */ |
|
429
|
0
|
0
|
|
|
|
|
} else if (*end == ':') { |
|
430
|
|
|
|
|
|
|
/* hh:mm? */ |
|
431
|
0
|
|
|
|
|
|
min = strtoul(end + 1, &end, 10); |
|
432
|
0
|
0
|
|
|
|
|
if (end - (date + 1) != 5) |
|
433
|
0
|
|
|
|
|
|
min = 99; /* random stuff */ |
|
434
|
|
|
|
|
|
|
} /* otherwise we parsed "hh" */ |
|
435
|
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
/* |
|
437
|
|
|
|
|
|
|
* Don't accept any random stuff. Even though some places have |
|
438
|
|
|
|
|
|
|
* offset larger than 12 hours (e.g. Pacific/Kiritimati is at |
|
439
|
|
|
|
|
|
|
* UTC+14), there is something wrong if hour part is much |
|
440
|
|
|
|
|
|
|
* larger than that. We might also want to check that the |
|
441
|
|
|
|
|
|
|
* minutes are divisible by 15 or something too. (Offset of |
|
442
|
|
|
|
|
|
|
* Kathmandu, Nepal is UTC+5:45) |
|
443
|
|
|
|
|
|
|
*/ |
|
444
|
0
|
0
|
|
|
|
|
if (min < 60 && hour < 24) { |
|
|
|
0
|
|
|
|
|
|
|
445
|
0
|
|
|
|
|
|
int offset = hour * 60 + min; |
|
446
|
0
|
0
|
|
|
|
|
if (*date == '-') |
|
447
|
0
|
|
|
|
|
|
offset = -offset; |
|
448
|
0
|
|
|
|
|
|
*offp = offset; |
|
449
|
|
|
|
|
|
|
} |
|
450
|
0
|
|
|
|
|
|
return end - date; |
|
451
|
|
|
|
|
|
|
} |
|
452
|
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
/* |
|
454
|
|
|
|
|
|
|
* Parse a string like "0 +0000" as ancient timestamp near epoch, but |
|
455
|
|
|
|
|
|
|
* only when it appears not as part of any other string. |
|
456
|
|
|
|
|
|
|
*/ |
|
457
|
0
|
|
|
|
|
|
static int match_object_header_date(const char *date, git_time_t *timestamp, int *offset) |
|
458
|
|
|
|
|
|
|
{ |
|
459
|
|
|
|
|
|
|
char *end; |
|
460
|
|
|
|
|
|
|
unsigned long stamp; |
|
461
|
|
|
|
|
|
|
int ofs; |
|
462
|
|
|
|
|
|
|
|
|
463
|
0
|
0
|
|
|
|
|
if (*date < '0' || '9' <= *date) |
|
|
|
0
|
|
|
|
|
|
|
464
|
0
|
|
|
|
|
|
return -1; |
|
465
|
0
|
|
|
|
|
|
stamp = strtoul(date, &end, 10); |
|
466
|
0
|
0
|
|
|
|
|
if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-')) |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
467
|
0
|
|
|
|
|
|
return -1; |
|
468
|
0
|
|
|
|
|
|
date = end + 2; |
|
469
|
0
|
|
|
|
|
|
ofs = strtol(date, &end, 10); |
|
470
|
0
|
0
|
|
|
|
|
if ((*end != '\0' && (*end != '\n')) || end != date + 4) |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
471
|
0
|
|
|
|
|
|
return -1; |
|
472
|
0
|
|
|
|
|
|
ofs = (ofs / 100) * 60 + (ofs % 100); |
|
473
|
0
|
0
|
|
|
|
|
if (date[-1] == '-') |
|
474
|
0
|
|
|
|
|
|
ofs = -ofs; |
|
475
|
0
|
|
|
|
|
|
*timestamp = stamp; |
|
476
|
0
|
|
|
|
|
|
*offset = ofs; |
|
477
|
0
|
|
|
|
|
|
return 0; |
|
478
|
|
|
|
|
|
|
} |
|
479
|
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 |
|
481
|
|
|
|
|
|
|
(i.e. English) day/month names, and it doesn't work correctly with %z. */ |
|
482
|
0
|
|
|
|
|
|
static int parse_date_basic(const char *date, git_time_t *timestamp, int *offset) |
|
483
|
|
|
|
|
|
|
{ |
|
484
|
|
|
|
|
|
|
struct tm tm; |
|
485
|
|
|
|
|
|
|
int tm_gmt; |
|
486
|
|
|
|
|
|
|
git_time_t dummy_timestamp; |
|
487
|
|
|
|
|
|
|
int dummy_offset; |
|
488
|
|
|
|
|
|
|
|
|
489
|
0
|
0
|
|
|
|
|
if (!timestamp) |
|
490
|
0
|
|
|
|
|
|
timestamp = &dummy_timestamp; |
|
491
|
0
|
0
|
|
|
|
|
if (!offset) |
|
492
|
0
|
|
|
|
|
|
offset = &dummy_offset; |
|
493
|
|
|
|
|
|
|
|
|
494
|
0
|
|
|
|
|
|
memset(&tm, 0, sizeof(tm)); |
|
495
|
0
|
|
|
|
|
|
tm.tm_year = -1; |
|
496
|
0
|
|
|
|
|
|
tm.tm_mon = -1; |
|
497
|
0
|
|
|
|
|
|
tm.tm_mday = -1; |
|
498
|
0
|
|
|
|
|
|
tm.tm_isdst = -1; |
|
499
|
0
|
|
|
|
|
|
tm.tm_hour = -1; |
|
500
|
0
|
|
|
|
|
|
tm.tm_min = -1; |
|
501
|
0
|
|
|
|
|
|
tm.tm_sec = -1; |
|
502
|
0
|
|
|
|
|
|
*offset = -1; |
|
503
|
0
|
|
|
|
|
|
tm_gmt = 0; |
|
504
|
|
|
|
|
|
|
|
|
505
|
0
|
|
|
|
|
|
if (*date == '@' && |
|
506
|
0
|
|
|
|
|
|
!match_object_header_date(date + 1, timestamp, offset)) |
|
507
|
0
|
|
|
|
|
|
return 0; /* success */ |
|
508
|
|
|
|
|
|
|
for (;;) { |
|
509
|
0
|
|
|
|
|
|
size_t match = 0; |
|
510
|
0
|
|
|
|
|
|
unsigned char c = *date; |
|
511
|
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
/* Stop at end of string or newline */ |
|
513
|
0
|
0
|
|
|
|
|
if (!c || c == '\n') |
|
|
|
0
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
break; |
|
515
|
|
|
|
|
|
|
|
|
516
|
0
|
0
|
|
|
|
|
if (isalpha(c)) |
|
517
|
0
|
|
|
|
|
|
match = match_alpha(date, &tm, offset); |
|
518
|
0
|
0
|
|
|
|
|
else if (isdigit(c)) |
|
519
|
0
|
|
|
|
|
|
match = match_digit(date, &tm, offset, &tm_gmt); |
|
520
|
0
|
0
|
|
|
|
|
else if ((c == '-' || c == '+') && isdigit(date[1])) |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
521
|
0
|
|
|
|
|
|
match = match_tz(date, offset); |
|
522
|
|
|
|
|
|
|
|
|
523
|
0
|
0
|
|
|
|
|
if (!match) { |
|
524
|
|
|
|
|
|
|
/* BAD */ |
|
525
|
0
|
|
|
|
|
|
match = 1; |
|
526
|
|
|
|
|
|
|
} |
|
527
|
|
|
|
|
|
|
|
|
528
|
0
|
|
|
|
|
|
date += match; |
|
529
|
0
|
|
|
|
|
|
} |
|
530
|
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
/* mktime uses local timezone */ |
|
532
|
0
|
|
|
|
|
|
*timestamp = tm_to_time_t(&tm); |
|
533
|
0
|
0
|
|
|
|
|
if (*offset == -1) |
|
534
|
0
|
|
|
|
|
|
*offset = (int)((time_t)*timestamp - mktime(&tm)) / 60; |
|
535
|
|
|
|
|
|
|
|
|
536
|
0
|
0
|
|
|
|
|
if (*timestamp == (git_time_t)-1) |
|
537
|
0
|
|
|
|
|
|
return -1; |
|
538
|
|
|
|
|
|
|
|
|
539
|
0
|
0
|
|
|
|
|
if (!tm_gmt) |
|
540
|
0
|
|
|
|
|
|
*timestamp -= *offset * 60; |
|
541
|
0
|
|
|
|
|
|
return 0; /* success */ |
|
542
|
|
|
|
|
|
|
} |
|
543
|
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
/* |
|
546
|
|
|
|
|
|
|
* Relative time update (eg "2 days ago"). If we haven't set the time |
|
547
|
|
|
|
|
|
|
* yet, we need to set it from current time. |
|
548
|
|
|
|
|
|
|
*/ |
|
549
|
0
|
|
|
|
|
|
static git_time_t update_tm(struct tm *tm, struct tm *now, unsigned long sec) |
|
550
|
|
|
|
|
|
|
{ |
|
551
|
|
|
|
|
|
|
time_t n; |
|
552
|
|
|
|
|
|
|
|
|
553
|
0
|
0
|
|
|
|
|
if (tm->tm_mday < 0) |
|
554
|
0
|
|
|
|
|
|
tm->tm_mday = now->tm_mday; |
|
555
|
0
|
0
|
|
|
|
|
if (tm->tm_mon < 0) |
|
556
|
0
|
|
|
|
|
|
tm->tm_mon = now->tm_mon; |
|
557
|
0
|
0
|
|
|
|
|
if (tm->tm_year < 0) { |
|
558
|
0
|
|
|
|
|
|
tm->tm_year = now->tm_year; |
|
559
|
0
|
0
|
|
|
|
|
if (tm->tm_mon > now->tm_mon) |
|
560
|
0
|
|
|
|
|
|
tm->tm_year--; |
|
561
|
|
|
|
|
|
|
} |
|
562
|
|
|
|
|
|
|
|
|
563
|
0
|
|
|
|
|
|
n = mktime(tm) - sec; |
|
564
|
0
|
|
|
|
|
|
p_localtime_r(&n, tm); |
|
565
|
0
|
|
|
|
|
|
return n; |
|
566
|
|
|
|
|
|
|
} |
|
567
|
|
|
|
|
|
|
|
|
568
|
0
|
|
|
|
|
|
static void date_now(struct tm *tm, struct tm *now, int *num) |
|
569
|
|
|
|
|
|
|
{ |
|
570
|
|
|
|
|
|
|
GIT_UNUSED(num); |
|
571
|
0
|
|
|
|
|
|
update_tm(tm, now, 0); |
|
572
|
0
|
|
|
|
|
|
} |
|
573
|
|
|
|
|
|
|
|
|
574
|
0
|
|
|
|
|
|
static void date_yesterday(struct tm *tm, struct tm *now, int *num) |
|
575
|
|
|
|
|
|
|
{ |
|
576
|
|
|
|
|
|
|
GIT_UNUSED(num); |
|
577
|
0
|
|
|
|
|
|
update_tm(tm, now, 24*60*60); |
|
578
|
0
|
|
|
|
|
|
} |
|
579
|
|
|
|
|
|
|
|
|
580
|
0
|
|
|
|
|
|
static void date_time(struct tm *tm, struct tm *now, int hour) |
|
581
|
|
|
|
|
|
|
{ |
|
582
|
0
|
0
|
|
|
|
|
if (tm->tm_hour < hour) |
|
583
|
0
|
|
|
|
|
|
date_yesterday(tm, now, NULL); |
|
584
|
0
|
|
|
|
|
|
tm->tm_hour = hour; |
|
585
|
0
|
|
|
|
|
|
tm->tm_min = 0; |
|
586
|
0
|
|
|
|
|
|
tm->tm_sec = 0; |
|
587
|
0
|
|
|
|
|
|
} |
|
588
|
|
|
|
|
|
|
|
|
589
|
0
|
|
|
|
|
|
static void date_midnight(struct tm *tm, struct tm *now, int *num) |
|
590
|
|
|
|
|
|
|
{ |
|
591
|
|
|
|
|
|
|
GIT_UNUSED(num); |
|
592
|
0
|
|
|
|
|
|
date_time(tm, now, 0); |
|
593
|
0
|
|
|
|
|
|
} |
|
594
|
|
|
|
|
|
|
|
|
595
|
0
|
|
|
|
|
|
static void date_noon(struct tm *tm, struct tm *now, int *num) |
|
596
|
|
|
|
|
|
|
{ |
|
597
|
|
|
|
|
|
|
GIT_UNUSED(num); |
|
598
|
0
|
|
|
|
|
|
date_time(tm, now, 12); |
|
599
|
0
|
|
|
|
|
|
} |
|
600
|
|
|
|
|
|
|
|
|
601
|
0
|
|
|
|
|
|
static void date_tea(struct tm *tm, struct tm *now, int *num) |
|
602
|
|
|
|
|
|
|
{ |
|
603
|
|
|
|
|
|
|
GIT_UNUSED(num); |
|
604
|
0
|
|
|
|
|
|
date_time(tm, now, 17); |
|
605
|
0
|
|
|
|
|
|
} |
|
606
|
|
|
|
|
|
|
|
|
607
|
0
|
|
|
|
|
|
static void date_pm(struct tm *tm, struct tm *now, int *num) |
|
608
|
|
|
|
|
|
|
{ |
|
609
|
0
|
|
|
|
|
|
int hour, n = *num; |
|
610
|
0
|
|
|
|
|
|
*num = 0; |
|
611
|
|
|
|
|
|
|
GIT_UNUSED(now); |
|
612
|
|
|
|
|
|
|
|
|
613
|
0
|
|
|
|
|
|
hour = tm->tm_hour; |
|
614
|
0
|
0
|
|
|
|
|
if (n) { |
|
615
|
0
|
|
|
|
|
|
hour = n; |
|
616
|
0
|
|
|
|
|
|
tm->tm_min = 0; |
|
617
|
0
|
|
|
|
|
|
tm->tm_sec = 0; |
|
618
|
|
|
|
|
|
|
} |
|
619
|
0
|
|
|
|
|
|
tm->tm_hour = (hour % 12) + 12; |
|
620
|
0
|
|
|
|
|
|
} |
|
621
|
|
|
|
|
|
|
|
|
622
|
0
|
|
|
|
|
|
static void date_am(struct tm *tm, struct tm *now, int *num) |
|
623
|
|
|
|
|
|
|
{ |
|
624
|
0
|
|
|
|
|
|
int hour, n = *num; |
|
625
|
0
|
|
|
|
|
|
*num = 0; |
|
626
|
|
|
|
|
|
|
GIT_UNUSED(now); |
|
627
|
|
|
|
|
|
|
|
|
628
|
0
|
|
|
|
|
|
hour = tm->tm_hour; |
|
629
|
0
|
0
|
|
|
|
|
if (n) { |
|
630
|
0
|
|
|
|
|
|
hour = n; |
|
631
|
0
|
|
|
|
|
|
tm->tm_min = 0; |
|
632
|
0
|
|
|
|
|
|
tm->tm_sec = 0; |
|
633
|
|
|
|
|
|
|
} |
|
634
|
0
|
|
|
|
|
|
tm->tm_hour = (hour % 12); |
|
635
|
0
|
|
|
|
|
|
} |
|
636
|
|
|
|
|
|
|
|
|
637
|
0
|
|
|
|
|
|
static void date_never(struct tm *tm, struct tm *now, int *num) |
|
638
|
|
|
|
|
|
|
{ |
|
639
|
0
|
|
|
|
|
|
time_t n = 0; |
|
640
|
|
|
|
|
|
|
GIT_UNUSED(now); |
|
641
|
|
|
|
|
|
|
GIT_UNUSED(num); |
|
642
|
0
|
|
|
|
|
|
p_localtime_r(&n, tm); |
|
643
|
0
|
|
|
|
|
|
} |
|
644
|
|
|
|
|
|
|
|
|
645
|
|
|
|
|
|
|
static const struct special { |
|
646
|
|
|
|
|
|
|
const char *name; |
|
647
|
|
|
|
|
|
|
void (*fn)(struct tm *, struct tm *, int *); |
|
648
|
|
|
|
|
|
|
} special[] = { |
|
649
|
|
|
|
|
|
|
{ "yesterday", date_yesterday }, |
|
650
|
|
|
|
|
|
|
{ "noon", date_noon }, |
|
651
|
|
|
|
|
|
|
{ "midnight", date_midnight }, |
|
652
|
|
|
|
|
|
|
{ "tea", date_tea }, |
|
653
|
|
|
|
|
|
|
{ "PM", date_pm }, |
|
654
|
|
|
|
|
|
|
{ "AM", date_am }, |
|
655
|
|
|
|
|
|
|
{ "never", date_never }, |
|
656
|
|
|
|
|
|
|
{ "now", date_now }, |
|
657
|
|
|
|
|
|
|
{ NULL } |
|
658
|
|
|
|
|
|
|
}; |
|
659
|
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
static const char *number_name[] = { |
|
661
|
|
|
|
|
|
|
"zero", "one", "two", "three", "four", |
|
662
|
|
|
|
|
|
|
"five", "six", "seven", "eight", "nine", "ten", |
|
663
|
|
|
|
|
|
|
}; |
|
664
|
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
static const struct typelen { |
|
666
|
|
|
|
|
|
|
const char *type; |
|
667
|
|
|
|
|
|
|
int length; |
|
668
|
|
|
|
|
|
|
} typelen[] = { |
|
669
|
|
|
|
|
|
|
{ "seconds", 1 }, |
|
670
|
|
|
|
|
|
|
{ "minutes", 60 }, |
|
671
|
|
|
|
|
|
|
{ "hours", 60*60 }, |
|
672
|
|
|
|
|
|
|
{ "days", 24*60*60 }, |
|
673
|
|
|
|
|
|
|
{ "weeks", 7*24*60*60 }, |
|
674
|
|
|
|
|
|
|
{ NULL } |
|
675
|
|
|
|
|
|
|
}; |
|
676
|
|
|
|
|
|
|
|
|
677
|
0
|
|
|
|
|
|
static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched) |
|
678
|
|
|
|
|
|
|
{ |
|
679
|
|
|
|
|
|
|
const struct typelen *tl; |
|
680
|
|
|
|
|
|
|
const struct special *s; |
|
681
|
0
|
|
|
|
|
|
const char *end = date; |
|
682
|
|
|
|
|
|
|
int i; |
|
683
|
|
|
|
|
|
|
|
|
684
|
0
|
0
|
|
|
|
|
while (isalpha(*++end)) |
|
685
|
|
|
|
|
|
|
/* scan to non-alpha */; |
|
686
|
|
|
|
|
|
|
|
|
687
|
0
|
0
|
|
|
|
|
for (i = 0; i < 12; i++) { |
|
688
|
0
|
|
|
|
|
|
size_t match = match_string(date, month_names[i]); |
|
689
|
0
|
0
|
|
|
|
|
if (match >= 3) { |
|
690
|
0
|
|
|
|
|
|
tm->tm_mon = i; |
|
691
|
0
|
|
|
|
|
|
*touched = 1; |
|
692
|
0
|
|
|
|
|
|
return end; |
|
693
|
|
|
|
|
|
|
} |
|
694
|
|
|
|
|
|
|
} |
|
695
|
|
|
|
|
|
|
|
|
696
|
0
|
0
|
|
|
|
|
for (s = special; s->name; s++) { |
|
697
|
0
|
|
|
|
|
|
size_t len = strlen(s->name); |
|
698
|
0
|
0
|
|
|
|
|
if (match_string(date, s->name) == len) { |
|
699
|
0
|
|
|
|
|
|
s->fn(tm, now, num); |
|
700
|
0
|
|
|
|
|
|
*touched = 1; |
|
701
|
0
|
|
|
|
|
|
return end; |
|
702
|
|
|
|
|
|
|
} |
|
703
|
|
|
|
|
|
|
} |
|
704
|
|
|
|
|
|
|
|
|
705
|
0
|
0
|
|
|
|
|
if (!*num) { |
|
706
|
0
|
0
|
|
|
|
|
for (i = 1; i < 11; i++) { |
|
707
|
0
|
|
|
|
|
|
size_t len = strlen(number_name[i]); |
|
708
|
0
|
0
|
|
|
|
|
if (match_string(date, number_name[i]) == len) { |
|
709
|
0
|
|
|
|
|
|
*num = i; |
|
710
|
0
|
|
|
|
|
|
*touched = 1; |
|
711
|
0
|
|
|
|
|
|
return end; |
|
712
|
|
|
|
|
|
|
} |
|
713
|
|
|
|
|
|
|
} |
|
714
|
0
|
0
|
|
|
|
|
if (match_string(date, "last") == 4) { |
|
715
|
0
|
|
|
|
|
|
*num = 1; |
|
716
|
0
|
|
|
|
|
|
*touched = 1; |
|
717
|
|
|
|
|
|
|
} |
|
718
|
0
|
|
|
|
|
|
return end; |
|
719
|
|
|
|
|
|
|
} |
|
720
|
|
|
|
|
|
|
|
|
721
|
0
|
|
|
|
|
|
tl = typelen; |
|
722
|
0
|
0
|
|
|
|
|
while (tl->type) { |
|
723
|
0
|
|
|
|
|
|
size_t len = strlen(tl->type); |
|
724
|
0
|
0
|
|
|
|
|
if (match_string(date, tl->type) >= len-1) { |
|
725
|
0
|
|
|
|
|
|
update_tm(tm, now, tl->length * *num); |
|
726
|
0
|
|
|
|
|
|
*num = 0; |
|
727
|
0
|
|
|
|
|
|
*touched = 1; |
|
728
|
0
|
|
|
|
|
|
return end; |
|
729
|
|
|
|
|
|
|
} |
|
730
|
0
|
|
|
|
|
|
tl++; |
|
731
|
|
|
|
|
|
|
} |
|
732
|
|
|
|
|
|
|
|
|
733
|
0
|
0
|
|
|
|
|
for (i = 0; i < 7; i++) { |
|
734
|
0
|
|
|
|
|
|
size_t match = match_string(date, weekday_names[i]); |
|
735
|
0
|
0
|
|
|
|
|
if (match >= 3) { |
|
736
|
0
|
|
|
|
|
|
int diff, n = *num -1; |
|
737
|
0
|
|
|
|
|
|
*num = 0; |
|
738
|
|
|
|
|
|
|
|
|
739
|
0
|
|
|
|
|
|
diff = tm->tm_wday - i; |
|
740
|
0
|
0
|
|
|
|
|
if (diff <= 0) |
|
741
|
0
|
|
|
|
|
|
n++; |
|
742
|
0
|
|
|
|
|
|
diff += 7*n; |
|
743
|
|
|
|
|
|
|
|
|
744
|
0
|
|
|
|
|
|
update_tm(tm, now, diff * 24 * 60 * 60); |
|
745
|
0
|
|
|
|
|
|
*touched = 1; |
|
746
|
0
|
|
|
|
|
|
return end; |
|
747
|
|
|
|
|
|
|
} |
|
748
|
|
|
|
|
|
|
} |
|
749
|
|
|
|
|
|
|
|
|
750
|
0
|
0
|
|
|
|
|
if (match_string(date, "months") >= 5) { |
|
751
|
|
|
|
|
|
|
int n; |
|
752
|
0
|
|
|
|
|
|
update_tm(tm, now, 0); /* fill in date fields if needed */ |
|
753
|
0
|
|
|
|
|
|
n = tm->tm_mon - *num; |
|
754
|
0
|
|
|
|
|
|
*num = 0; |
|
755
|
0
|
0
|
|
|
|
|
while (n < 0) { |
|
756
|
0
|
|
|
|
|
|
n += 12; |
|
757
|
0
|
|
|
|
|
|
tm->tm_year--; |
|
758
|
|
|
|
|
|
|
} |
|
759
|
0
|
|
|
|
|
|
tm->tm_mon = n; |
|
760
|
0
|
|
|
|
|
|
*touched = 1; |
|
761
|
0
|
|
|
|
|
|
return end; |
|
762
|
|
|
|
|
|
|
} |
|
763
|
|
|
|
|
|
|
|
|
764
|
0
|
0
|
|
|
|
|
if (match_string(date, "years") >= 4) { |
|
765
|
0
|
|
|
|
|
|
update_tm(tm, now, 0); /* fill in date fields if needed */ |
|
766
|
0
|
|
|
|
|
|
tm->tm_year -= *num; |
|
767
|
0
|
|
|
|
|
|
*num = 0; |
|
768
|
0
|
|
|
|
|
|
*touched = 1; |
|
769
|
0
|
|
|
|
|
|
return end; |
|
770
|
|
|
|
|
|
|
} |
|
771
|
|
|
|
|
|
|
|
|
772
|
0
|
|
|
|
|
|
return end; |
|
773
|
|
|
|
|
|
|
} |
|
774
|
|
|
|
|
|
|
|
|
775
|
0
|
|
|
|
|
|
static const char *approxidate_digit(const char *date, struct tm *tm, int *num) |
|
776
|
|
|
|
|
|
|
{ |
|
777
|
|
|
|
|
|
|
char *end; |
|
778
|
0
|
|
|
|
|
|
unsigned long number = strtoul(date, &end, 10); |
|
779
|
|
|
|
|
|
|
|
|
780
|
0
|
0
|
|
|
|
|
switch (*end) { |
|
781
|
|
|
|
|
|
|
case ':': |
|
782
|
|
|
|
|
|
|
case '.': |
|
783
|
|
|
|
|
|
|
case '/': |
|
784
|
|
|
|
|
|
|
case '-': |
|
785
|
0
|
0
|
|
|
|
|
if (isdigit(end[1])) { |
|
786
|
0
|
|
|
|
|
|
size_t match = match_multi_number(number, *end, date, end, tm); |
|
787
|
0
|
0
|
|
|
|
|
if (match) |
|
788
|
0
|
|
|
|
|
|
return date + match; |
|
789
|
|
|
|
|
|
|
} |
|
790
|
|
|
|
|
|
|
} |
|
791
|
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
/* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */ |
|
793
|
0
|
0
|
|
|
|
|
if (date[0] != '0' || end - date <= 2) |
|
|
|
0
|
|
|
|
|
|
|
794
|
0
|
|
|
|
|
|
*num = number; |
|
795
|
0
|
|
|
|
|
|
return end; |
|
796
|
|
|
|
|
|
|
} |
|
797
|
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
/* |
|
799
|
|
|
|
|
|
|
* Do we have a pending number at the end, or when |
|
800
|
|
|
|
|
|
|
* we see a new one? Let's assume it's a month day, |
|
801
|
|
|
|
|
|
|
* as in "Dec 6, 1992" |
|
802
|
|
|
|
|
|
|
*/ |
|
803
|
0
|
|
|
|
|
|
static void pending_number(struct tm *tm, int *num) |
|
804
|
|
|
|
|
|
|
{ |
|
805
|
0
|
|
|
|
|
|
int number = *num; |
|
806
|
|
|
|
|
|
|
|
|
807
|
0
|
0
|
|
|
|
|
if (number) { |
|
808
|
0
|
|
|
|
|
|
*num = 0; |
|
809
|
0
|
0
|
|
|
|
|
if (tm->tm_mday < 0 && number < 32) |
|
|
|
0
|
|
|
|
|
|
|
810
|
0
|
|
|
|
|
|
tm->tm_mday = number; |
|
811
|
0
|
0
|
|
|
|
|
else if (tm->tm_mon < 0 && number < 13) |
|
|
|
0
|
|
|
|
|
|
|
812
|
0
|
|
|
|
|
|
tm->tm_mon = number-1; |
|
813
|
0
|
0
|
|
|
|
|
else if (tm->tm_year < 0) { |
|
814
|
0
|
0
|
|
|
|
|
if (number > 1969 && number < 2100) |
|
|
|
0
|
|
|
|
|
|
|
815
|
0
|
|
|
|
|
|
tm->tm_year = number - 1900; |
|
816
|
0
|
0
|
|
|
|
|
else if (number > 69 && number < 100) |
|
|
|
0
|
|
|
|
|
|
|
817
|
0
|
|
|
|
|
|
tm->tm_year = number; |
|
818
|
0
|
0
|
|
|
|
|
else if (number < 38) |
|
819
|
0
|
|
|
|
|
|
tm->tm_year = 100 + number; |
|
820
|
|
|
|
|
|
|
/* We mess up for number = 00 ? */ |
|
821
|
|
|
|
|
|
|
} |
|
822
|
|
|
|
|
|
|
} |
|
823
|
0
|
|
|
|
|
|
} |
|
824
|
|
|
|
|
|
|
|
|
825
|
0
|
|
|
|
|
|
static git_time_t approxidate_str(const char *date, |
|
826
|
|
|
|
|
|
|
time_t time_sec, |
|
827
|
|
|
|
|
|
|
int *error_ret) |
|
828
|
|
|
|
|
|
|
{ |
|
829
|
0
|
|
|
|
|
|
int number = 0; |
|
830
|
0
|
|
|
|
|
|
int touched = 0; |
|
831
|
0
|
|
|
|
|
|
struct tm tm = {0}, now; |
|
832
|
|
|
|
|
|
|
|
|
833
|
0
|
|
|
|
|
|
p_localtime_r(&time_sec, &tm); |
|
834
|
0
|
|
|
|
|
|
now = tm; |
|
835
|
|
|
|
|
|
|
|
|
836
|
0
|
|
|
|
|
|
tm.tm_year = -1; |
|
837
|
0
|
|
|
|
|
|
tm.tm_mon = -1; |
|
838
|
0
|
|
|
|
|
|
tm.tm_mday = -1; |
|
839
|
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
for (;;) { |
|
841
|
0
|
|
|
|
|
|
unsigned char c = *date; |
|
842
|
0
|
0
|
|
|
|
|
if (!c) |
|
843
|
0
|
|
|
|
|
|
break; |
|
844
|
0
|
|
|
|
|
|
date++; |
|
845
|
0
|
0
|
|
|
|
|
if (isdigit(c)) { |
|
846
|
0
|
|
|
|
|
|
pending_number(&tm, &number); |
|
847
|
0
|
|
|
|
|
|
date = approxidate_digit(date-1, &tm, &number); |
|
848
|
0
|
|
|
|
|
|
touched = 1; |
|
849
|
0
|
|
|
|
|
|
continue; |
|
850
|
|
|
|
|
|
|
} |
|
851
|
0
|
0
|
|
|
|
|
if (isalpha(c)) |
|
852
|
0
|
|
|
|
|
|
date = approxidate_alpha(date-1, &tm, &now, &number, &touched); |
|
853
|
0
|
|
|
|
|
|
} |
|
854
|
0
|
|
|
|
|
|
pending_number(&tm, &number); |
|
855
|
0
|
0
|
|
|
|
|
if (!touched) |
|
856
|
0
|
|
|
|
|
|
*error_ret = 1; |
|
857
|
0
|
|
|
|
|
|
return update_tm(&tm, &now, 0); |
|
858
|
|
|
|
|
|
|
} |
|
859
|
|
|
|
|
|
|
|
|
860
|
0
|
|
|
|
|
|
int git__date_parse(git_time_t *out, const char *date) |
|
861
|
|
|
|
|
|
|
{ |
|
862
|
|
|
|
|
|
|
time_t time_sec; |
|
863
|
|
|
|
|
|
|
git_time_t timestamp; |
|
864
|
0
|
|
|
|
|
|
int offset, error_ret=0; |
|
865
|
|
|
|
|
|
|
|
|
866
|
0
|
0
|
|
|
|
|
if (!parse_date_basic(date, ×tamp, &offset)) { |
|
867
|
0
|
|
|
|
|
|
*out = timestamp; |
|
868
|
0
|
|
|
|
|
|
return 0; |
|
869
|
|
|
|
|
|
|
} |
|
870
|
|
|
|
|
|
|
|
|
871
|
0
|
0
|
|
|
|
|
if (time(&time_sec) == -1) |
|
872
|
0
|
|
|
|
|
|
return -1; |
|
873
|
|
|
|
|
|
|
|
|
874
|
0
|
|
|
|
|
|
*out = approxidate_str(date, time_sec, &error_ret); |
|
875
|
0
|
|
|
|
|
|
return error_ret; |
|
876
|
|
|
|
|
|
|
} |
|
877
|
|
|
|
|
|
|
|
|
878
|
3
|
|
|
|
|
|
int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date) |
|
879
|
|
|
|
|
|
|
{ |
|
880
|
|
|
|
|
|
|
int written; |
|
881
|
|
|
|
|
|
|
struct tm gmt; |
|
882
|
|
|
|
|
|
|
time_t t; |
|
883
|
|
|
|
|
|
|
|
|
884
|
3
|
50
|
|
|
|
|
assert(out && date); |
|
|
|
50
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
|
|
886
|
3
|
|
|
|
|
|
t = (time_t) (date->time + date->offset * 60); |
|
887
|
|
|
|
|
|
|
|
|
888
|
3
|
50
|
|
|
|
|
if (p_gmtime_r (&t, &gmt) == NULL) |
|
889
|
0
|
|
|
|
|
|
return -1; |
|
890
|
|
|
|
|
|
|
|
|
891
|
15
|
|
|
|
|
|
written = p_snprintf(out, len, "%.3s, %u %.3s %.4u %02u:%02u:%02u %+03d%02d", |
|
892
|
3
|
|
|
|
|
|
weekday_names[gmt.tm_wday], |
|
893
|
|
|
|
|
|
|
gmt.tm_mday, |
|
894
|
3
|
|
|
|
|
|
month_names[gmt.tm_mon], |
|
895
|
3
|
|
|
|
|
|
gmt.tm_year + 1900, |
|
896
|
|
|
|
|
|
|
gmt.tm_hour, gmt.tm_min, gmt.tm_sec, |
|
897
|
6
|
|
|
|
|
|
date->offset / 60, date->offset % 60); |
|
898
|
|
|
|
|
|
|
|
|
899
|
3
|
50
|
|
|
|
|
if (written < 0 || (written > (int) len - 1)) |
|
|
|
50
|
|
|
|
|
|
|
900
|
0
|
|
|
|
|
|
return -1; |
|
901
|
|
|
|
|
|
|
|
|
902
|
3
|
|
|
|
|
|
return 0; |
|
903
|
|
|
|
|
|
|
} |
|
904
|
|
|
|
|
|
|
|