time reduced by 1in .NET when retrieved from C++ class [closed]
When trying to retieve an input datetime from C++ structure and use it in C# code, I noticed the hours are reduced by 1 and then converted to GMT+2 time zone in .NET there are the modules I use to read time from C++: time.inl and wchar.h
When calling standard C++ classes to retrieve the date and consume it in a .NET classe it happens that the hours are reduced by 1. it looks like C++ STANDARD date and time functions does convert the system date to some format, therefore the date is sent wrongly to .NEt class.开发者_开发百科 I'm talkig about the standatd time class in C++ and DateTime class in .NET there is no specific code written that's why I'm not providing any code.
I will try to make my question clearer and that is Has anyone come across this problem before. if Yes please share your findings if not then don't Thanks a lot for your help
I'm not clear from your question exactly what you are asking, but...it may be that you are observing a difference between time handling in Win32 versus .NET, specifically around "Daylight savings" shifts.
The issue is described in some detail here by Raymond Chen.
Check it, maybe it is what is troubling you.
I came up with this to adjust times when getting them via Win32 and using them in .NET:
// If I read a time from a file with GetLastWriteTime() (etc), I need
// to adjust it for display in the .NET environment.
internal static DateTime AdjustTime_Forward(DateTime time)
{
if (time.Kind == DateTimeKind.Utc) return time;
DateTime adjusted = time;
if (DateTime.Now.IsDaylightSavingTime() && !time.IsDaylightSavingTime())
adjusted = time + new System.TimeSpan(1, 0, 0);
else if (!DateTime.Now.IsDaylightSavingTime() && time.IsDaylightSavingTime())
adjusted = time - new System.TimeSpan(1, 0, 0);
return adjusted;
}
"Forward" in the name of the method refers to Win32 -> .NET, not "forward" in a temporal sense. It presumes the input time parameter was obtained from something like DateTime.FromFileTimeUtc()
.
This code assumes a shift of 1 hour for daylight savings. I'm not sure if that applies everywhere daylight savings is used.
精彩评论