北京时间校对

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

Moderators: tmplinshi, arcticir

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

北京时间校对

09 Apr 2018, 11:37

Code: Select all

; 以管理员身份运行
if !A_IsAdmin {
	Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
	ExitApp
}

if SynTime.DoIt()
	MsgBox, 64, 成功, 同步北京时间成功!
else
	MsgBox, 48, 失败, 同步失败!

; 同步北京时间。用法: SynTime.DoIt()
Class SynTime
{
	DoIt() { ; 成功返回 1
		return this.SetLocalTime( this.BeijingTime() )
	}

	SetLocalTime(YYYYMMDDHHMISS) {
		FormatTime, t, %YYYYMMDDHHMISS%, yyyy/M/1/d/H/m/s/0

		VarSetCapacity(SystemTime, 16, 0)
		Loop, Parse, t, /
			NumPut(A_LoopField, SystemTime, (A_Index-1)*2, "UShort")
		return DllCall("SetLocalTime", "Ptr", &SystemTime)
	}

	BeijingTime() { ; 返回 YYYYMMDDHHMISS
		whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
		whr.Open("HEAD", "http://baidu.com", true)
		whr.Send()
		whr.WaitForResponse()
		_date := whr.GetResponseHeader("Date") ; 示例数据: Mon, 21 Apr 2014 14:58:23 GMT

		arr := StrSplit( _date, [A_Space, ":"] )

		oMonth := {Jan:"01",Feb:"02",Mar:"03",Apr:"04",May:"05",Jun:"06",Jul:"07",Aug:"08",Sep:"09",Oct:"10",Nov:"11",Dec:"12"}
		Mon    := oMonth[ arr.3 ] ; 月份英文缩写转换成数字

		timestamp := arr.4 Mon arr.2 arr.5 arr.6 arr.7 ; YYYYMMDDHHMISS 格式的日期
		timestamp += 8, Hours ; 加 8 小时就是北京时间
		return timestamp
	}
}
Last edited by tmplinshi on 28 Aug 2018, 22:38, edited 1 time in total.
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: 北京时间校对

28 Aug 2018, 17:32

这个很好用,谢谢分享! :thumbup: :bravo:
garry
Posts: 3760
Joined: 22 Dec 2013, 12:50

Re: 北京时间校对

07 Sep 2018, 03:33

@tmplinshi
谢谢,我试过这个 歐洲 CET / CEST Central European Summer-Time

Code: Select all

;- this script :
;- https://autohotkey.com/boards/viewtopic.php?f=28&t=46997 ---

;- https://www.timeanddate.com/time/dst/
;- https://www.timeanddate.com/worldclock/
;- https://www.science.co.il/language/Locale-codes.php
;- 歐洲 CET / CEST Central European Summer-Time
;-----------------------------------------------------

#warn
setworkingdir,%a_scriptdir%

; 以管理员身份运行
if !A_IsAdmin {
	Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
	ExitApp
}

global dst

if SynTime.DoIt()
    msgbox, 262208,SUCCESS ,Succes synchronized %dst%
else
    msgbox, 262208,NO SUCCESS ,	NO Succes synchronized CET
return

/*
if SynTime.DoIt()
	MsgBox, 64, 成功 , 同步北京时间成功!
else
	MsgBox, 48, 失败 , 同步失败!
; 同步北京时间。用法: SynTime.DoIt()
*/


