Skip to content

Instantly share code, notes, and snippets.

@wrl
Created February 10, 2014 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrl/8924636 to your computer and use it in GitHub Desktop.
Save wrl/8924636 to your computer and use it in GitHub Desktop.
generating ISO 8601 dates on win32 vs on anything else
/**
* 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(&lt, &whence);
len = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &lt);
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, &lt);
len = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", &lt);
yajl_gen_string(gen, (uint8_t *) buf, len);
}
#endif
@wrl
Copy link
Author

wrl commented Feb 10, 2014

"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:

"2014-02-10T22:29:46+0100"

and this on win32:

"2014-02-10T21:47:04Central Europe Standard Time"

thanks microsoft

@zacharyvoase
Copy link

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