I want to decode windows 64bit hex value: little endian time in C++.
I have a value(=FB7E5D6A355DD201) but I don't know how decode this value in C++.
Please tell me a sample code or related site.
In unix systems date/time is often (but not always) recorded as a 32-bit integer indicating seconds past since Epoch (00:00:00, Jan 1, 1970). But for Windows 64-bit LE format, it means "x100 nanoseconds past since 00:00:00.000,000,0 Jan 1, 1601
". Your hexadecimal number is a raw binary integer, so it can be easily interpreted by scanf
.
Here's a piece of stuff that I found from one of my oldest C programs. You can optimize it and add time zone support by yourself.
#include <stdio.h>
int mDays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
const int daySec = 86400;
int yearDays(int y){
return 365 + (y%400==0 || (y%4==0 && y%100!=0));
// Pre-Caesar calendar doesn't work well, don't add it
}
void win_u64tod(unsigned long long utime){
int y=1601, d=0, mo=0, h=0, m=0, s=0, ms=0;
ms = utime % 10000000;
utime /= 10000000;
while (utime >= daySec*yearDays(y)){
utime -= daySec*yearDays(y);
y ++;
}
if (yearDays(y) == 366) mDays[1]=29;
d = utime / daySec;
utime %= daySec;
while (d >= mDays[mo]){
d -= mDays[mo];
mo ++;
}
h = utime/3600;
utime %= 3600;
m = utime/60;
s = utime%60;
printf("%4u-%2u-%2u %2u:%02u:%02u.%07u\n", y, 1+mo, 1+d, h, m, s, ms);
}
int main(){
union{
char c[8];
unsigned long long ull;
} a;
int i;
for (i = 0; i < 8; i ++){
scanf("%2x", a.c + i);
}
win_u64tod(a.ull);
}
Sample input: FB7E5D6A355DD201
Sample output: 2016-12-23 15:58:34.6327803
(Please note time zone is not added)
Reference: DATECONVERT
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.