Class SynTime
{
	DoIt() { ; 成功返回 1
		return this.SetLocalTime( this.BeijingTime() )
	}
	SetLocalTime(YYYYMMDDHHMISS) {
		FormatTime, t, %YYYYMMDDHHMISS%, yyyy/M/1/d/H/m/s/0
		VarSetCapacity(SystemTime, 16, 0)
		Loop, Parse, t, /
			NumPut(A_LoopField, SystemTime, (A_Index-1)*2, "UShort")
		return DllCall("SetLocalTime", "Ptr", &SystemTime)
	}
	BeijingTime() { ; 返回 YYYYMMDDHHMISS
		whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
		whr.Open("HEAD", "http://baidu.com", true)
		whr.Send()
		whr.WaitForResponse()
		_date := whr.GetResponseHeader("Date") ; 示例数据: Mon, 21 Apr 2014 14:58:23 GMT
		arr := StrSplit( _date, [A_Space, ":"] )
		oMonth := {Jan:"01",Feb:"02",Mar:"03",Apr:"04",May:"05",Jun:"06",Jul:"07",Aug:"08",Sep:"09",Oct:"10",Nov:"11",Dec:"12"}
		Mon    := oMonth[ arr.3 ] ; 月份英文缩写转换成数字
		timestamp := arr.4 Mon arr.2 arr.5 arr.6 arr.7 ; YYYYMMDDHHMISS 格式的日期
		p2:=1                             ;- ( UTC + 1 / CET - Central European Time - begin last sunday in octobre / e.g  sun 2018-10-28 03:00>02:00 )
        stringmid,q,timestamp,1,8
        stringmid,Y,timestamp,1,4
        q:=(q)
        lsm := LastSunday(Y . "03")
        lso := LastSunday(Y . "10")
        if (q >= lsm) and (q < lso)
           {
		   DST:="CEST summertime UTC+2"
           p2:=(P2+1)                     ;- ( UTC + 2 / CEST - Central European SUMMER Time - DST begin last sunday in mars / e.g. sun 2018-03-25 02:00>03:00 )
           timestamp += +P2,hours
           }
        else
		   {
		   DST:="CET normaltime UTC+1"
           timestamp += +P2,hours
		   }
		;timestamp += 8, Hours ; 加 8 小时就是北京时间
		return timestamp
	}
}

;-----------------------------------------------------------------------
;- Function last sunday in mars and october for DST daylight saving time
;- https://autohotkey.com/board/topic/97664-find-specific-wday/ ---
LastSunday(Date)
    {
    Date += 31, D
    Date := SubStr(Date, 1, 6)
    Date += -1, Day
    FormatTime, WD , %Date%, WDay
    Date += -(WD - 1), D
    return, SubStr(Date, 1, 8)
   }
;============================================================================
Image

Code: Select all

;- modified= 20180910
;- created = 20180910
;- descript= show time e.g. 7-Countries / synchronize time / change time ( for test or if needed ( or if no internet-connection) )
;- DST     = only calculate  CET Central-European-Time

;- https://www.timeanddate.com/time/dst/
;- https://www.timeanddate.com/worldclock/
;- https://www.science.co.il/language/Locale-codes.php
;- https://autohotkey.com/boards/viewtopic.php?f=28&t=46997  

;             ( UTC + 1 / CET  - Central European        Time - begin last sunday in octobre / e.g  sun 2018-10-28 03:00>02:00 UTC 01:00)
;             ( UTC + 2 / CEST - Central European SUMMER Time - begin last sunday in mars    / e.g. sun 2018-03-25 02:00>03:00 UTC 01:00)  

;- https://en.wikipedia.org/wiki/Summer_Time_in_Europe
;- On 8  February 2018, the European Parliament voted to ask the European Commission to re-evaluate DST in Europe.
;- On 31 August   2018, the European Commission announced that they, after a web query giving high support of not switching clock twice annually, intend to go ahead with a new directive.  
/*
A formula which cn be used to calculate the beginning of European Summer Time is:
Sunday (31 − ((((5 × y) ÷ 4) + 4) mod 7)) March at 01:00 UTC
The corresponding formula for the end of European Summer Time is:
Sunday (31 − ((((5 × y) ÷ 4) + 1) mod 7)) October at 01:00 UTC
where y is the year, and a mod b is b times the fractional part of a/b. These formulae are valid until 2099
*/
  
       

#warn
#NoEnv
#singleinstance,force
SendMode Input
SetWorkingDir %A_ScriptDir%
SetBatchLines, -1
Fileencoding,UTF-8
Autotrim,off
SplitPath,a_scriptfullpath, name, dir, ext, name_no_ext, drive
name1=%name_no_ext%

gosub,createsecondscript
gosub,3rdscript

