Changeset View
Changeset View
Standalone View
Standalone View
source/lib/sysdep/os/win/wposix/wtime.cpp
/* Copyright (C) 2010 Wildfire Games. | /* Copyright (C) 2018 Wildfire Games. | ||||
* | * | ||||
* Permission is hereby granted, free of charge, to any person obtaining | * Permission is hereby granted, free of charge, to any person obtaining | ||||
* a copy of this software and associated documentation files (the | * a copy of this software and associated documentation files (the | ||||
* "Software"), to deal in the Software without restriction, including | * "Software"), to deal in the Software without restriction, including | ||||
* without limitation the rights to use, copy, modify, merge, publish, | * without limitation the rights to use, copy, modify, merge, publish, | ||||
* distribute, sublicense, and/or sell copies of the Software, and to | * distribute, sublicense, and/or sell copies of the Software, and to | ||||
* permit persons to whom the Software is furnished to do so, subject to | * permit persons to whom the Software is furnished to do so, subject to | ||||
* the following conditions: | * the following conditions: | ||||
▲ Show 20 Lines • Show All 200 Lines • ▼ Show 20 Lines | |||||
static const char *abb_weekdays[] = { | static const char *abb_weekdays[] = { | ||||
"Sun", | "Sun", | ||||
"Mon", | "Mon", | ||||
"Tue", | "Tue", | ||||
"Wed", | "Wed", | ||||
"Thu", | "Thu", | ||||
"Fri", | "Fri", | ||||
"Sat", | "Sat", | ||||
NULL | nullptr | ||||
}; | }; | ||||
static const char *full_weekdays[] = { | static const char *full_weekdays[] = { | ||||
"Sunday", | "Sunday", | ||||
"Monday", | "Monday", | ||||
"Tuesday", | "Tuesday", | ||||
"Wednesday", | "Wednesday", | ||||
"Thursday", | "Thursday", | ||||
"Friday", | "Friday", | ||||
"Saturday", | "Saturday", | ||||
NULL | nullptr | ||||
}; | }; | ||||
static const char *abb_month[] = { | static const char *abb_month[] = { | ||||
"Jan", | "Jan", | ||||
"Feb", | "Feb", | ||||
"Mar", | "Mar", | ||||
"Apr", | "Apr", | ||||
"May", | "May", | ||||
"Jun", | "Jun", | ||||
"Jul", | "Jul", | ||||
"Aug", | "Aug", | ||||
"Sep", | "Sep", | ||||
"Oct", | "Oct", | ||||
"Nov", | "Nov", | ||||
"Dec", | "Dec", | ||||
NULL | nullptr | ||||
}; | }; | ||||
static const char *full_month[] = { | static const char *full_month[] = { | ||||
"January", | "January", | ||||
"February", | "February", | ||||
"Mars", | "Mars", | ||||
"April", | "April", | ||||
"May", | "May", | ||||
"June", | "June", | ||||
"July", | "July", | ||||
"August", | "August", | ||||
"September", | "September", | ||||
"October", | "October", | ||||
"November", | "November", | ||||
"December", | "December", | ||||
NULL, | nullptr, | ||||
}; | }; | ||||
static const char *ampm[] = { | static const char *ampm[] = { | ||||
"am", | "am", | ||||
"pm", | "pm", | ||||
NULL | nullptr | ||||
}; | }; | ||||
/* | /* | ||||
* Try to match `*buf' to one of the strings in `strs'. Return the | * Try to match `*buf' to one of the strings in `strs'. Return the | ||||
* index of the matching string (or -1 if none). Also advance buf. | * index of the matching string (or -1 if none). Also advance buf. | ||||
*/ | */ | ||||
static int | static int | ||||
match_string (const char **buf, const char **strs) | match_string (const char **buf, const char **strs) | ||||
{ | { | ||||
int i = 0; | int i = 0; | ||||
for (i = 0; strs[i] != NULL; ++i) { | for (i = 0; strs[i] != nullptr; ++i) { | ||||
size_t len = strlen (strs[i]); | size_t len = strlen (strs[i]); | ||||
if (strncasecmp (*buf, strs[i], len) == 0) { | if (strncasecmp (*buf, strs[i], len) == 0) { | ||||
*buf += len; | *buf += len; | ||||
return i; | return i; | ||||
} | } | ||||
} | } | ||||
return -1; | return -1; | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | for (; (c = *format) != '\0'; ++format) { | ||||
} else if (c == '%' && format[1] != '\0') { | } else if (c == '%' && format[1] != '\0') { | ||||
c = *++format; | c = *++format; | ||||
if (c == 'E' || c == 'O') | if (c == 'E' || c == 'O') | ||||
c = *++format; | c = *++format; | ||||
switch (c) { | switch (c) { | ||||
case 'A' : | case 'A' : | ||||
ret = match_string (&buf, full_weekdays); | ret = match_string (&buf, full_weekdays); | ||||
if (ret < 0) | if (ret < 0) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_wday = ret; | timeptr->tm_wday = ret; | ||||
break; | break; | ||||
case 'a' : | case 'a' : | ||||
ret = match_string (&buf, abb_weekdays); | ret = match_string (&buf, abb_weekdays); | ||||
if (ret < 0) | if (ret < 0) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_wday = ret; | timeptr->tm_wday = ret; | ||||
break; | break; | ||||
case 'B' : | case 'B' : | ||||
ret = match_string (&buf, full_month); | ret = match_string (&buf, full_month); | ||||
if (ret < 0) | if (ret < 0) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_mon = ret; | timeptr->tm_mon = ret; | ||||
break; | break; | ||||
case 'b' : | case 'b' : | ||||
case 'h' : | case 'h' : | ||||
ret = match_string (&buf, abb_month); | ret = match_string (&buf, abb_month); | ||||
if (ret < 0) | if (ret < 0) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_mon = ret; | timeptr->tm_mon = ret; | ||||
break; | break; | ||||
case 'C' : | case 'C' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_year = (ret * 100) - tm_year_base; | timeptr->tm_year = (ret * 100) - tm_year_base; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'c' : | case 'c' : | ||||
abort (); | abort (); | ||||
case 'D' : /* %m/%d/%y */ | case 'D' : /* %m/%d/%y */ | ||||
s = strptime (buf, "%m/%d/%y", timeptr); | s = strptime (buf, "%m/%d/%y", timeptr); | ||||
if (s == NULL) | if (s == nullptr) | ||||
return NULL; | return nullptr; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'd' : | case 'd' : | ||||
case 'e' : | case 'e' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_mday = ret; | timeptr->tm_mday = ret; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'H' : | case 'H' : | ||||
case 'k' : | case 'k' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_hour = ret; | timeptr->tm_hour = ret; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'I' : | case 'I' : | ||||
case 'l' : | case 'l' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
if (ret == 12) | if (ret == 12) | ||||
timeptr->tm_hour = 0; | timeptr->tm_hour = 0; | ||||
else | else | ||||
timeptr->tm_hour = ret; | timeptr->tm_hour = ret; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'j' : | case 'j' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_yday = ret - 1; | timeptr->tm_yday = ret - 1; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'm' : | case 'm' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_mon = ret - 1; | timeptr->tm_mon = ret - 1; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'M' : | case 'M' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_min = ret; | timeptr->tm_min = ret; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'n' : | case 'n' : | ||||
if (*buf == '\n') | if (*buf == '\n') | ||||
++buf; | ++buf; | ||||
else | else | ||||
return NULL; | return nullptr; | ||||
break; | break; | ||||
case 'p' : | case 'p' : | ||||
ret = match_string (&buf, ampm); | ret = match_string (&buf, ampm); | ||||
if (ret < 0) | if (ret < 0) | ||||
return NULL; | return nullptr; | ||||
if (timeptr->tm_hour == 0) { | if (timeptr->tm_hour == 0) { | ||||
if (ret == 1) | if (ret == 1) | ||||
timeptr->tm_hour = 12; | timeptr->tm_hour = 12; | ||||
} else | } else | ||||
timeptr->tm_hour += 12; | timeptr->tm_hour += 12; | ||||
break; | break; | ||||
case 'r' : /* %I:%M:%S %p */ | case 'r' : /* %I:%M:%S %p */ | ||||
s = strptime (buf, "%I:%M:%S %p", timeptr); | s = strptime (buf, "%I:%M:%S %p", timeptr); | ||||
if (s == NULL) | if (s == nullptr) | ||||
return NULL; | return nullptr; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'R' : /* %H:%M */ | case 'R' : /* %H:%M */ | ||||
s = strptime (buf, "%H:%M", timeptr); | s = strptime (buf, "%H:%M", timeptr); | ||||
if (s == NULL) | if (s == nullptr) | ||||
return NULL; | return nullptr; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'S' : | case 'S' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_sec = ret; | timeptr->tm_sec = ret; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 't' : | case 't' : | ||||
if (*buf == '\t') | if (*buf == '\t') | ||||
++buf; | ++buf; | ||||
else | else | ||||
return NULL; | return nullptr; | ||||
break; | break; | ||||
case 'T' : /* %H:%M:%S */ | case 'T' : /* %H:%M:%S */ | ||||
case 'X' : | case 'X' : | ||||
s = strptime (buf, "%H:%M:%S", timeptr); | s = strptime (buf, "%H:%M:%S", timeptr); | ||||
if (s == NULL) | if (s == nullptr) | ||||
return NULL; | return nullptr; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'u' : | case 'u' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_wday = ret - 1; | timeptr->tm_wday = ret - 1; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'w' : | case 'w' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_wday = ret; | timeptr->tm_wday = ret; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'U' : | case 'U' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
set_week_number_sun (timeptr, ret); | set_week_number_sun (timeptr, ret); | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'V' : | case 'V' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
set_week_number_mon4 (timeptr, ret); | set_week_number_mon4 (timeptr, ret); | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'W' : | case 'W' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
set_week_number_mon (timeptr, ret); | set_week_number_mon (timeptr, ret); | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'x' : | case 'x' : | ||||
s = strptime (buf, "%Y:%m:%d", timeptr); | s = strptime (buf, "%Y:%m:%d", timeptr); | ||||
if (s == NULL) | if (s == nullptr) | ||||
return NULL; | return nullptr; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'y' : | case 'y' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
if (ret < 70) | if (ret < 70) | ||||
timeptr->tm_year = 100 + ret; | timeptr->tm_year = 100 + ret; | ||||
else | else | ||||
timeptr->tm_year = ret; | timeptr->tm_year = ret; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'Y' : | case 'Y' : | ||||
ret = strtol (buf, &s, 10); | ret = strtol (buf, &s, 10); | ||||
if (s == buf) | if (s == buf) | ||||
return NULL; | return nullptr; | ||||
timeptr->tm_year = ret - tm_year_base; | timeptr->tm_year = ret - tm_year_base; | ||||
buf = s; | buf = s; | ||||
break; | break; | ||||
case 'Z' : | case 'Z' : | ||||
abort (); | abort (); | ||||
case '\0' : | case '\0' : | ||||
--format; | --format; | ||||
/* FALLTHROUGH */ | /* FALLTHROUGH */ | ||||
case '%' : | case '%' : | ||||
if (*buf == '%') | if (*buf == '%') | ||||
++buf; | ++buf; | ||||
else | else | ||||
return NULL; | return nullptr; | ||||
break; | break; | ||||
default : | default : | ||||
if (*buf == '%' || *++buf == c) | if (*buf == '%' || *++buf == c) | ||||
++buf; | ++buf; | ||||
else | else | ||||
return NULL; | return nullptr; | ||||
break; | break; | ||||
} | } | ||||
} else { | } else { | ||||
if (*buf == c) | if (*buf == c) | ||||
++buf; | ++buf; | ||||
else | else | ||||
return NULL; | return nullptr; | ||||
} | } | ||||
} | } | ||||
return (char *)buf; | return (char *)buf; | ||||
} | } | ||||
//----------------------------------------------------------------------------- | //----------------------------------------------------------------------------- | ||||
static Status wtime_Init() | static Status wtime_Init() | ||||
{ | { | ||||
LatchInitialSystemTime(); | LatchInitialSystemTime(); | ||||
return INFO::OK; | return INFO::OK; | ||||
} | } |
Wildfire Games · Phabricator