Skip to content

Instantly share code, notes, and snippets.

@wrl
Created February 10, 2014 21:34
Show Gist options
  • 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
@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