I am trying to convert the code below from VB6 to C#. I am just not sure where to begin, I guess maybe because I am sucked way to much into VB6 and can't seem to take a step back.
Private Function GMT_OFFSET() As Integer
'****DECLARE VARIABLES
Dim dtNow As Date, dtEngland As Date
Dim dtGMT As SYSTEMTIME
'****GET SYSTEM TIME INFORMATION
Call GetSystemTime(dtGMT)
dtNow = Now
dtEngland = dtGMT.wYear & "-" & dtGMT.wMonth & "-" & dtGMT.wDay & " " & dtGMT.wHour & ":" & dtGMT.wMinute & ":" & dtGMT.wSecond
'****RETURN QUARTER HOURS
GMT_OFFSET = DateDiff("n", dtEngland, dtNow) \ 15
End Function
I know GetSystemTime is a Win32 API, obviously don't want to do PInvoke in .NET but rather use the pure objects in the .NET Framework.
Based on your code, you are looking for something along the lines of:
var dtNow = DateTime.Now;
var dtOffset = (dtNow.ToUniversalTime().Subtract(dtNow)).TotalMinutes / 15;
You may need to adjust the offset negatively if the universal time is greater than the current system time.
The system time:
DateTime systemDateTime = DateTime.Now;
But more importantly, the offset the system is running with:
TimeZoneInfo systemTimeZone = TimeZoneInfo.Local;
TimeSpan offset = systemTimeZone.GetUtcOffset();
Int32 offsetMinutes = offset.Hours * 60 + offset.Minutes;
Near direct translation:
//Private Function GMT_OFFSET() As Integer
int GMT_OFFSET()
{
//'****DECLARE VARIABLES
//Dim dtNow As Date, dtEngland As Date
DateTime dtNow, dtEngland;
//Dim dtGMT As SYSTEMTIME
//'****GET SYSTEM TIME INFORMATION
//Call GetSystemTime(dtGMT)
//dtNow = Now
//dtEngland = dtGMT.wYear & "-" & dtGMT.wMonth & "-" & dtGMT.wDay & " " & dtGMT.wHour & ":" & dtGMT.wMinute & ":" & dtGMT.wSecond
dtEngland = DateTime.UtcNow;
dtNow = dtEngland.ToLocalTime;
//'****RETURN QUARTER HOURS
//GMT_OFFSET = DateDiff("n", dtEngland, dtNow) \ 15
return Convert.ToInt32((dtNow - dtEngland).TotalMinutes) / 15;
//End Function
}
NB VB6's DateDiff(interval,date1,date2)
results in intervals based upon date2 - date1
, not vice versa.
But the one-liner is:
return Convert.ToInt32(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow).TotalMinutes) / 15;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.