• C++-Code-Time Transfer-Windows FILETIME(1601) To 1970 UTC


    C++-Code-Time Transfer-Windows FILETIME(1601) To 1970 UTC

    November 28, 2020 4:56 PM

    最近遇到需要进行时间转换的实现。
    以下是从mongoose.c中截取的,与之后在网上找到的思路是一致的。

    Function

    double cs_time(void) {
      double now;
    #ifndef _WIN32
      struct timeval tv;
      if (gettimeofday(&tv, NULL /* tz */) != 0) return 0;
      now = (double) tv.tv_sec + (((double) tv.tv_usec) / 1000000.0);
    #else
      SYSTEMTIME sysnow;
      FILETIME ftime;
      GetLocalTime(&sysnow);
      SystemTimeToFileTime(&sysnow, &ftime);
      /*
       * 1. VC 6.0 doesn't support conversion uint64 -> double, so, using int64
       * This should not cause a problems in this (21th) century
       * 2. Windows FILETIME is a number of 100-nanosecond intervals since January
       * 1, 1601 while time_t is a number of _seconds_ since January 1, 1970 UTC,
       * thus, we need to convert to seconds and adjust amount (subtract 11644473600
       * seconds)
       */
      now = (double) (((int64_t) ftime.dwLowDateTime +
                       ((int64_t) ftime.dwHighDateTime << 32)) /
                      10000000.0) -
            11644473600;
    #endif /* _WIN32 */
      return now;
    }
    

    网上抄的代码

    转自Windows中的时间(SYSTEMTIME和FILETIME)

    #include <iostream>
    
    using namespace std;
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <windows.h>
    
    
    int main()
    {
        SYSTEMTIME      stLocal, stUTC, stUTC2;
        FILETIME        ftLocal, ftUTC, ft;
        ULARGE_INTEGER  uli;
    
        GetLocalTime(&stLocal);
        GetSystemTime(&stUTC);
        printf("Local System Time(YYYY-MM-DD HH:MM:SS): %d-%d-%d %d:%d:%d
    ", stLocal.wYear, stLocal.wMonth,
            stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
        printf("UTC System Time  (YYYY-MM-DD HH:MM:SS): %d-%d-%d %d:%d:%d
    ", stUTC.wYear, stUTC.wMonth,
            stUTC.wDay, stUTC.wHour, stUTC.wMinute, stUTC.wSecond);
    
        SystemTimeToFileTime(&stLocal, &ftLocal);
        uli.LowPart = ftLocal.dwLowDateTime;
        uli.HighPart = ftLocal.dwHighDateTime;
        printf("Local File Time: %llu
    ", uli.QuadPart);
    
        LocalFileTimeToFileTime(&ftLocal, &ftUTC);
        uli.LowPart = ftUTC.dwLowDateTime;
        uli.HighPart = ftUTC.dwHighDateTime;
        printf("UTC File Time: %llu
    ", uli.QuadPart);
    
        FileTimeToSystemTime(&ftUTC, &stUTC2);
        printf("UTC System Time2 (YYYY-MM-DD HH:MM:SS): %d-%d-%d %d:%d:%d
    ", stUTC2.wYear, stUTC2.wMonth,
            stUTC2.wDay, stUTC2.wHour, stUTC2.wMinute, stUTC2.wSecond);
    
        return EXIT_SUCCESS;
    }
    
    
  • 相关阅读:
    Basic4android v3.20 发布
    KbmMW 4.40.00 正式版发布
    Devexpress VCL Build v2013 vol 13.2.2 发布
    KbmMW 4.40.00 测试发布
    kbmMWtable for XE5 接近尾声
    使用delphi 开发多层应用(二十一)使用XE5 RESTClient 直接访问kbmmw 数据库
    为什么有些东西,反反复复总是学不会
    心灵沟通
    <转>离婚前夜悟出的三件事
    c++ socket 客户端库 socks5 客户端 RudeSocket™ Open Source C++ Socket Library
  • 原文地址:https://www.cnblogs.com/yongchao/p/14053180.html
Copyright © 2020-2023  润新知