海洋調查經常使用UTC時間,而有些時候近岸工程使用的是北京時間,之間相差8個小時,需要轉換,下面是這兩個時間轉化的函數:
'UTC轉換爲北京時間 函數可直接調用
Function UTCToBeijing(UTCyear As Long, UTCmonth As Long, UTCday As Long, UTChour As Long) As String
Dim year As Long, month As Long, day As Long, hour As Long, lastday As Long, lastlastday As Long
year = 0: month = 0: day = 0: hour = 0
lastday = 0 ' 月的最後一天日期
lastlastday = 0 '上月的最後一天日期
year = UTCyear
month = UTCmonth
day = UTCday
hour = UTChour + 8 'UTC+8轉換爲北京時間
If (month = 1 Or month = 3 Or month = 5 Or month = 7 Or month = 8 Or month = 10 Or month = 12) Then
lastday = 31
If month = 3 Then
If year Mod 400 = 0 Or (year Mod 4 = 0 And year Mod 100 <> 0) Then '判斷是否爲閏年
lastlastday = 29 '閏年的2月爲29天,平年爲28天
Else
lastlastday = 28
End If
End If
If month = 8 Then
lastlastday = 31
End If
Else
If month = 4 Or month = 6 Or month = 9 Or month = 11 Then
lastday = 30
lastlastday = 31
Else
lastlastday = 31
If year Mod 400 = 0 Or (year Mod 4 = 0 And year Mod 100 <> 0) Then '閏年的2月爲29天,平年爲28天
lastday = 29
Else
lastday = 28
End If
End If
End If
If hour >= 24 Then '當算出的時大於或等於24:00時,應減去24:00,日期加一天
hour = hour - 24
day = day + 1
If day > lastday Then '當算出的日期大於該月最後一天時,應減去該月最後一天的日期,月份加上一個月
day = day - lastday
month = month + 1
If month > 12 Then '當算出的月份大於12,應減去12,年份加上1年
month = month - 12
year = year + 1
End If
End If
End If
UTCToBeijing = CStr(year) & " " & CStr(month) & " " & CStr(day) & " " & CStr(hour)
End Function
'UTC轉換爲北京時間 函數可直接調用
Function BeijingToUTC(UTCyear As Long, UTCmonth As Long, UTCday As Long, UTChour As Long) As String
Dim year As Long, month As Long, day As Long, hour As Long, lastday As Long, lastlastday As Long
year = 0: month = 0: day = 0: hour = 0
lastday = 0 ' 月的最後一天日期
lastlastday = 0 '上月的最後一天日期
year = UTCyear
month = UTCmonth
day = UTCday
hour = UTChour - 8 'Beijing-8轉換爲UTC時間
If hour < 0 Then '當算出的時小於0:00時,應加上24:00,日期減一天
hour = hour + 24
day = day - 1
If day = 0 Then '當算出的日期等於0時,月份減一個月,應加上上月最後一天的日期
month = month - 1
If month = 0 Then '當算出的月份等於0,應加上12,年份減去1年
month = month + 12
year = year - 1
End If
If (month = 1 Or month = 3 Or month = 5 Or month = 7 Or month = 8 Or month = 10 Or month = 12) Then
lastday = 31
If month = 3 Then
If year Mod 400 = 0 Or (year Mod 4 = 0 And year Mod 100 <> 0) Then '判斷是否爲閏年
lastlastday = 29 '閏年的2月爲29天,平年爲28天
Else
lastlastday = 28
End If
End If
If month = 8 Then
lastlastday = 31
End If
Else
If month = 4 Or month = 6 Or month = 9 Or month = 11 Then
lastday = 30
lastlastday = 31
Else
lastlastday = 31
If year Mod 400 = 0 Or (year Mod 4 = 0 And year Mod 100 <> 0) Then '閏年的2月爲29天,平年爲28天
lastday = 29
Else
lastday = 28
End If
End If
End If
day = day + lastday
End If
End If
BeijingToUTC = CStr(year) & " " & CStr(month) & " " & CStr(day) & " " & CStr(hour)
End Function