;- country , +/-hours , DST , Language
e4x=
(Ltrim Join`r`n
UTC_GMT    ,0,N,L2057
Portugal   ,0,Y,L2070
Switzerland,1,Y,L2055
Finland    ,2,Y,L1035
China      ,8,N,L2052
Vietnam    ,7,N,L1066
Montevideo ,-3,N,L11274
)
wa:=A_screenwidth
ha:=A_screenHeight
xx:=105
GW  :=(wa*50) /xx   
GH  :=(ha*33) /xx      
Info=%a_osversion% | %a_ostype% | 64bit=%a_is64bitos% | %wa%*%ha% | %a_username%=%a_isadmin% | %a_computername% | AHK=%a_ahkversion%
Gui,2:default
Gui,2: -DPIScale
SS_REALSIZECONTROL := 0x40
Gui,2:Color,Black
Gui,2:Color,ControlColor, Black
Gui,2:Font,s12 cYellow,Lucida Console
;A_GuiFont     := GuiDefaultFont()
;A_GuiFontSize := A_LastError
;aac:= a_screendpi   ;- 168 at 4k
;=================================================
sectA=Country,DATE       TIME,DaylightSavingTime DST,Language
stringsplit,k,sectA,`,
           columns:=k0   ;- 4
SectB=SYNC-TIME,ChangeTime,Worldclock,LanguageCode
stringsplit,b,sectB,`,
;------------------------------		   
rows:=9
all :=(rows*columns)     ;- 36
;------------------------------		   
d :=(wa*.3)/xx     ;-  difference horizontal
h :=(ha* 2)/xx     ;-  ROW-height
w :=(wa*11)/xx     ;- xROW-width
i:=0
r:=0
e:=0
q:=0
s:=3
col:=""
Loop,%all%
  {
  ;-----------------
  M:=mod(i,columns)
  if m=0             ;- skip to next line
     R++
  ;-----------------	 
  ;--- (Line-1) first line text 	 
  if (m=0 and R=1)
    {
	x :=(wa*.5)/xx
    y :=(ha* 1)/xx
    Gui,2:add,text,section x%x% y%y% w0 h0,
	}
  if (R=1)	
    Gui,2:Add,text,ys x+%d% h%h% w%w% ,% k%a_index%
	
  ;--- ( Line 2-8 )second line begin with EDIT ED1... ED28	
  if (m=0 and R>1 and R<9)
    {
	x :=(wa*.5)/xx
    y :=(ha* s)/xx
	s :=(s+3)      ;- add difference vertikal 
    Gui,2:add,text,section x%x% y%y% w0 h0,
	}
  if (R>1 and R<9)
    {
    e++	
	if (e=2 or e=6 or e=10 or e= 14 or e=18 or e=22 or e=26)
      col=Gray
	else
      col=D5E66D  ;-yellow	
	Gui,2:Add,Edit , ys  x+%d%  w%w% h%h% vED%e% c%col% readonly left,
	}
  
  ;------------ Line 9  ------------	 
  if (m=0 and R>8 and R<10)
    {
	x :=(wa*.5)/xx
    y :=(ha* s)/xx
	s :=(s+3) 
    Gui,2:add,text,section x%x% y%y% w0 h0,
	}
  if (R>8 and R<10)
    {
	q++
	;Gui,2:add,button, ys x+%d% w%w% h%h% gStart2,% b%q%
    Gui,2:Add,Progress,ys x+%d% w%w% h%h%  Disabled BackgroundGray
    if (q<2)
      Gui,2:Add,Text,     xp   yp  wp  hp   cYellow    BackgroundTrans Center 0x200 gStart2 ,% b%q%
    else
      Gui,2:Add,Text,     xp   yp  wp  hp   cBlack     BackgroundTrans Center 0x200 gStart2 ,% b%q%
	}
  i++
  }
;----------------  
y :=(ha*27   )/xx
x :=(wa*11.92)/xx   
Gui,2:Add,Edit ,x%x% y%y%  w%w% h%h% vTimex readonly left,
y :=(ha*30)/xx
x :=(wa* 1)/xx   
Gui,2:Add,text ,x%x% y%y%  cGray center,%info% 
  
