idfk v0.1.0 Idfk.DateTime

Summary

Functions

A wrapper method around :calendar.universal_time. It’s difficult to mock methods on :calendar, and it’s easy to mock this method

Returns the datetime with milliseconds. Even though method has arity of 2, arguments are only used for testing purposes

Drops milliseconds from datetime with millisecond precision

Converts an integer formatted datetime based on the Gregorian epoch of 1 Jan 0000 @ 12:00 a.m. to an integer formatted datetime based on the posix epoch of 1 Jan 1970 @ 12:00 a.m

The datetime 1 Jan 1970 @ 12:00 a.m. in seconds. Hard coded for performance reasons

A wrapper method around :os.timestamp. It’s difficult to mock methods on :os, and it’s easy to mock this method

Converts a datetime tuple with second or millisecond precision to an iso8601 formatted string within a tuple. Assumes input is UTC

Similar to to_iso8601, but it returns date_as_string instead of {:ok, date_as_string}. Raises an exception is there’s an error

Types

Functions

datetime()

Specs

A wrapper method around :calendar.universal_time. It’s difficult to mock methods on :calendar, and it’s easy to mock this method.

# this generates an error when running an ESpec test suite
# Error looks something like this:
# 12:16:46.292 [error] Can't load module ':calendar' that resides in sticky dir
before do: allow :calendar |> to(accept :universal_time,
                              fn -> {{2016, 4, 12}, {21, 8, 31}} end)

# this works like a charm!
before do: allow Idfk.DateTime |> to(accept :datetime,
                                  fn -> {{2016, 4, 12}, {21, 8, 31}} end)
datetime_with_milliseconds()

Specs

datetime_with_milliseconds :: datetime_millis_precision

Returns the datetime with milliseconds. Even though method has arity of 2, arguments are only used for testing purposes.

# Normally you'll simply call without arguments
Idfk.DateTime.datetime_with_milliseconds
datetime_with_milliseconds_to_datetime_with_seconds(arg)

Specs

datetime_with_milliseconds_to_datetime_with_seconds(datetime_millis_precision) :: datetime_second_precision

Drops milliseconds from datetime with millisecond precision

# this method name is kind of long...
iex> Idfk.DateTime.datetime_with_milliseconds_to_datetime_with_seconds({{2016, 4, 12}, {19, 11, 51, 730}})
{{2016, 4, 12}, {19, 11, 51}}
gregorian_seconds_to_posix_seconds(seconds)

Specs

gregorian_seconds_to_posix_seconds(integer) :: integer

Converts an integer formatted datetime based on the Gregorian epoch of 1 Jan 0000 @ 12:00 a.m. to an integer formatted datetime based on the posix epoch of 1 Jan 1970 @ 12:00 a.m.

iex> Idfk.posix_epoch_in_seconds(63627199062)
1459979862
posix_epoch_in_seconds()

Specs

posix_epoch_in_seconds :: integer

The datetime 1 Jan 1970 @ 12:00 a.m. in seconds. Hard coded for performance reasons.

# same as this scenario
iex> :calendar.datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}})
62167219200
timestamp()

Specs

A wrapper method around :os.timestamp. It’s difficult to mock methods on :os, and it’s easy to mock this method.

# this generates an error when running an ESpec test suite
# Error looks something like this:
# 12:16:46.292 [error] Can't load module ':os' that resides in sticky dir
before do: allow :os |> to(accept :timestamp,
                        fn -> {1460, 495583, 776409} end)

# this works like a charm!
before do: allow Idfk.DateTime |> to(accept :timestamp,
                                  fn -> {{1460, 495583, 776409} end)
to_iso8601(datetime)

Specs

to_iso8601({{year, month, day}, {hours, minutes, seconds}}) ::
  {:ok, String.t} |
  {:error, term}
to_iso8601({{year, month, day}, {hours, minutes, seconds, milliseconds}}) ::
  {:ok, String.t} |
  {:error, term}

Converts a datetime tuple with second or millisecond precision to an iso8601 formatted string within a tuple. Assumes input is UTC.

# second precision
iex> Idfk.to_iso8601({{2016,4,6},{16,12,35}})
{:ok, "2016-04-06T16:12:35+00:00"}

# millisecond precision
iex> Idfk.to_iso8601({{2016,4,6},{16,12,35,123}})
{:ok, "2016-04-06T16:12:35.123+00:00"}
to_iso8601!(datetime)

Specs

to_iso8601!(datetime) :: String.t

Similar to to_iso8601, but it returns date_as_string instead of {:ok, date_as_string}. Raises an exception is there’s an error.

iex> Idfk.to_iso8601!({{2016,4,6},{16,12,35}})
"2016-04-06T16:12:35+00:00"