Created
February 10, 2014 21:34
-
-
Save wrl/8924636 to your computer and use it in GitHub Desktop.
generating ISO 8601 dates on win32 vs on anything else
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* released under http://unlicense.org/ | |
*/ | |
#include <time.h> | |
#ifdef _WIN32 | |
#include <windows.h> | |
static void | |
gen_datetime(yajl_gen gen, time_t whence) | |
{ | |
int tz_bias, tz_hours, tz_minutes; | |
TIME_ZONE_INFORMATION tzi; | |
char buf[25]; | |
struct tm lt; | |
size_t len; | |
switch(GetTimeZoneInformation(&tzi)) { | |
case TIME_ZONE_ID_STANDARD: | |
tz_bias = tzi.Bias + tzi.StandardBias; | |
break; | |
case TIME_ZONE_ID_DAYLIGHT: | |
tz_bias = tzi.Bias + tzi.DaylightBias; | |
break; | |
default: | |
tz_bias = tzi.Bias; | |
break; | |
} | |
tz_bias = -tz_bias; | |
tz_hours = tz_bias / 60; | |
tz_minutes = tz_bias % 60; | |
localtime_s(<, &whence); | |
len = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", <); | |
if (len > sizeof(buf) - 6) { | |
yajl_gen_integer(gen, whence); | |
return; | |
} | |
buf[len++] = (tz_hours > 0) ? '+' : '-'; | |
sprintf_s(&buf[len], 4, "%02d%02d", abs(tz_hours), abs(tz_minutes)); | |
len += 4; | |
yajl_gen_string(gen, (uint8_t *) buf, len); | |
} | |
#else | |
static void | |
gen_datetime(yajl_gen gen, time_t whence) | |
{ | |
char buf[25]; | |
struct tm lt; | |
size_t len; | |
localtime_r(&whence, <); | |
len = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", <); | |
yajl_gen_string(gen, (uint8_t *) buf, len); | |
} | |
#endif |
but that's my fav feature of strftime, wai u tryna remove it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"but will," you say, "windows clearly supports strftime, why do you need all that extra bullshit?" my dear friend, it is chiefly the fault of the
%z
format spec. my format string (%z
included) looks like this on linux:and this on win32:
thanks microsoft