Skip to content

Instantly share code, notes, and snippets.

@0racle
Created June 19, 2024 09:06
Show Gist options
  • Save 0racle/f398470f8045d946e7d3d022dee22f6e to your computer and use it in GitHub Desktop.
Save 0racle/f398470f8045d946e7d3d022dee22f6e to your computer and use it in GitHub Desktop.
J translation of BQN datetime lib
cocurrent 'times'
dz_times_ =. 24 * *~ mz_times_ =. 60
ot_times_ =. 719469
dur_times_ =. ((100 * 365.25) - 0.75), 365.25, 30.6
off_times_ =. (ot_times_ - 0.25), 0.75, 0.41
T_times_ =. {{
'day sec' =. (0, dz) #: y
time =. mz | <.@%&mz^:(|. i. 3) sec
cym =. 0$0
for_z. dur ,"0 off do.
'zd zo' =. z
'dx mx' =. (0, zd) #: day + zo
day =. <. mx
cym =. cym, dx
end.
'cn yr mo' =. cym
mo =. mo - 12 * 10 <: mo
((100 * cn) + yr + mo < 0), (3 + mo), (1 + day), time
}}
F_times_ =. {{
'yx mx dx' =. 3 {. x: y
f =. 0 > mx =. mx - 3
z =. (dx - ot) + +/ <. (0,0, {: off) + dur * ((0,100) #: yx - f), mx + 12 * f
(dz * z) + (+~ mz&*)/ |. _3 {. y
}}
Now_z_ =: (6!:0)
ToTime_z_ =: T_times_ :. F_times_
FromTime_z_ =: F_times_ :. T_times_
@0racle
Copy link
Author

0racle commented Jun 19, 2024

Usage:

   load 'times.ijs'
   FromTime <. Now ''             NB. Now is 6!:0
1718814962
   ToTime FromTime <. Now ''
2024 6 19 16 36 5
   (-&36000)&.FromTime <. Now ''  NB. Conversions are Under-able
2024 6 19 6 36 13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment