【问题标题】:How to convert the negative timestamp to a string. C++如何将负时间戳转换为字符串。 C++
【发布时间】:2012-06-28 17:23:47
【问题描述】:

我有负时间戳(即 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;
}

【问题讨论】:

  • 您是否需要考虑日历更改,顺便说一句,这些更改发生在不同国家/地区的不同时间?
  • 您是否尝试过使用通常用于正值的函数?我不知道它是否是标准行为,但在我的系统上,它们也适用于负数。
  • @artzub 你的Something like that. 指的是什么?我的评论?如果是我的,你还有什么其他数据?国名?
  • @artzub:我不知道timeinfo 是什么功能,但使用gmtime() 之类的功能会为我返回适当的年份。也许您可以向我们展示您正在做的一个独立的最小示例,人们可以获取、编译并亲自查看您所看到的?
  • 它与问题非常直接相关。在俄罗斯,公历于 1918 年被接受。东正教欧洲最后一个采用公历的国家是希腊(1923 年)。有一个good article on the Gregorian calendar on Wikipedia。因此,根据地点的不同,从过去的日期到现在所经过的秒数可能会有很大差异。

标签: c++ timestamp time-t


【解决方案1】:

由于您使用的是 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 解决方案,请使用我来自 this answer 的秒到日期转换器。

这个 sn-p 显示了如何使用它:

  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。

【讨论】:

  • 谢谢回答!我知道 -2326924800 不是 15.4.1869,例如这个日期。
【解决方案2】:

我建议使用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 ultimately a time_t typedef to?)

【讨论】:

  • 不,没有指定 :)。见stackoverflow.com/questions/471248/…
  • 我的意思是证明“ISO C 将 time_t 定义为算术类型,但没有指定任何特定类型、范围、分辨率或编码的引用。”是真的,但足够公平。
  • 天哪,time_t 不能接受负值真是太神奇了。因为很明显,在为time_t 定义规范时,对于那些制定规范的人来说,很明显没有程序员愿意进行日期小于 1970 年 1 月 1 日的计算。但当然。
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 2013-01-16
  • 1970-01-01
  • 2018-01-27
相关资源
最近更新 更多