在C#中实现与CTime.GetTime()方法相同的行为
我试图在C中实现在C#中实现与CTime.GetTime()方法相同的行为,c#,c++,datetime,C#,C++,Datetime,我试图在C中实现CTime::GetTime()方法的行为 下面是我的控制台应用程序的代码片段(用C++/CLI编写): int main(数组^args) { int-epochYear=1970; int epochMonth=1; int-epochDay=1; DateTime managedEpochDate=DateTime(epochYear、epochonth、epochDay); 国际年=2013年; 整月=2; 整日=13; 整小时=9; 整数分钟=49; 整数秒=46; /
CTime::GetTime()
方法的行为
下面是我的控制台应用程序的代码片段(用C++/CLI编写):
int main(数组^args)
{
int-epochYear=1970;
int epochMonth=1;
int-epochDay=1;
DateTime managedEpochDate=DateTime(epochYear、epochonth、epochDay);
国际年=2013年;
整月=2;
整日=13;
整小时=9;
整数分钟=49;
整数秒=46;
//日期时间/CTime->数字时间(时间64)
DateTime managedDateTime=日期时间(年、月、日、小时、分钟、秒);
CTime nativeDateTime=CTime(年、月、日、时、分、秒);
DWORD NATIVETIMEOSERIALIZE=nativeDateTime.GetTime();
UInt32 ManagedTimeSerialize=(managedDateTime-ManagedDepochDate)
.总秒数;
}
最后,我有以下不同的值:
- 国产化二甲基硅氧烷=1360745386
- ManagedTimeSerialize=1360748986
有谁能帮助我理解这种差异的原因吗?这种差异来自于您使用的将时间转换为UTC的事实: 此构造函数将适当转换为UTC 鉴于在这种情况下,承包商(和承包商)不知道时区:
Kind
属性初始化为DateTimeKind.Unspecified
DateTime
的定义也不考虑时区:
< > <代码>减法(DATETIME,DATETIME)< /C>方法在执行减法时不考虑“<代码>类型> <代码> > DATETIME//COD>值的<代码>类型/代码>属性的值。p>
要获得所需的结果,请适当设置CTime
构造函数的最后一个参数,例如:
CTime nativeDateTime = CTime(year, month, day, hour, minutes, seconds, 0);
要修改DateTime
用法,您需要:
int epochYear = 1970;
int epochMonth = 1;
int epochDay = 1;
DateTime managedEpochDateUtc
= new DateTime(epochYear, epochMonth, epochDay, 0, 0, 0, DateTimeKind.Utc);
int year = 2013;
int month = 2;
int day = 13;
int hour = 9;
int minutes = 49;
int seconds = 46;
DateTime managedDateTimeLocal
= new DateTime(year, month, day, hour, minutes, seconds, DateTimeKind.Local);
DateTime managedDateTimeUtc = managedDateTimeLocal.ToUniversalTime();
uint managedTimeToSerialize = (uint)(managedDateTimeUtc - managedEpochDateUtc)
.TotalSeconds;
或者,根据您的建议,可以使用而不是
DateTime
,这通常是推荐的,因为DateTime
不同之处在于您使用的是将时间转换为UTC:
此构造函数将适当转换为UTC
鉴于在这种情况下,承包商(和承包商)不知道时区:
Kind
属性初始化为DateTimeKind.Unspecified
DateTime
的定义也不考虑时区:
< > <代码>减法(DATETIME,DATETIME)< /C>方法在执行减法时不考虑“<代码>类型> <代码> > DATETIME//COD>值的<代码>类型/代码>属性的值。p>
要获得所需的结果,请适当设置CTime
构造函数的最后一个参数,例如:
CTime nativeDateTime = CTime(year, month, day, hour, minutes, seconds, 0);
要修改DateTime
用法,您需要:
int epochYear = 1970;
int epochMonth = 1;
int epochDay = 1;
DateTime managedEpochDateUtc
= new DateTime(epochYear, epochMonth, epochDay, 0, 0, 0, DateTimeKind.Utc);
int year = 2013;
int month = 2;
int day = 13;
int hour = 9;
int minutes = 49;
int seconds = 46;
DateTime managedDateTimeLocal
= new DateTime(year, month, day, hour, minutes, seconds, DateTimeKind.Local);
DateTime managedDateTimeUtc = managedDateTimeLocal.ToUniversalTime();
uint managedTimeToSerialize = (uint)(managedDateTimeUtc - managedEpochDateUtc)
.TotalSeconds;
或者,根据您的建议,可以使用而不是
DateTime
,这通常是推荐的,因为DateTime
的差值为3600秒,即1小时。这与您的时区有什么关系?CTime具有声明是否应考虑夏令时的功能-我猜DateTime会自动执行此操作-因此小时数为difference@benjymous实际上,在这种情况下,情况正好相反——请看我的答案。使用@Jon:my timezone is+1我的时区是3600秒,或者1小时。这与您的时区有什么关系?CTime具有声明是否应考虑夏令时的功能-我猜DateTime会自动执行此操作-因此小时数为difference@benjymous事实上,在这种情况下,情况正好相反——请看我的答案。使用@Jon:my timezone是+1