Gui,2:show, x100 y5 w%gw% h%gh% ,%name1%
settimer,clock,1000
settimer,a0,1000
return
;--------------
2Guiclose:
exitapp
;--------------
Start2:
gui,2:submit,nohide
r:= a_guicontrol
goto,%r%
return
;--------------
changetime:
try
run,%f1%
return

sync-time:
try
run,%f31%
return


;--------------
dst:
run,https://www.timeanddate.com/time/dst/
return
;--------------
worldclock:
run,https://www.timeanddate.com/worldclock/
return
;--------------
languagecode:
run,https://www.science.co.il/language/Locale-codes.php
return
;--------------
clock:
GuiControl,2: ,timex, %A_YYYY%-%A_MM%-%A_DD% %a_hour%:%a_min%:%a_sec%
return
;--------------
a0:
;UTC       := GetTimestampUTC()
UTC:=a_nowutc
e=
Loop,parse,e4x,`n,`r
  {
  x:=a_loopfield
  if x=
     continue
  stringsplit,p,x,`,
  country:=p1
  p1:=utc
  if (p3="Y")
     {
     gosub,cc
     continue
     }
  else
     {
     p1 += +P2,hours
     FormatTime,px, %p1% %p4%,MMMM dddd
     FormatTime,p1, %p1% %p4%,yyyy-MM-dd HH:mm:ss
     e .= country . "," . p1 . ",NO DST," . px . "`r`n"
     continue
     }
  }
;msgbox,%e%
i=0
Loop,parse,e,`n,`r
   {
   stringsplit,d,a_loopfield,`,
   loop,4
      {
	  i++
      GuiControl,2:,ed%i%,% d%a_index%
	  }
   }
e=
return
;---------------------------
cc:
stringmid,q,p1,1,8
stringmid,Y,p1,1,4
q:=(q)
lsm := LastSunday(Y . "03")
lso := LastSunday(Y . "10")
if (q >= lsm) and (q < lso)
   {
   p2:=(P2+1)
   p1 += +P2,hours
   FormatTime,px, %p1% %p4%,MMMM dddd
   FormatTime,p1, %p1% %p4%,yyyy-MM-dd HH:mm:ss
   e .= country "," p1 ",-SummerTime," px "`r`n"
   }
else
   {
   p1 += +P2,hours
   FormatTime,px, %p1% %p4%,MMMM dddd
   FormatTime,p1, %p1% %p4%,yyyy-MM-dd HH:mm:ss
   e .= country "," p1 ",-WinterTime," px "`r`n"
   }
return
;--------------------------
;-----------------------------------------------------------------------
;- Function last sunday in mars and october for DST daylight saving time
;- https://autohotkey.com/board/topic/97664-find-specific-wday/ ---
LastSunday(Date)
    {
    Date += 31, D
    Date := SubStr(Date, 1, 6)
    Date += -1, Day
    FormatTime, WD , %Date%, WDay
    Date += -(WD - 1), D
    return, SubStr(Date, 1, 8)
   }
