16733, “ben-albrecht”, “Add datetime type method to get time since Unix epoch”, “2020-11-17T20:53:13Z”
We would like to deprecate Time.getCurrentTime()
for DateTime
functionality. However, there are 2 common use-cases of getCurrentTime()
and one of them does not translate well to DateTime
:
-
getCurrentTime()
to get the hour, minute or seconds of the current time since midnight in local timezone. This required some additional effort on the user as pointed out in https://github.com/chapel-lang/chapel/issues/14571.datetime.now()
should be much better for this use-case. -
getCurrentTime()
to get timestamps throughout a program for timing purposes. This is not recommended, becausegetCurrentTime()
is time since midnight and the time could roll into the next day between time stamps. However, many codes use it for this purpose. One can translate theirgetCurrentTime()
call to(datetime.now() - datetime.now().replace(hour=0,minute=0,second=0,microsecond=0)).total_seconds()
in the DateTime module, but this is (1) much more verbose, and (2) still prone to the day roll-over issue.
To better support use-case (2), I propose we add a DateTime function (perhaps a datetime type method) that returns the time since Unix epoch:
// a better name probably exists...
var t = DateTime.timeSinceUnixEpoch();
writeln(t); // 1605632958
This feature is already available in the DateTime internals, but simply needs to be exposed through a nice interface.
Eventually, we’d want a true monotonically increasing clock feature (https://github.com/chapel-lang/chapel/issues/7536). However, this will likely be a longer term effort.