1 /** @todo: Add Microsoft copyright */
8 #define TM_BASE_YEAR 1900
10 static const int ydays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
12 bool is_leap_year(int yy)
14 int yyyy = yy + TM_BASE_YEAR;
19 return yyyy % 400 == 0;
25 char * strptime(const char *buf, const char *fmt, struct tm *tm)
31 if ((fmt == NULL) || (tm == NULL))
36 //initialize the tm struct values
37 memset(tm, 0, sizeof(struct tm));
40 while (buf && (c = *fmt++))
54 case 'd': /* day of the month (1..31)*/
55 sscanf(buf, "%2d", &temp);
56 if (temp >= 1 && temp <= 31)
65 case 'H': /* hour (0..23) */
66 sscanf(buf, "%2d", &temp);
67 if (temp >= 0 && temp <= 23)
76 case 'M': /* minute (0..59) */
77 sscanf(buf, "%2d", &temp);
78 if (temp >= 0 && temp <= 59)
87 case 'm': /* month (1..12) */
88 sscanf(buf, "%2d", &temp);
89 if (temp >= 1 && temp <= 12)
91 tm->tm_mon = temp - 1;
98 case 'S': /* seconds (0..59) */
99 sscanf(buf, "%2d", &temp);
100 if (temp >= 0 && temp <= 59)
110 sscanf(buf, "%4d", &temp);
111 if (temp >= 0 && temp <= 9999)
113 tm->tm_year = temp - TM_BASE_YEAR;
131 //calculate tm_wday and tm_yday
132 tm->tm_yday = ydays[tm->tm_mon] + ((is_leap_year(tm->tm_year) && (tm->tm_mon >= 2)) ? 1 : 0) + tm->tm_mday - 1;
134 //1st Jan 1900 was Monday, hence weekday = the number of days from 1/1/1900 modulus 7 + 1
135 tm->tm_wday = (365 * tm->tm_year) + (tm->tm_year / 4) + tm->tm_yday + 1; //1st Jan 1900 was Monday, hence add 1
136 if (is_leap_year(tm->tm_year))