-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathTime.ahk
160 lines (151 loc) · 5.06 KB
/
Time.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
; Link: https://autohotkey.com/board/topic/42668-time-count-days-hours-minutes-seconds-between-dates/
; Author: HotKeyIt
; Date:
; for: AHK_L
/* Time() - Count Days, hours, minutes, seconds between dates
; to and from can be in following format (from will be A_Now if empty):
; - 20090101...
; - 20090101000000
; - Apr-04-2009 or Apr-04 for current year
; - 01-01-2009 or 01-01 for current year
; Units (output units) can be d=days, h=hours, m=minutes, s=seconds
; params can be W,D,H,M,S
; - W = WeeksDay to include, e.g. W2-6 (mon-fri) or W1.7 (exclude weekends)
; - D = Day of Month to include, e.g. e D1-5 (to exclude D1.2.3)
; - B = Bank holiday to exclude, e.g. B0101.3112
; - H = Hours to include, e.g. H8-17 (to exclude use H0.1.2...)
; - M = Minutes to include, e.g. M0-30 (to exclude use M0.1.2...)
; - S = Seconds to include, e.g. S0-30 (to exclude use S0.1.2...)
#NoEnv
SetBatchLines,-1
MsgBox % "Count days from 12 May until 15 May`n"
. Time("May-15","12.May","d")
MsgBox % "Working hours from 01.05.2009 00:00:00 to 10.05.09 09:30:00`n"
. "Count only working hours from 09 to 17 and consider weekends and 01.May bank holiday`n"
. Time("20090504093000","20090501000000","h","W1.7 H9-17 B0105")
MsgBox % "Days to work till end of 2009`n" . Time("01.01.2010","","d","W1.7")
MsgBox % "Hours to work till end of 2009 from 09 - 17`n" . Time("01.01.2010","","h","W1.7 H9-17")
ExitApp
*/
Time(to,from="",units="d",params=""){
static _:="0000000000",s:=1,m:=60,h:=3600,d:=86400
,Jan:="01",Feb:="02",Mar:="03",Apr:="04",May:="05",Jun:="06",Jul:="07",Aug:="08",Sep:="09",Okt:=10,Nov:=11,Dec:=12
r:=0
units:=units ? %units% : 8640
If (InStr(to,"/") or InStr(to,"-") or InStr(to,".")){
Loop,Parse,to,/-.,%A_Space%
_%A_Index%:=RegExMatch(A_LoopField,"\d+") ? A_LoopField : %A_LoopField%
,_%A_Index%:=(StrLen(_%A_Index%)=1 ? "0" : "") . _%A_Index%
to:=SubStr(A_Now,1,8-StrLen(_1 . _2 . _3)) . _3 . (RegExMatch(SubStr(to,1,1),"\d") ? (_2 . _1) : (_1 . _2))
_1:="",_2:="",_3:=""
}
If (from and InStr(from,"/") or InStr(from,"-") or InStr(from,".")){
Loop,Parse,from,/-.,%A_Space%
_%A_Index%:=RegExMatch(A_LoopField,"\d+") ? A_LoopField : %A_LoopField%
,_%A_Index%:=(StrLen(_%A_Index%)=1 ? "0" : "") . _%A_Index%
from:=SubStr(A_Now,1,8-StrLen(_1 . _2 . _3)) . _3 . (RegExMatch(SubStr(from,1,1),"\d") ? (_2 . _1) : (_1 . _2))
}
count:=StrLen(to)<9 ? "days" : StrLen(to)<11 ? "hours" : StrLen(to)<13 ? "minutes" : "seconds"
to.=SubStr(_,1,14-StrLen(to)),(from ? from.=SubStr(_,1,14-StrLen(from)))
Loop,Parse,params,%A_Space%
If (unit:=SubStr(A_LoopField,1,1))
%unit%1:=InStr(A_LoopField,"-") ? SubStr(A_LoopField,2,InStr(A_LoopField,"-")-2) : ""
,%unit%2:=SubStr(A_LoopField,InStr(A_LoopField,"-") ? (InStr(A_LoopField,"-")+1) : 2)
count:=!params ? count : "seconds"
add:=!params ? 1 : (S2="" ? (M2="" ? (H2="" ? ((D2="" and B2="" and W="") ? d : h) : m) : s) : s)
While % (from<to){
FormatTime,year,%from%,YYYY
FormatTime,month,%from%,MM
FormatTime,day,%from%,dd
FormatTime,hour,%from%,H
FormatTime,minute,%from%,m
FormatTime,second,%from%,s
FormatTime,WDay,%from%,WDay
EnvAdd,from,%add%,%count%
If (W1 or W2){
If (W1=""){
If (W2=WDay or InStr(W2,"." . WDay) or InStr(W2,WDay . ".")){
Continue=1
}
} else If WDay not Between %W1% and %W2%
Continue=1
;else if (Wday=W2)
; Continue=1
If (Continue){
tempvar:=SubStr(from,1,8)
EnvAdd,tempvar,1,days
EnvSub,tempvar,%from%,seconds
EnvAdd,from,%tempvar% ,seconds
Continue=
continue
}
}
If (D1 or D2 or B2){
If (D1=""){
If (D2=day or B2=(day . month) or InStr(B2,"." . day . month) or InStr(B2,day . month . ".") or InStr(D2,"." . day) or InStr(D2,day . ".")){
Continue=1
}
} else If day not Between %D1% and %D2%
Continue=1
;else if (day=D2)
; Continue=1
If (Continue){
tempvar:=SubStr(from,1,8)
EnvAdd,tempvar,1,days
EnvSub,tempvar,%from%,seconds
EnvAdd,from,%tempvar% ,seconds
Continue=
continue
}
}
If (H1 or H2){
If (H1=""){
If (H2=hour or InStr(H2,hour . ".") or InStr(H2,"." hour)){
Continue=1
}
} else If hour not Between %H1% and %H2%
continue=1
;else if (hour=H2)
; continue=1
If (continue){
tempvar:=SubStr(from,1,10)
EnvAdd,tempvar,1,hours
EnvSub,tempvar,%from%,seconds
EnvAdd,from,%tempvar% ,seconds
continue=
continue
}
}
If (M1 or M2){
If (M1=""){
If (M2=minute or InStr(M2,minute . ".") or InStr(M2,"." minute)){
Continue=1
}
} else If minute not Between %M1% and %M2%
continue=1
;else if (minute=M2)
; continue=1
If (continue){
tempvar:=SubStr(from,1,12)
EnvAdd,tempvar,1,minutes
EnvSub,tempvar,%from%,seconds
EnvAdd,from,%tempvar% ,seconds
continue=
continue
}
}
If (S1 or S2){
If (S1=""){
If (S2=second or InStr(S2,second . ".") or InStr(S2,"." second)){
Continue
}
} else if (second!=S2)
If second not Between %S1% and %S2%
continue
}
r+=add
}
tempvar:=SubStr(count,1,1)
tempvar:=%tempvar%
Return (r*tempvar)/units
}