| 发表于:2007-01-25 12:57:2611楼 得分:20 |
正好前几天遇到这个问题,用c编写了一个转换utc的程序,翻译成vb: private function utctodatetime(lngseconds as long) as string dim nyear as long, nmonth as long, nday as long, nhour as long, nmin as long, nsec as long ' ' '取得年份 nyear = 1970 do while (lngseconds > = getyearseconds(nyear)) lngseconds = lngseconds - getyearseconds(nyear) nyear = nyear + 1 loop ' ' '取得月份 nmonth = 1 do while (lngseconds > = getmonthseconds(nyear, nmonth)) lngseconds = lngseconds - getmonthseconds(nyear, nmonth) nmonth = nmonth + 1 loop nday = lngseconds \ (3600# * 24) + 1 lngseconds = lngseconds mod (3600# * 24) nhour = lngseconds \ (3600#) lngseconds = lngseconds mod 3600# nmin = lngseconds \ 60# lngseconds = lngseconds mod 60 nsec = lngseconds utctodatetime = cstr(nyear) & "- " & cstr(nmonth) & "- " & cstr(nday) & " " & cstr(nhour) & ": " & cstr(nmin) & ": " & cstr(nsec) utctodatetime = format(utctodatetime, "yyyy-mm-dd hh:nn:ss ") end function ' ' '将现在时刻转化成自1970-01-01 00:00:00经过的秒数 private function nowtoutc(ttime as date) as long dim nyear as long, nmonth as long dim lngsenconds as long lngsenconds = 0 for nyear = 1970 to year(ttime) - 1 lngsenconds = lngsenconds + getyearseconds(nyear) next for nmonth = 1 to month(ttime) - 1 lngsenconds = lngsenconds + getmonthseconds(nyear, nmonth) next lngsenconds = lngsenconds + 3600# * 24 * (day(ttime) - 1) lngsenconds = lngsenconds + 3600# * hour(ttime) lngsenconds = lngsenconds + 60 * minute(ttime) lngsenconds = lngsenconds + second(ttime) nowtoutc = lngsenconds end function private function getyearseconds(year as long) as long if year mod 400 = 0 or (year mod 4 = 0 and year mod 100 <> 0) then getyearseconds = 366# * 3600# * 24# else getyearseconds = 365# * 3600# * 24# end if end function private function getmonthseconds(year as long, month as long) as long select case month case 1, 3, 5, 7, 8, 10, 12 getmonthseconds = 3600# * 24 * 31 case 4, 6, 9, 11 getmonthseconds = 3600# * 24 * 30 case 2 if year mod 400 = 0 or (year mod 4 = 0 and year mod 100 <> 0) then getmonthseconds = 3600# * 24 * 29 else getmonthseconds = 3600# * 24 * 28 end if end select end function private sub command1_click() dim i as long dim t as date t = now i = nowtoutc(t) debug.print "now: "; t debug.print "utc: "; cstr(i) debug.print "time: "; utctodatetime(i) end sub 运行结果: now: 2007-1-25 12:55:19 utc: 1169729719 time:2007-01-25 12:55:19 | | |
|