1

In Dyalog APL I can use the function ⎕TS to get the current year, month, day, hour, minute, second etc, but is there a way to get the current timestamp in seconds from e.g. 1970-01-01 (Unix timestamp)?

August Karlstrom
  • 10,773
  • 7
  • 38
  • 60

2 Answers2

4

Using days from https://dfns.dyalog.com/n_days.htm

      )copy dfns days
C:\Program Files\Dyalog\Dyalog APL-64 17.1 Unicode\ws\dfns.dws saved Wed Aug 21 19:40:24 2019
      86400×(days ⎕TS)-(days 1970 1 1)
1570618030

Could also use the over operator, coming in Dyalog version 18.0

      )copy dfns days
C:\Program Files\Dyalog\Dyalog APL-64 17.1 Unicode\ws\dfns.dws saved Wed Aug 21 19:40:24 2019
      O←{(⍺⍺ ⍺)⍵⍵ ⍺⍺ ⍵} ⍝ The over operator
      86400×⎕TS days O - 1970 1 1
1570618337
RikedyP
  • 632
  • 4
  • 8
  • Thanks for the suggestion.Wouldn't it make sense to provide a builtin function which returns a timestamp in seconds. – August Karlstrom Oct 09 '19 at 10:38
  • In a namespace script (`:Namespace`), how do I import *dfns* and *days*? As far as I understand `:Require` can only be used for other namespace scripts. – August Karlstrom Oct 09 '19 at 10:52
  • @AugustKarlstrom Yes, 18.0 will have a system function which can return UNIX time (among many other formats). Use `'days'⎕CY'dfns'` to import. – Adám Oct 09 '19 at 20:22
0

Since 18.0, Dyalog has had the ⎕DT function which can convert among multiple different date/time formats. UNIX timestamps are format number 20, so this will get the current one:

      20 ⎕DT ⊂⎕TS
1659344000

Actually, if the right argument is a character scalar (interpreted as a military timezone designation), the current time is assumed. However, the conversion function assumes the date/time value it's converting from is already in UTC, so to get the correct value you must specify that zone via the letter "Z" (in uppercase; the zone designations are case-sensitive):

      20 ⎕DT 'Z'
1659358600
Mark Reed
  • 91,912
  • 16
  • 138
  • 175