test scripts: refactor start_httpd helper
[git/ljr.git] / date.c
blobd75dff42405404ec86820a534620c1ba68cd9116
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include "cache.h"
9 /*
10 * This is like mktime, but without normalization of tm_wday and tm_yday.
12 time_t tm_to_time_t(const struct tm *tm)
14 static const int mdays[] = {
15 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
17 int year = tm->tm_year - 70;
18 int month = tm->tm_mon;
19 int day = tm->tm_mday;
21 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
22 return -1;
23 if (month < 0 || month > 11) /* array bounds */
24 return -1;
25 if (month < 2 || (year + 2) % 4)
26 day--;
27 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
28 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
31 static const char *month_names[] = {
32 "January", "February", "March", "April", "May", "June",
33 "July", "August", "September", "October", "November", "December"
36 static const char *weekday_names[] = {
37 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
40 static time_t gm_time_t(unsigned long time, int tz)
42 int minutes;
44 minutes = tz < 0 ? -tz : tz;
45 minutes = (minutes / 100)*60 + (minutes % 100);
46 minutes = tz < 0 ? -minutes : minutes;
47 return time + minutes * 60;
51 * The "tz" thing is passed in as this strange "decimal parse of tz"
52 * thing, which means that tz -0100 is passed in as the integer -100,
53 * even though it means "sixty minutes off"
55 static struct tm *time_to_tm(unsigned long time, int tz)
57 time_t t = gm_time_t(time, tz);
58 return gmtime(&t);
62 * What value of "tz" was in effect back then at "time" in the
63 * local timezone?
65 static int local_tzoffset(unsigned long time)
67 time_t t, t_local;
68 struct tm tm;
69 int offset, eastwest;
71 t = time;
72 localtime_r(&t, &tm);
73 t_local = tm_to_time_t(&tm);
75 if (t_local < t) {
76 eastwest = -1;
77 offset = t - t_local;
78 } else {
79 eastwest = 1;
80 offset = t_local - t;
82 offset /= 60; /* in minutes */
83 offset = (offset % 60) + ((offset / 60) * 100);
84 return offset * eastwest;
87 const char *show_date(unsigned long time, int tz, enum date_mode mode)
89 struct tm *tm;
90 static char timebuf[200];
92 if (mode == DATE_RAW) {
93 snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
94 return timebuf;
97 if (mode == DATE_RELATIVE) {
98 unsigned long diff;
99 struct timeval now;
100 gettimeofday(&now, NULL);
101 if (now.tv_sec < time)
102 return "in the future";
103 diff = now.tv_sec - time;
104 if (diff < 90) {
105 snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
106 return timebuf;
108 /* Turn it into minutes */
109 diff = (diff + 30) / 60;
110 if (diff < 90) {
111 snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
112 return timebuf;
114 /* Turn it into hours */
115 diff = (diff + 30) / 60;
116 if (diff < 36) {
117 snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
118 return timebuf;
120 /* We deal with number of days from here on */
121 diff = (diff + 12) / 24;
122 if (diff < 14) {
123 snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
124 return timebuf;
126 /* Say weeks for the past 10 weeks or so */
127 if (diff < 70) {
128 snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
129 return timebuf;
131 /* Say months for the past 12 months or so */
132 if (diff < 360) {
133 snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
134 return timebuf;
136 /* Else fall back on absolute format.. */
139 if (mode == DATE_LOCAL)
140 tz = local_tzoffset(time);
142 tm = time_to_tm(time, tz);
143 if (!tm)
144 return NULL;
145 if (mode == DATE_SHORT)
146 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
147 tm->tm_mon + 1, tm->tm_mday);
148 else if (mode == DATE_ISO8601)
149 sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
150 tm->tm_year + 1900,
151 tm->tm_mon + 1,
152 tm->tm_mday,
153 tm->tm_hour, tm->tm_min, tm->tm_sec,
154 tz);
155 else if (mode == DATE_RFC2822)
156 sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
157 weekday_names[tm->tm_wday], tm->tm_mday,
158 month_names[tm->tm_mon], tm->tm_year + 1900,
159 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
160 else
161 sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
162 weekday_names[tm->tm_wday],
163 month_names[tm->tm_mon],
164 tm->tm_mday,
165 tm->tm_hour, tm->tm_min, tm->tm_sec,
166 tm->tm_year + 1900,
167 (mode == DATE_LOCAL) ? 0 : ' ',
168 tz);
169 return timebuf;
173 * Check these. And note how it doesn't do the summer-time conversion.
175 * In my world, it's always summer, and things are probably a bit off
176 * in other ways too.
178 static const struct {
179 const char *name;
180 int offset;
181 int dst;
182 } timezone_names[] = {
183 { "IDLW", -12, 0, }, /* International Date Line West */
184 { "NT", -11, 0, }, /* Nome */
185 { "CAT", -10, 0, }, /* Central Alaska */
186 { "HST", -10, 0, }, /* Hawaii Standard */
187 { "HDT", -10, 1, }, /* Hawaii Daylight */
188 { "YST", -9, 0, }, /* Yukon Standard */
189 { "YDT", -9, 1, }, /* Yukon Daylight */
190 { "PST", -8, 0, }, /* Pacific Standard */
191 { "PDT", -8, 1, }, /* Pacific Daylight */
192 { "MST", -7, 0, }, /* Mountain Standard */
193 { "MDT", -7, 1, }, /* Mountain Daylight */
194 { "CST", -6, 0, }, /* Central Standard */
195 { "CDT", -6, 1, }, /* Central Daylight */
196 { "EST", -5, 0, }, /* Eastern Standard */
197 { "EDT", -5, 1, }, /* Eastern Daylight */
198 { "AST", -3, 0, }, /* Atlantic Standard */
199 { "ADT", -3, 1, }, /* Atlantic Daylight */
200 { "WAT", -1, 0, }, /* West Africa */
202 { "GMT", 0, 0, }, /* Greenwich Mean */
203 { "UTC", 0, 0, }, /* Universal (Coordinated) */
205 { "WET", 0, 0, }, /* Western European */
206 { "BST", 0, 1, }, /* British Summer */
207 { "CET", +1, 0, }, /* Central European */
208 { "MET", +1, 0, }, /* Middle European */
209 { "MEWT", +1, 0, }, /* Middle European Winter */
210 { "MEST", +1, 1, }, /* Middle European Summer */
211 { "CEST", +1, 1, }, /* Central European Summer */
212 { "MESZ", +1, 1, }, /* Middle European Summer */
213 { "FWT", +1, 0, }, /* French Winter */
214 { "FST", +1, 1, }, /* French Summer */
215 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
216 { "EEST", +2, 1, }, /* Eastern European Daylight */
217 { "WAST", +7, 0, }, /* West Australian Standard */
218 { "WADT", +7, 1, }, /* West Australian Daylight */
219 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
220 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
221 { "EAST", +10, 0, }, /* Eastern Australian Standard */
222 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
223 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
224 { "NZT", +12, 0, }, /* New Zealand */
225 { "NZST", +12, 0, }, /* New Zealand Standard */
226 { "NZDT", +12, 1, }, /* New Zealand Daylight */
227 { "IDLE", +12, 0, }, /* International Date Line East */
230 static int match_string(const char *date, const char *str)
232 int i = 0;
234 for (i = 0; *date; date++, str++, i++) {
235 if (*date == *str)
236 continue;
237 if (toupper(*date) == toupper(*str))
238 continue;
239 if (!isalnum(*date))
240 break;
241 return 0;
243 return i;
246 static int skip_alpha(const char *date)
248 int i = 0;
249 do {
250 i++;
251 } while (isalpha(date[i]));
252 return i;
256 * Parse month, weekday, or timezone name
258 static int match_alpha(const char *date, struct tm *tm, int *offset)
260 int i;
262 for (i = 0; i < 12; i++) {
263 int match = match_string(date, month_names[i]);
264 if (match >= 3) {
265 tm->tm_mon = i;
266 return match;
270 for (i = 0; i < 7; i++) {
271 int match = match_string(date, weekday_names[i]);
272 if (match >= 3) {
273 tm->tm_wday = i;
274 return match;
278 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
279 int match = match_string(date, timezone_names[i].name);
280 if (match >= 3) {
281 int off = timezone_names[i].offset;
283 /* This is bogus, but we like summer */
284 off += timezone_names[i].dst;
286 /* Only use the tz name offset if we don't have anything better */
287 if (*offset == -1)
288 *offset = 60*off;
290 return match;
294 if (match_string(date, "PM") == 2) {
295 tm->tm_hour = (tm->tm_hour % 12) + 12;
296 return 2;
299 if (match_string(date, "AM") == 2) {
300 tm->tm_hour = (tm->tm_hour % 12) + 0;
301 return 2;
304 /* BAD CRAP */
305 return skip_alpha(date);
308 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
310 if (month > 0 && month < 13 && day > 0 && day < 32) {
311 struct tm check = *tm;
312 struct tm *r = (now_tm ? &check : tm);
313 time_t specified;
315 r->tm_mon = month - 1;
316 r->tm_mday = day;
317 if (year == -1) {
318 if (!now_tm)
319 return 1;
320 r->tm_year = now_tm->tm_year;
322 else if (year >= 1970 && year < 2100)
323 r->tm_year = year - 1900;
324 else if (year > 70 && year < 100)
325 r->tm_year = year;
326 else if (year < 38)
327 r->tm_year = year + 100;
328 else
329 return 0;
330 if (!now_tm)
331 return 1;
333 specified = tm_to_time_t(r);
335 /* Be it commit time or author time, it does not make
336 * sense to specify timestamp way into the future. Make
337 * sure it is not later than ten days from now...
339 if (now + 10*24*3600 < specified)
340 return 0;
341 tm->tm_mon = r->tm_mon;
342 tm->tm_mday = r->tm_mday;
343 if (year != -1)
344 tm->tm_year = r->tm_year;
345 return 1;
347 return 0;
350 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
352 time_t now;
353 struct tm now_tm;
354 struct tm *refuse_future;
355 long num2, num3;
357 num2 = strtol(end+1, &end, 10);
358 num3 = -1;
359 if (*end == c && isdigit(end[1]))
360 num3 = strtol(end+1, &end, 10);
362 /* Time? Date? */
363 switch (c) {
364 case ':':
365 if (num3 < 0)
366 num3 = 0;
367 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
368 tm->tm_hour = num;
369 tm->tm_min = num2;
370 tm->tm_sec = num3;
371 break;
373 return 0;
375 case '-':
376 case '/':
377 case '.':
378 now = time(NULL);
379 refuse_future = NULL;
380 if (gmtime_r(&now, &now_tm))
381 refuse_future = &now_tm;
383 if (num > 70) {
384 /* yyyy-mm-dd? */
385 if (is_date(num, num2, num3, refuse_future, now, tm))
386 break;
387 /* yyyy-dd-mm? */
388 if (is_date(num, num3, num2, refuse_future, now, tm))
389 break;
391 /* Our eastern European friends say dd.mm.yy[yy]
392 * is the norm there, so giving precedence to
393 * mm/dd/yy[yy] form only when separator is not '.'
395 if (c != '.' &&
396 is_date(num3, num, num2, refuse_future, now, tm))
397 break;
398 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
399 if (is_date(num3, num2, num, refuse_future, now, tm))
400 break;
401 /* Funny European mm.dd.yy */
402 if (c == '.' &&
403 is_date(num3, num, num2, refuse_future, now, tm))
404 break;
405 return 0;
407 return end - date;
410 /* Have we filled in any part of the time/date yet? */
411 static inline int nodate(struct tm *tm)
413 return tm->tm_year < 0 &&
414 tm->tm_mon < 0 &&
415 tm->tm_mday < 0 &&
416 !(tm->tm_hour | tm->tm_min | tm->tm_sec);
420 * We've seen a digit. Time? Year? Date?
422 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
424 int n;
425 char *end;
426 unsigned long num;
428 num = strtoul(date, &end, 10);
431 * Seconds since 1970? We trigger on that for any numbers with
432 * more than 8 digits. This is because we don't want to rule out
433 * numbers like 20070606 as a YYYYMMDD date.
435 if (num >= 100000000 && nodate(tm)) {
436 time_t time = num;
437 if (gmtime_r(&time, tm)) {
438 *tm_gmt = 1;
439 return end - date;
444 * Check for special formats: num[-.:/]num[same]num
446 switch (*end) {
447 case ':':
448 case '.':
449 case '/':
450 case '-':
451 if (isdigit(end[1])) {
452 int match = match_multi_number(num, *end, date, end, tm);
453 if (match)
454 return match;
459 * None of the special formats? Try to guess what
460 * the number meant. We use the number of digits
461 * to make a more educated guess..
463 n = 0;
464 do {
465 n++;
466 } while (isdigit(date[n]));
468 /* Four-digit year or a timezone? */
469 if (n == 4) {
470 if (num <= 1400 && *offset == -1) {
471 unsigned int minutes = num % 100;
472 unsigned int hours = num / 100;
473 *offset = hours*60 + minutes;
474 } else if (num > 1900 && num < 2100)
475 tm->tm_year = num - 1900;
476 return n;
480 * Ignore lots of numerals. We took care of 4-digit years above.
481 * Days or months must be one or two digits.
483 if (n > 2)
484 return n;
487 * NOTE! We will give precedence to day-of-month over month or
488 * year numbers in the 1-12 range. So 05 is always "mday 5",
489 * unless we already have a mday..
491 * IOW, 01 Apr 05 parses as "April 1st, 2005".
493 if (num > 0 && num < 32 && tm->tm_mday < 0) {
494 tm->tm_mday = num;
495 return n;
498 /* Two-digit year? */
499 if (n == 2 && tm->tm_year < 0) {
500 if (num < 10 && tm->tm_mday >= 0) {
501 tm->tm_year = num + 100;
502 return n;
504 if (num >= 70) {
505 tm->tm_year = num;
506 return n;
510 if (num > 0 && num < 32) {
511 tm->tm_mday = num;
512 } else if (num > 0 && num < 13) {
513 tm->tm_mon = num-1;
516 return n;
519 static int match_tz(const char *date, int *offp)
521 char *end;
522 int offset = strtoul(date+1, &end, 10);
523 int min, hour;
524 int n = end - date - 1;
526 min = offset % 100;
527 hour = offset / 100;
530 * Don't accept any random crap.. At least 3 digits, and
531 * a valid minute. We might want to check that the minutes
532 * are divisible by 30 or something too.
534 if (min < 60 && n > 2) {
535 offset = hour*60+min;
536 if (*date == '-')
537 offset = -offset;
539 *offp = offset;
541 return end - date;
544 static int date_string(unsigned long date, int offset, char *buf, int len)
546 int sign = '+';
548 if (offset < 0) {
549 offset = -offset;
550 sign = '-';
552 return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
555 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
556 (i.e. English) day/month names, and it doesn't work correctly with %z. */
557 int parse_date(const char *date, char *result, int maxlen)
559 struct tm tm;
560 int offset, tm_gmt;
561 time_t then;
563 memset(&tm, 0, sizeof(tm));
564 tm.tm_year = -1;
565 tm.tm_mon = -1;
566 tm.tm_mday = -1;
567 tm.tm_isdst = -1;
568 offset = -1;
569 tm_gmt = 0;
571 for (;;) {
572 int match = 0;
573 unsigned char c = *date;
575 /* Stop at end of string or newline */
576 if (!c || c == '\n')
577 break;
579 if (isalpha(c))
580 match = match_alpha(date, &tm, &offset);
581 else if (isdigit(c))
582 match = match_digit(date, &tm, &offset, &tm_gmt);
583 else if ((c == '-' || c == '+') && isdigit(date[1]))
584 match = match_tz(date, &offset);
586 if (!match) {
587 /* BAD CRAP */
588 match = 1;
591 date += match;
594 /* mktime uses local timezone */
595 then = tm_to_time_t(&tm);
596 if (offset == -1)
597 offset = (then - mktime(&tm)) / 60;
599 if (then == -1)
600 return -1;
602 if (!tm_gmt)
603 then -= offset * 60;
604 return date_string(then, offset, result, maxlen);
607 enum date_mode parse_date_format(const char *format)
609 if (!strcmp(format, "relative"))
610 return DATE_RELATIVE;
611 else if (!strcmp(format, "iso8601") ||
612 !strcmp(format, "iso"))
613 return DATE_ISO8601;
614 else if (!strcmp(format, "rfc2822") ||
615 !strcmp(format, "rfc"))
616 return DATE_RFC2822;
617 else if (!strcmp(format, "short"))
618 return DATE_SHORT;
619 else if (!strcmp(format, "local"))
620 return DATE_LOCAL;
621 else if (!strcmp(format, "default"))
622 return DATE_NORMAL;
623 else if (!strcmp(format, "raw"))
624 return DATE_RAW;
625 else
626 die("unknown date format %s", format);
629 void datestamp(char *buf, int bufsize)
631 time_t now;
632 int offset;
634 time(&now);
636 offset = tm_to_time_t(localtime(&now)) - now;
637 offset /= 60;
639 date_string(now, offset, buf, bufsize);
642 static void update_tm(struct tm *tm, unsigned long sec)
644 time_t n = mktime(tm) - sec;
645 localtime_r(&n, tm);
648 static void date_yesterday(struct tm *tm, int *num)
650 update_tm(tm, 24*60*60);
653 static void date_time(struct tm *tm, int hour)
655 if (tm->tm_hour < hour)
656 date_yesterday(tm, NULL);
657 tm->tm_hour = hour;
658 tm->tm_min = 0;
659 tm->tm_sec = 0;
662 static void date_midnight(struct tm *tm, int *num)
664 date_time(tm, 0);
667 static void date_noon(struct tm *tm, int *num)
669 date_time(tm, 12);
672 static void date_tea(struct tm *tm, int *num)
674 date_time(tm, 17);
677 static void date_pm(struct tm *tm, int *num)
679 int hour, n = *num;
680 *num = 0;
682 hour = tm->tm_hour;
683 if (n) {
684 hour = n;
685 tm->tm_min = 0;
686 tm->tm_sec = 0;
688 tm->tm_hour = (hour % 12) + 12;
691 static void date_am(struct tm *tm, int *num)
693 int hour, n = *num;
694 *num = 0;
696 hour = tm->tm_hour;
697 if (n) {
698 hour = n;
699 tm->tm_min = 0;
700 tm->tm_sec = 0;
702 tm->tm_hour = (hour % 12);
705 static void date_never(struct tm *tm, int *num)
707 time_t n = 0;
708 localtime_r(&n, tm);
711 static const struct special {
712 const char *name;
713 void (*fn)(struct tm *, int *);
714 } special[] = {
715 { "yesterday", date_yesterday },
716 { "noon", date_noon },
717 { "midnight", date_midnight },
718 { "tea", date_tea },
719 { "PM", date_pm },
720 { "AM", date_am },
721 { "never", date_never },
722 { NULL }
725 static const char *number_name[] = {
726 "zero", "one", "two", "three", "four",
727 "five", "six", "seven", "eight", "nine", "ten",
730 static const struct typelen {
731 const char *type;
732 int length;
733 } typelen[] = {
734 { "seconds", 1 },
735 { "minutes", 60 },
736 { "hours", 60*60 },
737 { "days", 24*60*60 },
738 { "weeks", 7*24*60*60 },
739 { NULL }
742 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
744 const struct typelen *tl;
745 const struct special *s;
746 const char *end = date;
747 int i;
749 while (isalpha(*++end));
752 for (i = 0; i < 12; i++) {
753 int match = match_string(date, month_names[i]);
754 if (match >= 3) {
755 tm->tm_mon = i;
756 return end;
760 for (s = special; s->name; s++) {
761 int len = strlen(s->name);
762 if (match_string(date, s->name) == len) {
763 s->fn(tm, num);
764 return end;
768 if (!*num) {
769 for (i = 1; i < 11; i++) {
770 int len = strlen(number_name[i]);
771 if (match_string(date, number_name[i]) == len) {
772 *num = i;
773 return end;
776 if (match_string(date, "last") == 4)
777 *num = 1;
778 return end;
781 tl = typelen;
782 while (tl->type) {
783 int len = strlen(tl->type);
784 if (match_string(date, tl->type) >= len-1) {
785 update_tm(tm, tl->length * *num);
786 *num = 0;
787 return end;
789 tl++;
792 for (i = 0; i < 7; i++) {
793 int match = match_string(date, weekday_names[i]);
794 if (match >= 3) {
795 int diff, n = *num -1;
796 *num = 0;
798 diff = tm->tm_wday - i;
799 if (diff <= 0)
800 n++;
801 diff += 7*n;
803 update_tm(tm, diff * 24 * 60 * 60);
804 return end;
808 if (match_string(date, "months") >= 5) {
809 int n = tm->tm_mon - *num;
810 *num = 0;
811 while (n < 0) {
812 n += 12;
813 tm->tm_year--;
815 tm->tm_mon = n;
816 return end;
819 if (match_string(date, "years") >= 4) {
820 tm->tm_year -= *num;
821 *num = 0;
822 return end;
825 return end;
828 static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
830 char *end;
831 unsigned long number = strtoul(date, &end, 10);
833 switch (*end) {
834 case ':':
835 case '.':
836 case '/':
837 case '-':
838 if (isdigit(end[1])) {
839 int match = match_multi_number(number, *end, date, end, tm);
840 if (match)
841 return date + match;
845 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
846 if (date[0] != '0' || end - date <= 2)
847 *num = number;
848 return end;
851 unsigned long approxidate(const char *date)
853 int number = 0;
854 struct tm tm, now;
855 struct timeval tv;
856 char buffer[50];
858 if (parse_date(date, buffer, sizeof(buffer)) > 0)
859 return strtoul(buffer, NULL, 10);
861 gettimeofday(&tv, NULL);
862 localtime_r(&tv.tv_sec, &tm);
863 now = tm;
864 for (;;) {
865 unsigned char c = *date;
866 if (!c)
867 break;
868 date++;
869 if (isdigit(c)) {
870 date = approxidate_digit(date-1, &tm, &number);
871 continue;
873 if (isalpha(c))
874 date = approxidate_alpha(date-1, &tm, &number);
876 if (number > 0 && number < 32)
877 tm.tm_mday = number;
878 if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year)
879 tm.tm_year--;
880 return mktime(&tm);