[英]DateTime to time_t produces Hour: 2 on c++ side
The below code snippet convert DateTime
to time_t
in c#. 下面的代码片段在c#中将
DateTime
转换为time_t
。 When in C++, i convert it back using localtime_s
I get tm_hour = 2
, why? 在C ++中,我使用
localtime_s
将其转换回,我得到tm_hour = 2
,为什么?
I convert: DateTimeUtils.Secs(DateTime.Parse("2015-05-01"))
public static Int64 Secs(DateTime dt)
{
var delta = dt - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64(delta.TotalSeconds);
}
time_t DropTimeFromDate(time_t time)
{
struct tm timeInfo;
localtime_s(&timeInfo, &time);
timeInfo.tm_hour = 0;
timeInfo.tm_min = 0;
timeInfo.tm_sec = 0;
return mktime(&timeInfo);
}
Should I somehow fix the c# conversion? 我应该以某种方式解决c#转换吗?
I'm in the UK, presently UTC+1:00 (British Summer Time), running your code I get a tm_hour of 1 rather than 2 which I can only guess is due to us being in different timezones (UTC+1 isn't always UTC+1, timezones are just a headache in general) 我在英国,目前在UTC + 1:00(英国夏令时),运行您的代码,我得到的tm_hour为1而不是2,我只能猜测是由于我们处于不同的时区(UTC + 1是“ t始终为UTC + 1,时区通常令人头疼)
Either way the problem down to the C++ or C code you're using to convert back to time_t - the C# code is fine 无论哪种方式,问题都可以归结为您用来转换回time_t的C ++或C代码-C#代码很好
if you change localtime_s(&timeInfo, time) to gmtime_s(&timeInfo, time) 如果将localtime_s(&timeInfo,time)更改为gmtime_s(&timeInfo,time)
you should have better luck, the latter ignores the current timezone and just treats the value as a raw UTC time which is best to work with if you can 您应该有更好的运气,后者会忽略当前时区,而只是将值视为原始UTC时间,如果可以的话,最好与之配合使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.