从 HTTP 返回的头部信息中获取北京时间

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

从 HTTP 返回的头部信息中获取北京时间

21 Apr 2014, 11:38

原来获取北京标准时间可以这么容易...

先来看一个 http 请求返回的头部信息:
>curl -I baidu.com
HTTP/1.1 200 OK
Date: Mon, 21 Apr 2014 14:58:23 GMT
Server: Apache
Cache-Control: max-age=86400
Expires: Tue, 22 Apr 2014 14:58:23 GMT
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-4b4c7d90"
Accept-Ranges: bytes
Content-Length: 81
Connection: Keep-Alive
Content-Type: text/html
这个 GMT 时间加 8 小时就是北京时间了 :)

Code: Select all

MsgBox, % BeijingTime()

BeijingTime() { ; 返回 YYYYMMDDHHMISS
	
	; 获取 http 返回的头部信息
	Http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	Http.Open("HEAD", "http://baidu.com", True)
	Http.Send()
	Http.WaitForResponse(-1)
	_Date := Http.GetResponseHeader("Date")

	; 分割 _Date。格式示例:
	; 	Mon, 21 Apr 2014 14:58:23 GMT
	Obj    := StrSplit( _Date, [A_Space, ":"] )

	; 把英文月份转为两位数字
	Months := "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
	Mon    := SubStr(  "0" Ceil( Instr(Months, Obj.3) / 4 ), -1  )

	; YYYYMMDDHHMISS 格式的日期
	_Date  := Obj.4 Mon Obj.2 Obj.5 Obj.6 Obj.7

	; 加 8 小时就是北京时间
	_Date  += 8, Hours

	Return _Date
}
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: 从 HTTP 返回的头部信息中获取北京时间

21 Apr 2014, 15:09

thank you 謝謝 tmplinshi
another example from user justme

Code: Select all

;- http://www.autohotkey.com/board/topic/89516-iso-8601formatting-datetime/
;- from user just me
#NoEnv
SetBatchLines, -1

;- example-1 --
UTC       := GetTimestampUTC()
Beijing1  := GetTimestampUTC()
Beijing1  += 8, Hours

;- example-2 --
Beijing2  := % BeijingTime()

