Skip to content

Commit

Permalink
ae.utils.time.fpdur: Add fromUnixTime
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberShadow committed Nov 28, 2024
1 parent f10c618 commit 084cc09
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions utils/time/fpdur.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module ae.utils.time.fpdur;

import core.time;

import ae.utils.time.types : AbsTime;

/// A variant of core.time.dur which accepts floating-point values.
/// Useful for parsing command-line arguments.
/// Beware of rounding / floating-point errors! Do not use where precision matters.
Expand Down Expand Up @@ -82,3 +84,13 @@ debug(ae_unittest) unittest
import core.time : seconds, msecs;
assert(1500.msecs.fracTotal!"seconds" == 1.5);
}

AbsTime fromUnixTime(double unixTime)
{
import std.datetime.systime : SysTime;
import std.datetime.timezone : UTC;

auto durationSinceUnixEpoch = unixTime.seconds;
enum stdTimeEpoch = SysTime.fromUnixTime(0, UTC()).stdTime;
return AbsTime(stdTimeEpoch) + durationSinceUnixEpoch;
}

0 comments on commit 084cc09

Please sign in to comment.