viewpoint-particle

Author Topic: Getting the EPOCH date  (Read 107 times)

hdsouza

  • Full Member
  • ***
  • Posts: 142
Getting the EPOCH date
« on: October 06, 2018, 04:12:31 pm »
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
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

  • Pundit
  • *****
  • Posts: 919
    • Data & Stuff Inc.
Re: Getting the EPOCH date
« Reply #1 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

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 250
Re: Getting the EPOCH date
« Reply #2 on: October 06, 2018, 07:34:29 pm »
Here's the easy way:

Code: Winbatch
; 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

  • Full Member
  • ***
  • Posts: 142
Re: Getting the EPOCH date
« Reply #3 on: October 06, 2018, 07:41: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

  • Pundit
  • *****
  • Posts: 794
Re: Getting the EPOCH date
« Reply #4 on: October 07, 2018, 08:04:10 am »
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

;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

  • Full Member
  • ***
  • Posts: 142
Re: Getting the EPOCH date
« Reply #5 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

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

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 250
Re: Getting the EPOCH date
« Reply #6 on: October 07, 2018, 02:13:39 pm »
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

  • Pundit
  • *****
  • Posts: 794
Re: Getting the EPOCH date
« Reply #7 on: October 08, 2018, 06:44:38 am »
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

  • Tech Support
  • *****
  • Posts: 2595
    • WinBatch
Re: Getting the EPOCH date
« Reply #8 on: October 10, 2018, 09:30:30 am »
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
;; 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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


stanl

  • Pundit
  • *****
  • Posts: 794
Re: Getting the EPOCH date
« Reply #9 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.


Code: Winbatch

;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

  • Tech Support
  • *****
  • Posts: 2595
    • WinBatch
Re: Getting the EPOCH date
« Reply #10 on: October 11, 2018, 09:52:41 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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


stanl

  • Pundit
  • *****
  • Posts: 794
Re: Getting the EPOCH date
« Reply #11 on: October 11, 2018, 11:28:09 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.