3

我有负时间戳(即 1970 年以前的日期,例如 15.04.1896)。如何将给定的时间戳转换为正确的日期字符串。

正如我正在尝试做的那样

#include <ctime>
#include <string>
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
    time_t t = std::atol("-2326924800");
    struct tm * ptm;
    ptm = gmtime ( &t );

    std::cout << ptm->tm_year;
    std::cin.get();
    return 0;
}
4

2 回答 2

2

由于看起来您使用的是 Windows(我从 TCHAR 推断),您可能想要使用FileTimeToSystemTime(). 它适用于自 1601 年以来的数年。

例子:

#include <windows.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
  SYSTEMTIME sysTime;
  FILETIME fileTime;
  long long seconds;

  sysTime.wYear = 1896;
  sysTime.wMonth = 4;
  sysTime.wDayOfWeek = 0;
  sysTime.wDay = 15;
  sysTime.wHour = 0;
  sysTime.wMinute = 0;
  sysTime.wSecond = 0;
  sysTime.wMilliseconds = 0;

  if (SystemTimeToFileTime(&sysTime, &fileTime))
  {
    seconds = *(long long*)&fileTime;
    seconds /= 10000000; // 100-nanoseconds to seconds since Jan 1st 1601
    seconds -= 11644473600; // 1601 to 1970
    printf("%d.%d.%d is %lld seconds from Jan 1st 1970\n",
           sysTime.wDay,
           sysTime.wMonth,
           sysTime.wYear,
           seconds);
  }
  else
  {
    printf("SystemTimeToFileTime() failed with error 0x%X\n", GetLastError());
  }

  // Now, convert it back...

  seconds += 11644473600; // 1970 to 1601
  seconds *= 10000000; // seconds since Jan 1st 1601 to 100-nanoseconds
  *(long long*)&fileTime = seconds;

  memset(&sysTime, 0, sizeof(sysTime));

  if (FileTimeToSystemTime(&fileTime, &sysTime))
  {
    seconds /= 10000000; // 100-nanoseconds to seconds since Jan 1st 1601
    seconds -= 11644473600; // 1601 to 1970
    printf("%lld seconds from Jan 1st 1970 is %d.%d.%d\n",
           seconds,
           sysTime.wDay,
           sysTime.wMonth,
           sysTime.wYear);
  }
  else
  {
    printf("FileTimeToSystemTime() failed with error 0x%X\n", GetLastError());
  }

  return 0;
}

输出:

15.4.1896 is -2326147200 seconds from Jan 1st 1970
-2326147200 seconds from Jan 1st 1970 is 15.4.1896

粗略估计 (1896-1970+3.5/12)*365.2425*24*3600 给出 -2326010337。所以,我们很好。

编辑

如果您想要一个不涉及任何操作系统或编译器特定功能的 DIY 解决方案,请使用我的秒到日期转换器从这个答案

这个片段展示了如何使用它:

  struct tm t;
  SecondsSinceEpochToDateTime(&t, -2326924800LL);
  printf("-2326924800 is %d.%d.%d %d:%d:%d\n",
         t.tm_mday,
         t.tm_mon + 1,
         t.tm_year + 1900,
         t.tm_hour,
         t.tm_min,
         t.tm_sec);

这是它的输出:

-2326924800 is 6.4.1896 0:0:0

是的,-2326924800 不是公历的 15.4.1896。现在是 6.4。

于 2012-06-27T10:52:29.207 回答
1

我建议使用boost::posix_time

std::cout << boost::posix_time::to_simple_string(boost::posix_time::from_time_t(0) + boost::posix_time::seconds(-2326924800));

time_t不合适,因为它没有指定接受负值(请参阅What is finally a time_t typedef to?

于 2012-06-27T10:24:50.857 回答