Page 1 of 1

if you need epochtime

Posted: Tue Aug 01, 2023 11:35 pm
by Dino
Hi guys

If you need to get epoch time in your programs, epoch time, "unix time", basically the number of seconds since Unix was created "midnight 1/1/1970", there are different ways, one way is to use a formula in your program and get it from #datetimex.now, but you have to be careful with GMT and stuff.

Easiest way if you are running in the web, is to ask for it to javascript (one of unix sons). For that you can create a simple widget (lets say JSTools), make it a Widget Type Object, add a propertiy like EpochTime, to return a Decimal:
epochtimewidget.jpg
epochtimewidget.jpg (112.02 KiB) Viewed 10612 times
then you can copy those lines in the sample implementation, to the implementation tab, and replace all if you wish with this lines:

Code: Select all

function( PROTOTYPE, WIDGET )
{
  PROTOTYPE.onCreateInstance = function()  {  }

  // EpochTime - Gets Epoch Time
  // Type: Decimal
  PROTOTYPE.getEpochTime = function()  {    return new Date().getTime();  }
  PROTOTYPE.setEpochTime = function( decValue )   {     this.m_EpochTime = decValue;  }

  // Even though EpochTime is read-only, setEpochTime is still required for the default value
  return WIDGET.Completed;
}
Compile it.

Then in your web page, view, dialog, reusable part of type web, you can drag and drop the widget to it and use it as needed:

Code: Select all

Begin_Com Role(*EXTENDS #PRIM_WEB) Theme(#SYS_THEME<MaterialDesignBlue>)

Define_Com Class(#JSTools) Name(#JSTools)
Define_Com Class(#PRIM_MD.Label) Name(#Text) Caption('Epoch Time') DisplayPosition(1) Height(45) Left(20) Parent(#COM_OWNER) TabPosition(1) ThemeDrawStyle('Title') Top(24) Width(405) Hint('Epoch Time')

Evtroutine Handling(#Com_owner.Initialize)
#Text.Caption := #JSTools.EpochTime.AsString
Endroutine
End_Com
epochtimewidget2.jpg
epochtimewidget2.jpg (36.26 KiB) Viewed 10612 times

Re: if you need epochtime

Posted: Wed Aug 02, 2023 9:01 am
by BrendanB
And if you are not running in web, then try using this method:

Code: Select all

Mthroutine Name(GetDateTimeInEpoch) Access(*PRIVATE)

* Inputs
Define_Map For(*INPUT) Class(#DATETIMEX) Name(#From) Mandatory(*SQLNULL)
* Result
Define_Map For(*RESULT) Class(#XLib_INT64) Name(#Result)

* Check if have a date
If (#From.IsSqlNull)

* We don't, so set now as default
#From := #From.Now

Endif

* Time to send Result
#Result := ((#From.Date.Difference( 1970-01-01 ) * 86400) + #From.Time.AsSeconds) * 1000 + (#From.FractionalSeconds / 1000000)

Endroutine