Skip to content

Instantly share code, notes, and snippets.

@t-mat
Created January 7, 2015 01:41
Show Gist options
  • Save t-mat/f93d63bf883538542244 to your computer and use it in GitHub Desktop.
Save t-mat/f93d63bf883538542244 to your computer and use it in GitHub Desktop.
WIN32 : Date/Time conversion and strftime
#include <windows.h>
#include <time.h>
#include <stdio.h>
#include <stdint.h>
// http://www.programmingforums.org/post45492.html
static const int64_t EPOCH_DIFF = 0x019DB1DED53E8000LL; // 116444736000000000 nsecs
static const int RATE_DIFF = 10000000; // 100 nsecs
time_t tmToUnixTime(const struct tm& stm) {
auto tm = stm;
return mktime(&tm);
}
struct tm unixTimeToTm(time_t unixTime) {
struct tm stm {};
gmtime_s(&stm, &unixTime);
return stm;
}
FILETIME unixTimeToWin32FileTime(time_t unixTime) {
auto t = (static_cast<int64_t>(unixTime) * RATE_DIFF) + EPOCH_DIFF;
return * reinterpret_cast<const FILETIME*>(&t);
}
time_t win32FileTimeToUnixTime(const FILETIME& win32FileTime) {
const auto ft = * reinterpret_cast<const int64_t*>(&win32FileTime);
auto t = (ft - EPOCH_DIFF) / RATE_DIFF;
return static_cast<time_t>(t);
}
SYSTEMTIME win32FileTimeToSystemTime(const FILETIME& win32FileTime) {
SYSTEMTIME st;
FileTimeToSystemTime(&win32FileTime, &st);
return st;
}
FILETIME win32SystemTimeToFileTime(const SYSTEMTIME& win32SystemTime) {
FILETIME ft;
SystemTimeToFileTime(&win32SystemTime, &ft);
return ft;
}
SYSTEMTIME win32SystemTimeToLocalTime(const SYSTEMTIME& win32SystemTime) {
SYSTEMTIME lt;
SystemTimeToTzSpecificLocalTime(nullptr, &win32SystemTime, &lt);
return lt;
}
SYSTEMTIME win32LocalTimeToSystemTime(const SYSTEMTIME& win32LocalTime) {
SYSTEMTIME st;
TzSpecificLocalTimeToSystemTime(nullptr, &win32LocalTime, &st);
return st;
}
SYSTEMTIME win32FileTimeToLocalTime(const FILETIME& win32FileTime) {
return win32SystemTimeToLocalTime(win32FileTimeToSystemTime(win32FileTime));
}
FILETIME win32LocalTimeToFileTime(const SYSTEMTIME& win32LocalTime) {
return win32SystemTimeToFileTime(win32LocalTimeToSystemTime(win32LocalTime));
}
SYSTEMTIME getSystemTime() {
SYSTEMTIME st;
GetSystemTime(&st);
return st;
}
SYSTEMTIME getLocalTime() {
SYSTEMTIME st;
GetLocalTime(&st);
return st;
}
int main() {
const auto lt = getLocalTime();
const auto ft = win32LocalTimeToFileTime(lt);
const auto ut = win32FileTimeToUnixTime(ft);
const auto tm = unixTimeToTm(ut);
static const char* fmts[][2] = {
{ "%a", "Abbreviated weekday name" },
{ "%A", "Full weekday name" },
{ "%b", "Abbreviated month name" },
{ "%B", "Full month name" },
{ "%c", "Date and time representation appropriate for locale" },
{ "%d", "Day of month as decimal number (01..31)" },
{ "%H", "Hour in 24-hour format (00..23)" },
{ "%I", "Hour in 12-hour format (01..12)" },
{ "%j", "Day of year as decimal number (001..366)" },
{ "%m", "Month as decimal number (01..12)" },
{ "%M", "Minute as decimal number (00..59)" },
{ "%p", "Current locale's A.M./P.M. indicator for 12-hour clock" },
{ "%S", "Second as decimal number (00..59)" },
{ "%U", "Week of year as decimal number, with Sunday as first day of week (00..53)" },
{ "%w", "Weekday as decimal number (0..6; Sunday is 0)" },
{ "%W", "Week of year as decimal number, with Monday as first day of week (00..53)" },
{ "%x", "Date representation for current locale" },
{ "%X", "Time representation for current locale" },
{ "%y", "Year without century, as decimal number (00..99)" },
{ "%Y", "Year with century, as decimal number" },
{ "%z", "Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown" },
{ "%Z", "Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown" },
{ "%%", "Percent sign" },
};
for(auto& f : fmts) {
const auto* fmt = f[0];
const auto* desc = f[1];
char buf[256];
strftime(buf, sizeof(buf), fmt, &tm);
printf("%s -> [%-17s] : %s\n", fmt, buf, desc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment