Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
DOSuser
- Posts: 4
- Joined: 25 Sep 2023 17:35
#1
Post
by DOSuser » 29 Sep 2023 12:38
To get an environment variable set to the linux version of epoch time (seconds since 01/01/1970)
create two files , the first is named toEpoch.vbs and contains
Code: Select all
set dateTime= CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate (now())
'WScript.Echo DateDiff("s", "01/01/1970 00:00:00", Now())
WScript.Echo DateDiff("s", "01/01/1970 00:00:00", dateTime.GetVarDate (false))
the second is a batch file named epoch.bat which is:
Code: Select all
@echo off
rem set /A tzoffset=0 'timezone offset
rem set /A tzoffset=%tzoffset%*60*60
for /f "delims=" %%x in ('cscript /nologo toEpoch.vbs') do set epoch=%%x
rem set /A epoch =%epoch% + %tzoffset%
echo %epoch%
the commented lines can be uncommented and used for UTC offset if the datetime variable in toEpoch.vbs does not work for you
For a timer environment variable which has hundreds of seconds see the timesec.bat batch file (does not require any vbs code)
-
Batcher
- Posts: 74
- Joined: 16 Apr 2009 10:36
#2
Post
by Batcher » 30 Sep 2023 19:19
test-1.bat
Code: Select all
@echo off
for /f %%i in ('powershell "[int](Get-Date -UFormat %%s)"') do (
set "EpochSecond=%%i"
)
echo,%EpochSecond%
pause
-
DOSuser
- Posts: 4
- Joined: 25 Sep 2023 17:35
#3
Post
by DOSuser » 01 Oct 2023 08:55
A third version which only uses DOS batch commands - no vbscript and no power shell
: epochbat ep timezone_offset_hours -- epoch seconds since 01/01/1970
: -- ep [out,opt] - epoch seconds
: -- timezone_offset in hours (eastern us standard time is 4)
@echo off
SETLOCAL
set /A tzoffset=4 & rem timezone offset
if %1.==/?. GOTO SHOW_HELP
if %1.==-?. GOTO SHOW_HELP
if "%~2" NEQ "" (SET /A tzoffset= %2)
set /A tzoffset=%tzoffset%*60*60
set THETIME=%TIME%
rem Change fraction part for non-English localizations
set THETIME=%THETIME:,=.%
rem
set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a epochd=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4-2440588
rem Convert time to 100's of seconds
for /F "tokens=1-4 delims=:.," %%a in ("%THETIME%") do (
set /A "ep=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
set /A ep =%ep%/100 + %tzoffset% + %epochd%*24*60*60
ENDLOCAL & if "%~1" NEQ "" (SET %~1=%ep%) ELSE (echo.%ep%)
GOTO END
:SHOW_HELP
echo sets an environment variable to epoch seconds
echo example call epochbat ep 4 sets environment variable ep to epoch seconds with
echo timezone offset of 4 hours (eastern us standard time)
:END
EXIT /b
-
Batcher
- Posts: 74
- Joined: 16 Apr 2009 10:36
#4
Post
by Batcher » 01 Oct 2023 19:33
%date% or date /t rely on HKEY_CURRENT_USER\Control Panel\International\sShortDate
test-2.bat
Code: Select all
@echo off
for /f "tokens=2 delims==" %%i in ('wmic Path Win32_OperatingSystem get LocalDateTime /value ^| findstr "="') do (
set "now=%%i"
)
call :DateToSecs %now:~0,4% %now:~4,2% %now:~6,2% %now:~8,2% %now:~10,2% %now:~12,2% EpochSecond
echo,%EpochSecond%
pause
exit /b
:DateToSecs %yy% %mm% %dd% %hh% %nn% %ss% secs
setlocal ENABLEEXTENSIONS
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5&set ss=%6
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
set /a j=j*86400+hh*3600+nn*60+ss
endlocal&set %7=%j%&goto :EOF