我正在尝试将以下代码从VB6转换为C#。我只是不确定从哪里开始,我想也许是因为我很喜欢VB6并且似乎无法退后一步。
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
我知道GetSystemTime是一个Win32 API,显然不想在.NET中使用PInvoke,而是使用.NET Framework中的纯对象。
答案 0 :(得分:2)
根据您的代码,您正在寻找以下内容:
var dtNow = DateTime.Now;
var dtOffset = (dtNow.ToUniversalTime().Subtract(dtNow)).TotalMinutes / 15;
如果通用时间大于当前系统时间,则可能需要负偏移调整偏移量。
答案 1 :(得分:1)
系统时间:
DateTime systemDateTime = DateTime.Now;
但更重要的是,系统运行的偏移量为:
TimeZoneInfo systemTimeZone = TimeZoneInfo.Local;
TimeSpan offset = systemTimeZone.GetUtcOffset();
Int32 offsetMinutes = offset.Hours * 60 + offset.Minutes;
答案 2 :(得分:1)
近直接翻译:
//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 DateDiff(interval,date1,date2)
导致基于date2 - date1
的间隔,不反之亦然。
但是单行是:
return Convert.ToInt32(TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow).TotalMinutes) / 15;