C++ 如何将负时间戳转换为字符串。C++;
我有负时间戳(即1970年以前的日期,例如1896年4月15日)。如何将给定的时间戳转换为正确的日期字符串 当我试着去做的时候C++ 如何将负时间戳转换为字符串。C++;,c++,timestamp,time-t,C++,Timestamp,Time T,我有负时间戳(即1970年以前的日期,例如1896年4月15日)。如何将给定的时间戳转换为正确的日期字符串 当我试着去做的时候 #include <ctime> #include <string> #include <iostream> int _tmain(int argc, _TCHAR* argv[]) { time_t t = std::atol("-2326924800"); struct tm * ptm; ptm =
#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;
}
#包括
#包括
#包括
int _tmain(int argc,_TCHAR*argv[]
{
时间t=std::atol(“-2326924800”);
struct tm*ptm;
ptm=gmtime(&t);
std::cout tm_年;
std::cin.get();
返回0;
}
我建议使用
std::cout因为看起来您正在使用Windows(我是从TCHAR推断出来的),所以您可能想要使用。自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;
}
粗略估计(1896-1970+3.5/12)*365.2425*24*3600得出-2326010337。所以,我们很好
编辑:
如果您希望DIY解决方案不涉及任何操作系统或编译器特定的功能,请使用my seconds to date converter from
此代码段显示了如何使用它:
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。您需要考虑日历的变化吗?顺便说一句,这些变化发生在不同的国家/地区的不同时间?您甚至尝试过使用您通常用于正值的函数吗?我不知道它是否是标准行为,但在我的系统中,它们也适用于负数。@artzub你的类似的东西是指什么。
?我的评论?如果是我的,你还有什么其他数据?国家名称?@artzub:我不知道timeinfo
是什么函数,但使用gmtime()
之类的函数可以为我返回正确的年份。也许你可以给我们展示一个你正在做的事情的独立的最小的例子,人们可以拿出来,编译,并亲眼看到你看到了什么?它与这个问题非常直接。在俄国,公历于1918年被接受。东正教欧洲最后一个采用公历的国家是希腊(1923年)。有一个。因此,根据地点的不同,从过去的日期到现在所经过的秒数可能会有很大的不同。不,没有指定:)。请看我的意思,这句话证明了“ISO C将时间定义为一种算术类型,但没有为它指定任何特定的类型、范围、分辨率或编码。”这是真的,但足够公平。天哪,time\u t
不能接受负值真是太神奇了。因为很明显,当定义时间的规范时,对于那些制定规范的人来说,显然没有程序员会希望在1970年1月1日之前进行计算。当然,谢谢你的回答!我知道-2326924800不是15.4.1869,比如这个日期。
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