/
UnixTime.au3
172 lines (161 loc) · 6.38 KB
/
UnixTime.au3
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
161
162
163
164
165
166
167
168
169
170
171
172
#include-once
;===============================================================================
;
; AutoIt Version: 3.1.1++
; Language: English
; Description: Dll wrapper functions for dealing with Unix timestamps.
; Requirement(s): CrtDll.dll
; Notes: If CrtDll.dll is not available then functions will return false
; and set @error = 99.
;
;===============================================================================
;===============================================================================
;
; Description: _TimeGetStamp - Get current time as Unix timestamp value.
; Parameter(s): None
; Return Value(s): On Success - Returns Unix timestamp
; On Failure - Returns False, sets @error = 99
; Author(s): Saunders (rksaunders@gmail.com)
;
;===============================================================================
Func _TimeGetStamp()
Local $av_Time
$av_Time = DllCall('CrtDll.dll', 'long:cdecl', 'time', 'ptr', 0)
If @error Then
SetError(99)
Return False
EndIf
Return $av_Time[0]
EndFunc
;===============================================================================
;
; Description: _TimeMakeStamp - Create Unix timestamp from input values.
; Syntax: _TimeMakeStamp( [ second [, minute [, hour [, day [, month [, year [, isDST ]]]]]]] )
; Parameter(s): Second - Second for timestamp (0 - 59)
; Minute - Minute for timestamp (0 - 59)
; Hour - Hour for timestamp (0 - 23)
; Day - Day for timestamp (1 - 31)
; Month - Month for timestamp (1 - 12)
; Year - Year for timestamp (1970 - 2038)
; * All the above values default to the 'Default' keyword, where the current
; time/date value will be used.
; IsDST - Set to 1 during Daylight Saving Time (DST)
; - Set to 0 not during DST
; - Set to -1 if unknown, function will try to figure it out
; - Default is -1
; Return Value(s): On Success - Returns Unix timestamp
; On Failure - Parameter error, returns -1
; - Dll error, returns False, sets @error = 99
; Notes: The function will try and calculate dates for numbers outside of the
; usual range.
; For example: _TimeMakeStamp(0, 0, 0, 32, 1, 1995)
; 32nd day of January? Obviously that's not a valid date, but the function
; automatically calculates this to be February 1st. A date of 0 will return
; the last day of the previous month.
; Author(s): Saunders (rksaunders@gmail.com)
;
;===============================================================================
Func _TimeMakeStamp($i_Sec = Default, $i_Min = Default, $i_Hour = Default, $i_Day = Default, $i_Mon = Default, $i_Year = Default, $i_IsDST = -1)
Local $struct_Time, $ptr_Time, $av_Time
$struct_Time = DllStructCreate('uint;uint;uint;uint;uint;uint;uint;uint;uint')
Select
Case $i_Sec = Default
$i_Sec = @SEC
ContinueCase
Case $i_Min = Default
$i_Min = @MIN
ContinueCase
Case $i_Hour = Default
$i_Hour = @HOUR
ContinueCase
Case $i_Day = Default
$i_Day = @MDAY
ContinueCase
Case $i_IsDST = Default
$i_IsDST = -1
EndSelect
; The following is done because the mktime function demands
; that the month be in 0-11 (Jan = 0) format instead of 1-12.
Select
Case $i_Mon = Default
$i_Mon = (@MON - 1)
Case $i_Mon <> Default
$i_Mon -= 1
EndSelect
; The following is done because the mktime function expects the year in format
; (full year - 1900), thus 99 = 1999 and 100 = 2005. The function will try
; to figure out what year the user is trying to use. Thus if the function recieves
; 70, it's untouched, but if the user gives 1970, 1900 is subtracted automatically.
; Any year above 99 has 1900 automatically subtracted.
Select
Case $i_Year = Default
$i_Year = (@YEAR - 1900)
Case $i_Year < 70
$i_Year += 100
Case $i_Year > 99
$i_Year -= 1900
EndSelect
DllStructSetData($struct_Time, 1, $i_Sec)
DllStructSetData($struct_Time, 2, $i_Min)
DllStructSetData($struct_Time, 3, $i_Hour)
DllStructSetData($struct_Time, 4, $i_Day)
DllStructSetData($struct_Time, 5, $i_Mon)
DllStructSetData($struct_Time, 6, $i_Year)
DllStructSetData($struct_Time, 9, $i_IsDST)
$ptr_Time = DllStructGetPtr($struct_Time)
$av_Time = DllCall('CrtDll.dll', 'long', 'mktime', 'ptr', $ptr_Time)
If @error Then
SetError(99)
Return False
EndIf
Return $av_Time[0]
EndFunc
;===============================================================================
;
; Description: _StringFormatTime - Get a string representation of a timestamp
; according to the format string given to the function.
; Syntax: _StringFormatTime( "format" [, timestamp [, max length ]] )
; Parameter(s): Format String - A format string to convert the timestamp to.
; See notes for some of the values that can be
; used in this string.
; Timestamp - A timestamp to format, possibly returned from
; _TimeMakeStamp. If left empty, default, or less
; than 0, the current time is used. (default is -1)
; Max Length - Maximum length of the string to be returned.
; Default is 255.
; Return Value(s): On Success - Returns string formatted timestamp.
; On Failure - Returns False, sets @error = 99
; Requirement(s): _TimeGetStamp
; Notes: A few of the conversion specifiers for the Format String:
; %A - Full weekday name (Friday)
; %B - Full month name (July)
; %d - Day of the month (01-31)
; %H - Hour in 24hr format (00-23)
; %m - Month number (01-12)
; %M - Minute (00-59)
; %p - Ante meridiem or Post Meridiem ('AM' or 'PM')
; %S - Second (00-59)
; %w - Day of the week as a number (Sunday = 0)
; %Y - 4 digit year (1999)
; %% - Literal percent character
; For a full list (22 chars in all), see the documentation
; Author(s): Saunders (rksaunders@gmail.com)
;
;===============================================================================
Func _StringFormatTime($s_Format, $i_Timestamp = -1, $i_MaxLen = 255)
Local $struct_Time, $ptr_Time, $av_Time, $av_StrfTime
If $i_Timestamp = default OR $i_Timestamp < 0 Then
$i_Timestamp = _TimeGetStamp()
EndIf
$ptr_Time = DllCall('CrtDll.dll', 'ptr', 'localtime', 'long_ptr', $i_Timestamp)
If @error Then
SetError(99)
Return False
EndIf
$av_StrfTime = DllCall('CrtDll.dll', 'int', 'strftime', _
'str', '', _
'int', $i_MaxLen, _
'str', $s_Format, _
'ptr', $ptr_Time[0])
Return $av_StrfTime[1]
EndFunc