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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
but that's my fav feature of strftime, wai u tryna remove it