-
Notifications
You must be signed in to change notification settings - Fork 0
/
unixTimeFull.bat
227 lines (183 loc) · 10.9 KB
/
unixTimeFull.bat
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
:: by JaCk | Release 05/29/2018 | https://github.com/1ijack/BatchMajeek/blob/master/unixTimeFull.bat | unixTimeFull.bat -- returns current time in unix time -- total elapsed seconds since January 1st, 1970
:::
::: The zlib/libpng License -- https://opensource.org/licenses/Zlib
:: Copyright (c) 2018 JaCk
::
:: This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
::
:: Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
::
:: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
::
:: 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
::
:: 3. This notice may not be removed or altered from any source distribution.
:::
::::::::::::::::::::::::::::::::::::::::
::
:: What is teh Unix Time?
:: The number of seconds that have passed since the beginning of 00:00:00 UTC Thursday, 1 January 1970. The second layer encodes that number as a sequence of bits or decimal digits.
:: [ source: https://en.wikipedia.org/wiki/Unix_time ] -- accessed on 04/24/2018
::
::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal DisableDelayedExpansion EnableExtensions & ( for /f "tokens=1 delims==" %%A in ('set utx_') do set "%%~A=" ) 2>nul 1>nul & goto :func_utx_main
::::::::::::::::::::::::::::::::::::::::
:: ::
:: User Settings ::
:: ::
::::::::::::::::::::::::::::::::::::::::
:func_utx_user_settings
rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rem -----------------------------------------
rem Boolean settings below, use the following values to enable/disable behavior:
rem to Enable, set to: true
rem to Disable, set to: false [or undefined]
rem -----------------------------------------
rem Note: When below setting is undefined/false, output is seconds; otherwise, when value is 'true', output is in milliseconds
set "utx_out_ms=false" rem Default: ; Description: utx_out_ms --- will display with ms upto 1000 ms
rem -----------------------------------------
rem Note: UTC timezone/DST/offset source binary; when both are false, script will not use either; when both true, uses wmic only
set "utx_tz_w32tm=false" rem Default: false; Description: utx_tz_w32tm --- will use the w32tm.exe to get timezone info
set "utx_tz_wmic=true" rem Default: true ; Description: utx_tz_wmic --- will use the wmic.exe to get timezone info
rem -----------------------------------------
rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rem -----------------------------------------
rem String settings below, use expected strings
rem to Enable, set to: <string>
rem to Disable, set to: [undefined]
rem -----------------------------------------
rem The following is to bypass asking the w32tm.exe/wmic for the timezone settings. Value should be in seconds (integer). When value is undefined, script will ask the OS for timezone configuration.
rem -- Example: UTC+7 hours (aka 420 min), value would be: 25200
rem -- Example: UTC+0 hours (aka 0 min), value would be: 0
rem -- Example: UTC-10 hours (aka 600 min), value would be: -36000
set "utx_utc_offset="
rem -----------------------------------------
rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
goto :eof
::::::::::::::::::::::::::::::::::::::::
:: ::
:: Main ::
:: ::
::::::::::::::::::::::::::::::::::::::::
:func_utx_main
call :func_utx_user_settings
call :func_utx_error_checks
call :func_utx_calc_date_secs "utx_time{unix}" "%date:* =%" "01/01/1970"
echo/%utx_time{unix}%
rem Env Cleanup
( for /f "tokens=1 delims==" %%A in ('set utx_') do set "%%~A="
endlocal & goto :eof
) 2>nul 1>nul
goto :eof
::::::::::::::::::::::::::::::::::::::::
:: ::
:: Functions ::
:: ::
::::::::::::::::::::::::::::::::::::::::
2>nul ( goto :eof || exit /b ) 1>nul
:: Usage :: func_utx_error_checks
rem ensure that dependencies are resolved
:func_utx_error_checks
rem boolean NEEDS to be defined as something
set "utx_out_ms=%utx_out_ms%false"
rem Making sure binary exists, hard-coded value takes priority
if defined utx_utc_offset (
set /a "utx_utc{shift}=utx_utc_offset+0"
) else if not exist "%SystemRoot%\System32\w32tm.exe" (
set "utx_utc{shift}=0"
) else if not exist "%SystemRoot%\System32\wbem\wmic.exe" (
set "utx_utc{shift}=0"
)
goto :eof
:: Usage :: func_utx_calc_time_secs_rounded returnVarSecs returnVarMS "optional-HH:MM:SS.MS"
rem calc time elapsed seconds and ms (today OR param2) -- w/a for octals by (except ms)
rem Note: DOES NOT overwrite returnVarSecs value. Function ADDs to returnVarSecs
rem Note: DOES overwrite returnVarMS value.
:func_utx_calc_time_secs_ms
if "%~2" equ "" goto :eof
if "%~1" equ "" goto :eof
rem choose between system-clock vs provided-time
set "utx_scest=%time%"
if "%~3" neq "" set "utx_scest=%~3"
rem calculate total seconds and round milliseconds
rem returnVar ghetto ms -- purposefully not checking/fixing octal numbers to ensure three digit MS number
set "%~2=%utx_scest:~-2%0"
rem returnVar seconds -> seconds minutes -> seconds hours -> seconds
set /a "%~1+=( 1%utx_scest:~-5,2% - 100 ) + (( 1%utx_scest:~3,2% - 100 ) * 60 ) + ( %utx_scest:~0,2% * 3600 )"
set "utx_scest="
goto :eof
:: Usage :: func_utx_get_timeShift_w32tm returnVar optional-[ "hours" | "minutes" | "seconds" | "milliseconds" ]
rem Returns the (seconds) time difference from the system's timezone and UTC0 (daylight saving and other adjustments also factored)
rem Note: Number of seconds can be positive OR negative
rem Note: Param2 is optional units of time returned: 'hours', 'minutes', 'seconds', 'milliseconds'
rem Note: Param2 default is 'minutes'
:func_utx_get_timeShift_w32tm
if "%~1" equ "" goto :eof
rem Get current time adjustments
for /f "tokens=1-8 delims=: " %%A in ('"%SystemRoot%\System32\w32tm.exe" /tz') do (
if /i "%%~A" equ "Bias" for /f "tokens=1 delims=min" %%g in ("%%~B") do set /a "%~1+=%%g"
if /i "%%~B" equ "Bias" for /f "tokens=1 delims=min" %%g in ("%%~C") do set /a "%~1+=%%g"
if /i "%%~C" equ "Bias" for /f "tokens=1 delims=min" %%g in ("%%~D") do set /a "%~1+=%%g"
if /i "%%~D" equ "Bias" for /f "tokens=1 delims=min" %%g in ("%%~E") do set /a "%~1+=%%g"
if /i "%%~E" equ "Bias" for /f "tokens=1 delims=min" %%g in ("%%~F") do set /a "%~1+=%%g"
if /i "%%~F" equ "Bias" for /f "tokens=1 delims=min" %%g in ("%%~G") do set /a "%~1+=%%g"
if /i "%%~G" equ "Bias" for /f "tokens=1 delims=min" %%g in ("%%~H") do set /a "%~1+=%%g"
)
if "%~2" equ "" goto :eof
if /i "%~2" equ "minutes" goto :eof
rem convert min -> ( seconds OR milliseconds )
if /i "%~2" equ "hours" set /a "%~1/=60"
if /i "%~2" equ "seconds" set /a "%~1*=60"
if /i "%~2" equ "secs" set /a "%~1*=60"
if /i "%~2" equ "milliseconds" set /a "%~1*=6000"
if /i "%~2" equ "ms" set /a "%~1*=6000"
goto :eof
:: Usage :: func_utx_get_timeShift_wmic returnVar optional-[ "hours" | "minutes" | "seconds" | "milliseconds" ]
rem Returns the (seconds) time difference from the system's timezone and UTC0 (daylight saving and other adjustments also factored)
rem Note: Number of seconds can be positive OR negative
rem Note: Param2 is optional units of time returned: 'hours', 'minutes', 'seconds', 'milliseconds'
rem Note: Param2 default is 'minutes'
:func_utx_get_timeShift_wmic
if "%~1" equ "" goto :eof
rem Get current time adjustments
for /f %%g in ('"%SystemRoot%\System32\wbem\WMIC.exe computersystem get currenttimezone 2>&1"') do if %%g1 lss 1 set "%~1+=%%g"
if "%~2" equ "" goto :eof
if /i "%~2" equ "minutes" goto :eof
rem convert min -> ( seconds OR milliseconds )
if /i "%~2" equ "hours" set /a "%~1/=60"
if /i "%~2" equ "seconds" set /a "%~1*=60"
if /i "%~2" equ "secs" set /a "%~1*=60"
if /i "%~2" equ "milliseconds" set /a "%~1*=6000"
if /i "%~2" equ "ms" set /a "%~1*=6000"
goto :eof
:: Usage :: func_utx_calc_time_secs_rounded returnVar "optional-DD/MM/YYYY" "optional-DD/MM/YYYY"
rem calc days+months+years elapsed seconds (today/010111970 OR today/param3 OR param2/param3)
rem Note: DOES NOT overwrite returnVar value. Function ADDs to returnVar
rem Function Notes:
rem when param2 is greater than param3, swap variables; compares YYYYMMDD
rem current time, timezone, DST and other adjustments
rem calculate all the leapyears between the two year ranges -- using modulus, adds '1' when leap-year -- highly modified from source: "http://www.robvanderwoude.com/datetiment.php#LeapYear"
rem Dates need to be defined as "MM/DD/YYYY" (strip DOW)
rem ghetto milliseconds ftw
:func_utx_calc_date_secs
if "%~1" equ "" goto :eof
if "%~2" equ "" (( call %~0 %1 "%date:* =%" %3 ) & goto :eof )
if "%~3" equ "" (( call %~0 %1 %2 "01/01/1970" ) & goto :eof )
set "utx_p{two}=%~2"
set "utx_p{tri}=%~3"
if %utx_p{two}:~-4%%utx_p{two}:~-10,2%%utx_p{two}:~-7,2% lss %utx_p{tri}:~-4%%utx_p{tri}:~-10,2%%utx_p{tri}:~-7,2% (
set "utx_p{two}=%utx_p{tri}%"
set "utx_p{tri}=%utx_p{two}%"
)
if defined utx_tz_wmic if exist "%SystemRoot%\System32\wbem\WMIC.exe" set "utx_tz_w32tm="
if not defined utx_utc{shift} (
if defined utx_tz_wmic call :func_utx_get_timeShift_wmic "utx_utc{shift}" "seconds"
if defined utx_tz_w32tm call :func_utx_get_timeShift_w32tm "utx_utc{shift}" "seconds"
)
call :func_utx_calc_time_secs_ms "utx_elapsed{secs}" "utx_elapsed{ms}" "%time%"
for /l %%A in (%utx_p{tri}:~-4%,1,%utx_p{two}:~-4%) do set /a "utx_leap{total}+=!(%%A %% 4 & %%A %% 100 & %%A %% 400)"
for /f "tokens=1-3,4-6 delims=-/" %%A in ("%utx_p{two}%/%utx_p{tri}%") do set /a "%~1=((((( 1%%~A - 1%%~D ) * 305 ) / 10 ) + ( 1%%~B - 1%%~E + %utx_leap{total}% ) + ( ( %%~C - %%~F ) * 365 ) ) * 86400 ) - 172800 + %utx_utc{shift}% + %utx_elapsed{secs}%"
if /i "%utx_out_ms:~0,1%" neq "t" goto :eof
if defined utx_elapsed{ms} call set "%~1=%%%~1%%%utx_elapsed{ms}%"
goto :eof