;- http://www.science.co.il/language/locale-codes.asp
FormatTime, China     , %Beijing1% L2052, dddd MMMM yyyy-MM-dd
FormatTime, Hong_Kong , %Beijing1% L3076, dddd MMMM yyyy-MM-dd
FormatTime, Macau     , %Beijing1% L5124, dddd MMMM yyyy-MM-dd
FormatTime, Singapore , %Beijing1% L4100, dddd MMMM yyyy-MM-dd
FormatTime, Taiwan    , %Beijing1% L1028, dddd MMMM yyyy-MM-dd
msgbox, 262208,Time China ,UTC      =%utc%`nChina-1=%Beijing1%`nChina-2=%Beijing2%`nChina=%china%`nHong_Kong=%Hong_Kong%`nMacao=%Macau%`nSingapore=%Singapore%`nTaiwan=%Taiwan%
ExitApp

;- example-1 just-me --
GetTimestampUTC() { ; http://msdn.microsoft.com/en-us/library/ms724390
   VarSetCapacity(ST, 16, 0) ; SYSTEMTIME structure
   DllCall("Kernel32.dll\GetSystemTime", "Ptr", &ST)
   Return NumGet(ST, 0, "UShort")                        ; year   : 4 digits until 10000
        . SubStr("0" . NumGet(ST,  2, "UShort"), -1)     ; month  : 2 digits forced
        . SubStr("0" . NumGet(ST,  6, "UShort"), -1)     ; day    : 2 digits forced
        . SubStr("0" . NumGet(ST,  8, "UShort"), -1)     ; hour   : 2 digits forced
        . SubStr("0" . NumGet(ST, 10, "UShort"), -1)     ; minute : 2 digits forced
        . SubStr("0" . NumGet(ST, 12, "UShort"), -1)     ; second : 2 digits forced
}

;- example-2 tmplinshi --
;-------- http://ahkscript.org/boards/viewtopic.php?f=28&t=3408 ---
BeijingTime() { ; ?? YYYYMMDDHHMISS
    Http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
    Http.Open("HEAD", "http://baidu.com", True)
    Http.Send()
    Http.WaitForResponse(-1)
    _Date := Http.GetResponseHeader("Date")

    ;   Mon, 21 Apr 2014 14:58:23 GMT
    Obj    := StrSplit( _Date, [A_Space, ":"] )
    Months := "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
    Mon    := SubStr(  "0" Ceil( Instr(Months, Obj.3) / 4 ), -1  )
    ; YYYYMMDDHHMISS
    _Date  := Obj.4 Mon Obj.2 Obj.5 Obj.6 Obj.7
    ; add 8
    _Date  += 8, Hours
    Return _Date
}
User avatar
amnesiac
Posts: 186
Joined: 22 Nov 2013, 03:08
Location: Egret Island, China
Contact:

Re: 从 HTTP 返回的头部信息中获取北京时间

21 Apr 2014, 18:32

还是实践出真知。
AutoHotkey 学习指南(Beauty of AutoHotkey)
I do not make codes, and only a porter of AutoHotkey: from official to Chinese, from other languages to AutoHotkey, and show AutoHotkey to ordinary users sometimes.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 从 HTTP 返回的头部信息中获取北京时间

22 Apr 2014, 02:36

@garry
I didn't know that FormatTime can convert local time. Thanks.
BTW, BeijingTime() is designed for getting the Standard Beijing Time, from Internet.
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: 从 HTTP 返回的头部信息中获取北京时间

22 Apr 2014, 06:36

I just copied script from user "just me" , was another idea to show UTC / GMT and then add 8 hours ( China )
Formattime example below shows date in different languages

Beijing / China +8 hours :
http://www.timeanddate.com/time/map/#!cities=33

Code: Select all

 
;-- http://www.science.co.il/language/locale-codes.asp
Time1 += -2, Days
Time2 += -1, Days
FormatTime, Suomi   , %time1% L1035, dddd MMMM yyyy-MM-dd
FormatTime, Portugal, %time2% L2070, dddd MMMM yyyy-MM-dd
FormatTime, HereNow ,              , dddd MMMM yyyy-MM-dd
msgbox,Finland=%Suomi%`nPortugal=%Portugal%`nHere Now=%hereNow%
return

which weekday

Code: Select all

x=20140301             ;- saturday
FormatTime,a,%x%,dddd
msgbox,%x%=%a%
return
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 从 HTTP 返回的头部信息中获取北京时间

22 Apr 2014, 09:38

Sorry, I think you've misunderstood me, because of my bad English. I actually wanted to say:
Thank you for the example, it let me knew the GMT Timestamp can be converted by using FormatTime.
I purchased two books to learn English a few days ago, now waiting for the books arrived... :P
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: 从 HTTP 返回的头部信息中获取北京时间

26 Apr 2014, 02:03

ni hao tmplinshi, an example with ActiveX , see running actual time and date in China ( UTC + 8 )

Code: Select all

modified=20140426

#NoEnv
SendMode,Input
SetWorkingDir,%A_ScriptDir%
SetTitleMatchMode 2
SetBatchLines, -1

filename1=China-CST-Time_UTC +8
f1=http://free.timeanddate.com/clock/i44gnm06/n33/tltw/fn6/fs16/fc9ff/tc000/ftb/bas2/bat1/bacfff/pa8/tt0/tw0/tm3/td2/th1/ta1
;- CST=ChinaStandardTime
;- DST=NO
;- http://www.timeanddate.com/worldclock/fullscreen.html?n=33#
;- http://www.timeanddate.com/worldclock/city.html?n=33
;- http://www.timeanddate.com/clocks/free.html?n=33

Gui,2: Color, teal
Gui,2:Font,  S10 CDefault , FixedSys
h1=40
w1=250
h2=60
w2=270

;Gui,2:Add,ActiveX, x10 y10 w%w1% h%h1% vWB1 , Shell.Explorer    ;- MS-IE
Gui,2:Add,ActiveX, x10 y10 w%w1% h%h1% vWB1 ,Mozilla.Browser    ;- FireFox
ComObjError(false)
WB1.Navigate(F1)
Gui,2: Show,x0 y0 w%w2% h%h2%,%filename1%
return
;-----------------------------------------------------

2Guiclose:
exitapp
;============ end script ===================================

Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 37 guests