return
;============================================================================
createsecondscript:
f1=%a_scriptdir%\%name1%-change-time.ahk
IfNotExist,%f1%
{
e4x=
(LTrim Join`r`n % `
adm:=a_isadmin
if (adm=0)
Gosub, RunAsAdmin1
;-----------
stringmid,h,a_now,9,2
stringmid,m,a_now,11,2
stringmid,s,a_now,13,2
now:=h . "." . m . "." . s
;- Set time
runwait,%comspec% /k date /T&time /T&time
return
;--------------------------------------------------------------------
RunAsAdmin1:
  Run, *RunAs "%A_ScriptFullPath%"
Exitapp		
Return
;--------------------------------------------------------------------
RunAsAdmin2:
full_command_line := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
  try
  {
  if A_IsCompiled
    Run *RunAs "%A_ScriptFullPath%" /restart
      else
    Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
  }
ExitApp
}
return
)
fileappend,%e4x%,%f1%
}
return
;=================== end script DateandTime.com ===========================





;- function SYNCHRONIZE user tmplinshi
;- https://autohotkey.com/boards/viewtopic.php?f=28&t=46997
3rdscript:
f31=%a_scriptdir%\%name1%-SYNC-time.ahk
IfNotExist,%f31%
{
e5x=
(LTrim Join`r`n %
;-------- saved at Montag, 10. September 2018 22:24:45 --------------
;;-------- https://autohotkey.com/boards/viewtopic.php?f=28&t=46997 ---
;- this script :
;- https://autohotkey.com/boards/viewtopic.php?f=28&t=46997 ---
;- https://www.timeanddate.com/time/dst/
;- https://www.timeanddate.com/worldclock/
;- https://www.science.co.il/language/Locale-codes.php
;- 歐洲 CET / CEST Central European Summer-Time
;-----------------------------------------------------
#warn
setworkingdir,%a_scriptdir%
; 以管理员身份运行
if !A_IsAdmin {
	Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
	ExitApp
}
global dst
if SynTime.DoIt()
    msgbox, 262208,SUCCESS ,Succes synchronized %dst%,1
else
    msgbox, 262208,NO SUCCESS ,	NO Succes synchronized CET,1
return

/*
if SynTime.DoIt()
	MsgBox, 64, 成功 , 同步北京时间成功!
else
	MsgBox, 48, 失败 , 同步失败!
; 同步北京时间。用法: SynTime.DoIt()
return
*/

Class SynTime
{
	DoIt() { ; 成功返回 1
		return this.SetLocalTime( this.BeijingTime() )
	}
	SetLocalTime(YYYYMMDDHHMISS) {
		FormatTime, t, %YYYYMMDDHHMISS%, yyyy/M/1/d/H/m/s/0
		VarSetCapacity(SystemTime, 16, 0)
		Loop, Parse, t, /
			NumPut(A_LoopField, SystemTime, (A_Index-1)*2, "UShort")
		return DllCall("SetLocalTime", "Ptr", &SystemTime)
	}
	BeijingTime() { ; 返回 YYYYMMDDHHMISS
		whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
		whr.Open("HEAD", "http://baidu.com", true)
		whr.Send()
		whr.WaitForResponse()
		_date := whr.GetResponseHeader("Date") ; 示例数据: Mon, 21 Apr 2014 14:58:23 GMT
		arr := StrSplit( _date, [A_Space, ":"] )
		oMonth := {Jan:"01",Feb:"02",Mar:"03",Apr:"04",May:"05",Jun:"06",Jul:"07",Aug:"08",Sep:"09",Oct:"10",Nov:"11",Dec:"12"}
		Mon    := oMonth[ arr.3 ] ; 月份英文缩写转换成数字
		timestamp := arr.4 Mon arr.2 arr.5 arr.6 arr.7 ; YYYYMMDDHHMISS 格式的日期
		p2:=1                             ;- ( UTC + 1 / CET - Central European Time - begin last sunday in octobre / e.g  sun 2018-10-28 03:00>02:00 )
        stringmid,q,timestamp,1,8
        stringmid,Y,timestamp,1,4
        q:=(q)
        lsm := LastSunday(Y . "03")
        lso := LastSunday(Y . "10")
        if (q >= lsm) and (q < lso)
           {
		   DST:="CEST summertime UTC+2"
           p2:=(P2+1)                     ;- ( UTC + 2 / CEST - Central European SUMMER Time - DST begin last sunday in mars / e.g. sun 2018-03-25 02:00>03:00 )
           timestamp += +P2,hours
           }
        else
		   {
		   DST:="CET normaltime UTC+1"
           timestamp += +P2,hours
		   }
		;timestamp += 8, Hours ; 加 8 小时就是北京时间
		return timestamp
	}
}
;-----------------------------------------------------------------------
;- Function last sunday in mars and october for DST daylight saving time in Europe
;- https://autohotkey.com/board/topic/97664-find-specific-wday/ ---
LastSunday(Date)
    {
    Date += 31, D
    Date := SubStr(Date, 1, 6)
    Date += -1, Day
    FormatTime, WD , %Date%, WDay
    Date += -(WD - 1), D
    return, SubStr(Date, 1, 8)
   }
;============================================================================
)
fileappend,%e5x%,%f31%
}

return
;================== END script =========================================


Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 19 guests