Getting the EPOCH date

Started by hdsouza, October 06, 2018, 04:12:31 PM

Previous topic - Next topic

hdsouza

Hi, I am Trying to get the EPOCH date (also called the Unix date) from the current date.

I tried to get the syntax for the conversion from https://www.epochconverter.com/ and http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/Time~-~Timer~and~Date~Functions+Convert~Unix~Time.txt

Any ideas what I am doing incorrectly.

Code (winbatch) Select
Time_date = strsub(TimeYmdHms ( ), 1, 10)
Time_date = strcat(Time_date, ":00:00:00")
d1970="1970:01:01:00:00:00"
D1970M5 = TimeAdd(D1970,"0000:00:00:05:00:00")
Time_Epoch = TimeSubtract(Time_date, D1970M5 )


Please help
Thanks

JTaylor

Is this what you are wanting, or at least I think this will put you on the right path?

   etime = TimeDiffSecs("2018:10:07:01:32:55", "1970:01:01:00:00:00")


Jim

snowsnowsnow

Here's the easy way:

Code (winbatch) Select

; These 3 lines are the active lines.
msvcrt = DllLoad("msvcrt.dll")
tme = DllCallCdecl(msvcrt,long:"time",lpnull)
Pause("tme = ",tme)

; The rest is for test/verification...
buff = BinaryAlloc(4)
BinaryPoke4(buff,0,tme)
Pause("Current time = ",DllCallCdecl(msvcrt,lpstr:"ctime",lpbinary:buff))


hdsouza

Quote from: JTaylor on October 06, 2018, 06:33:41 PM
Is this what you are wanting, or at least I think this will put you on the right path?

   etime = TimeDiffSecs("2018:10:07:01:32:55", "1970:01:01:00:00:00")


Jim

Thanks Jim. It works great !!!!!

stanl

Jim's solution should give GMT time, while Snow's corrects for the bias offset.  OT: I had read that msvcrt.dll might have issue in Win10, but worked fine. 


Test both
Code (WINBATCH) Select


;submitted by Snow on 10/7
; These 3 lines are the active lines.
msvcrt = DllLoad("msvcrt.dll")
tme = DllCallCdecl(msvcrt,long:"time",lpnull)
Pause("tme = ",tme)


; The rest is for test/verification...
buff = BinaryAlloc(4)
BinaryPoke4(buff,0,tme)
Pause("Current time = ",DllCallCdecl(msvcrt,lpstr:"ctime",lpbinary:buff))


;checking with Jim's sugestion
etime = TimeDiffSecs(TimeYmdHms( ), "1970:01:01:00:00:00")
Pause("Time Diff",tme:@CRLF:etime)

hdsouza

Thanks for the check Stan and Snow
Looks like we have to subtract 4 hours from the 1970 time. Something to do with GMT correction

Code (winbatch) Select

d1970M5=Timesubtract("1970:01:01:00:00:00","0000:00:00:04:00:00")
etime = TimeDiffSecs(TimeYmdHms( ), d1970M5)

snowsnowsnow

Quote from: hdsouza on October 07, 2018, 09:32:56 AM
Thanks for the check Stan and Snow
Looks like we have to subtract 4 hours from the 1970 time. Something to do with GMT correction


If you do it the way I suggested, then you don't have to bother with this.

stanl

Quote from: snowsnowsnow on October 07, 2018, 02:13:39 PM
If you do it the way I suggested, then you don't have to bother with this.

Agree 100%. Op should have specified how he wanted EPOCH returned, especially if the alternative is to hardcode for DST.

td

Nothing wrong with using the C RTL but here is a simple UDF for converting from local time to UTC using the WIL YMDHMS time format.

Code (winbatch) Select
;; Returns input YMDHMS time converted to UTC
#definefunction TimeUTC(_YmdDate)
   nTimeBias = RegQueryDword(@REGMACHINE, "System\CurrentControlSet\control\TimeZoneInformation[ActiveTimeBias]")
   if nTimeBias > 0 then return TimeAdd(_YmdDate,'00:00:00:00:':nTimeBias:':00')
   else if nTimeBias < 0 then return TimeSubtract(_YmdDate,'00:00:00:00:':Abs(nTimeBias):':00') 
   return _YmdDate
#endfunction

;; Simple test.
Now = TimeYMDHMS()
Utc = TimeUtc(Now)
message('Current Time', 'Local - ':Now:@lf:'UTC - ':Utc)


Combined this UDF with Jim's solution and you get the Unix epoch ticks.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Dug this out from something I posted in 2011. Pretty much combines Jim and Tony [his Registry is probably better than WMI] and adds a little ActiveX in case the return needs to be inserted into a db table as a timestamp.


Code (WINBATCH) Select


;Winbatch 2011A - quick way to create Unix TimeStamp
;                 useful for filling SQLite Date Types
;                 Accounts for storing as GMT
;Stan Littlefield, June 9, 2011
;///////////////////////////////////////////////////////////
#DefineFunction UnixTime(toGMT)


u = TimeDiffSecs(TimeYmdHms(),"1970:01:01:00:00:00")


If toGMT
   strComputer = "."
   oWMI = GetObject(StrCat("winmgmts:{impersonationLevel=impersonate}!\\",strComputer,"\root\cimv2"))
   oT  = oWMI.ExecQuery("Select * from Win32_ComputerSystem")
   ForEach t In oT
        Time = t.CurrentTimeZone
   Next
   If Time<0
      u=u+(Abs(Time)*60)
   Else
      u=u-(Time*60)
   EndIf
EndIf


Return(u)
#EndFunction


;tests
TLocal=UnixTime(0)
TGMT = UnixTime(1)


Message("","UNIX Epoch Times":@CRLF:"Local ":TLocal:@CRLF:"GMT ":TGMT)


;create TimeStamp with ActiveX
;local time
oS = CreateObject("MSScriptControl.ScriptControl")
oS.Language = "VBScript"
oS.AllowUI = @FALSE
u=oS.Eval(: 'FormatDateTime((%TLocal%+2209161600)/86400,0)')
oS = 0


Message("Local TimeStamp",u)


Exit

td

Quote from: stanl on October 11, 2018, 03:34:14 AM
Dug this out from something I posted in 2011. Pretty much combines Jim and Tony [his Registry is probably better than WMI] and adds a little ActiveX in case the return needs to be inserted into a db table as a timestamp.


Using the registry has the advantage of less overhead but using WMI would in theory future proof a script to some extent.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: td on October 11, 2018, 09:52:41 AM
Using the registry has the advantage of less overhead but using WMI would in theory future proof a script to some extent.

That was 2011.