Kronos v1.0.0 Kronos
Kronos is a tool to facilitate the manipulation of dates (via Timestamps). This library use the seconds as a reference.
iex> import Kronos
...> use Kronos.Infix
...> {:ok, t} = new({2010, 12, 20}, {0, 0, 0})
...> r = t + ~t(2)day + ~t(3)hour + ~t(10)minute + ~t(13)second
...> Kronos.to_string(r)
"2010-12-22 03:10:13Z"
The measures references are :
Kronos.secondKronos.minuteKronos.secondKronos.fifteen_minutesKronos.half_hourKronos.hourKronos.half_dayKronos.dayKronos.weekKronos.monthThis measure is an approximation (30 days)Kronos.yearThis measure is an approximation (365 days)
Unsafe values
Kronos.month and Kronos.year are unsafe, they represents a “approximation”
of a month or a year. You should never use it with Kronos.truncate/2,
Kronos.next/2 and Kronos.pred/2 (and of course as a precision flag).
Summary
Types
This type represents a couple date-time
This type represents the day of the week
This type represents a range between two timestamp
This type represents a metric_type
This type represents a triplet of non negative values
This type represents a failable result
This type represents a typed timestamp
This type represents a specific week type
Functions
Kronos.after?(a, b) check if a is later in time than b
Kronos.before?(a, b) check if a is earlier in time than b
References the subtype day
Builds a value into the subtype day
Returns the day of the week from a Kronos.t.
0 for Monday, 6 for Sunday
Returns the day of the week from a Kronos.t.
0 for Monday, 6 for Sunday
Returns a Kronos.t with the number of days in a month,
the month is referenced by year and month (non neg integer)
Returns the difference (always positive) between to members of a duration
Kronos.equivalent?(a, b) check if a is at the same moment of b
References the subtype fifteen_minutes
Builds a value into the subtype fifteen_minutes
Converts a DateTime.t into a Kronos.t
References the subtype half_day
Builds a value into the subtype half_day
References the subtype half_hour
Builds a value into the subtype half_hour
References the subtype hour
Builds a value into the subtype hour
Returns the hours (relatives) of a timestamp
Check if a Kronos.t is include into a Kronos.duration
Creates a duration between two Kronos.t. This duration
is a Mizur.Range.range
Creates a duration between two Kronos.t. This duration
is a Mizur.Range.range
Returns if the given year is a leap year
References the subtype minute
Builds a value into the subtype minute
Returns the minutes (relatives) of a timestamp
References the subtype month
Builds a value into the subtype month
Converts an erlang datetime representation to a Kronos.result
Converts two tuple (date, time) to a Kronos.result
Same of Kronos.new/1 but raise an ArgumentError if the
timestamp creation failed
Same of Kronos.new/2 but raise an ArgumentError if the
timestamp creation failed
Jump to the next value of a type. For example
next(Kronos.day, of: Kronos.new({2017, 10, 10}, {22, 12, 12})) give the
date : 2017-10-11, 0:0:0
Returns the current timestamp (in a Kronos.t)
Checks that two durations have an intersection
Jump to the pred value of a type. For example
next(Kronos.day, of: Kronos.new({2017, 10, 10}, {22, 12, 12})) give the
date : 2017-10-09, 0:0:0
References the subtype second
Builds a value into the subtype second
Returns the seconds (relatives) of a timestamp
Converts a Kronos.t to a DateTime.t, the result is wrapped
into {:ok, value} or {:error, reason}
Converts a Kronos.t to a DateTime.t. Raise an ArgumentError if
the timestamp is not valid
Returns the wrapped values (into a Kronos.t) as an
integer in second. This function is mainly used to convert
Kronos.t toDateTime.t
Convert a Kronos.t into a string, use the
DateTime inspect
Rounds the given timestamp (timestamp) to the given type (at)
References the subtype week
Builds a value into the subtype week
References the subtype year
Builds a value into the subtype year
Types
This type represents a couple date-time
This type represents the day of the week
This type represents a range between two timestamp
This type represents a metric_type
This type represents a triplet of non negative values
This type represents a failable result
This type represents a typed timestamp
This type represents a specific week type
Functions
Kronos.after?(a, b) check if a is later in time than b.
iex> {a, b} = {Kronos.new!(2), Kronos.new!(1)}
...> Kronos.after?(a, b)
true
You can specify a precision, to ignore minutes, hours or days.
(By passing a precision, both parameters will be truncated via
Kronos.truncate/2).
Kronos.before?(a, b) check if a is earlier in time than b.
iex> {a, b} = {Kronos.new!(2), Kronos.new!(1)}
...> Kronos.before?(b, a)
true
You can specify a precision, to ignore minutes, hours or days.
(By passing a precision, both parameters will be truncated via
Kronos.truncate/2).
Returns the day of the week from a Kronos.t.
0 for Monday, 6 for Sunday.
iex> a = KronosTest.mock(:day, 1970, 1, 1, 12, 10, 11)
...> Kronos.day_of_week(a)
:thu
iex> a = KronosTest.mock(:day, 2017, 4, 29, 0, 3, 11)
...> Kronos.day_of_week(a)
:sat
Returns the day of the week from a Kronos.t.
0 for Monday, 6 for Sunday.
iex> a = KronosTest.mock(:day, 1970, 1, 1, 12, 10, 11)
...> Kronos.day_of_week_internal(a)
3
iex> a = KronosTest.mock(:day, 2017, 4, 29, 0, 3, 11)
...> Kronos.day_of_week_internal(a)
5
Returns a Kronos.t with the number of days in a month,
the month is referenced by year and month (non neg integer).
iex> Kronos.days_in(2004, 2)
Kronos.day(29)
iex> Kronos.days_in(2005, 2)
Kronos.day(28)
iex> Kronos.days_in(2005, 1)
Kronos.day(31)
iex> Kronos.days_in(2001, 4)
Kronos.day(30)
Returns the difference (always positive) between to members of a duration.
iex> duration = KronosTest.mock(:duration, 2017, 2018)
...> Mizur.from((Kronos.diff(duration)), to: Kronos.day)
Kronos.day(365)
Kronos.equivalent?(a, b) check if a is at the same moment of b.
iex> {a, b} = {Kronos.new!(2), Kronos.new!(1)}
...> Kronos.equivalent?(b, a, Kronos.hour())
true
You can specify a precision, to ignore minutes, hours or days.
(By passing a precision, both parameters will be truncated via
Kronos.truncate/2).
Converts a DateTime.t into a Kronos.t
Returns the hours (relatives) of a timestamp.
iex> a = KronosTest.mock(:day, 2017, 10, 10, 23, 45, 53)
...> Kronos.hours_of(a)
23
iex> a = KronosTest.mock(:day, 1903, 10, 10, 13, 22, 7)
...> Kronos.hours_of(a)
13
Check if a Kronos.t is include into a Kronos.duration.
iex> duration = KronosTest.mock(:duration, 2017, 2018)
...> a = KronosTest.mock(:day, 2015, 12, 10)
...> b = KronosTest.mock(:day, 2017, 5, 10)
...> {Kronos.include?(a, in: duration), Kronos.include?(b, in: duration)}
{false, true}
Creates a duration between two Kronos.t. This duration
is a Mizur.Range.range.
iex> a = Kronos.new!(1)
...> b = Kronos.new!(100)
...> [from: a, to: b] |> Kronos.laps
Mizur.Range.new(Kronos.new!(1), Kronos.new!(100))
Creates a duration between two Kronos.t. This duration
is a Mizur.Range.range.
iex> a = Kronos.new!(1)
...> b = Kronos.new!(100)
...> Kronos.laps(a, b)
Mizur.Range.new(Kronos.new!(1), Kronos.new!(100))
Returns if the given year is a leap year.
iex> Kronos.leap_year?(2004)
true
iex> Kronos.leap_year?(2017)
false
Returns the minutes (relatives) of a timestamp.
iex> a = KronosTest.mock(:day, 2017, 10, 10, 23, 45, 53)
...> Kronos.minutes_of(a)
45
iex> a = KronosTest.mock(:day, 1903, 10, 10, 13, 22, 7)
...> Kronos.minutes_of(a)
22
Converts an erlang datetime representation to a Kronos.result
Converts two tuple (date, time) to a Kronos.result
Same of Kronos.new/1 but raise an ArgumentError if the
timestamp creation failed.
Same of Kronos.new/2 but raise an ArgumentError if the
timestamp creation failed.
Jump to the next value of a type. For example
next(Kronos.day, of: Kronos.new({2017, 10, 10}, {22, 12, 12})) give the
date : 2017-10-11, 0:0:0.
iex> t = KronosTest.mock(:day, 2017, 10, 10)
...> Kronos.next(Kronos.day, of: t)
KronosTest.mock(:day, 2017, 10, 11)
Returns the current timestamp (in a Kronos.t)
Checks that two durations have an intersection.
iex> durationA = KronosTest.mock(:duration, 2016, 2018)
...> durationB = KronosTest.mock(:duration, 2017, 2019)
...> Kronos.overlap?(durationA, with: durationB)
true
Jump to the pred value of a type. For example
next(Kronos.day, of: Kronos.new({2017, 10, 10}, {22, 12, 12})) give the
date : 2017-10-09, 0:0:0.
iex> t = KronosTest.mock(:day, 2017, 10, 10)
...> Kronos.pred(Kronos.day, of: t)
KronosTest.mock(:day, 2017, 10, 9)
Returns the seconds (relatives) of a timestamp.
iex> a = KronosTest.mock(:day, 2017, 10, 10, 23, 45, 53)
...> Kronos.seconds_of(a)
53
iex> a = KronosTest.mock(:day, 1903, 10, 10, 13, 22, 7)
...> Kronos.seconds_of(a)
7
Converts a Kronos.t to a DateTime.t, the result is wrapped
into {:ok, value} or {:error, reason}.
iex> ts = 1493119897
...> a = Kronos.new!(ts)
...> b = DateTime.from_unix(1493119897)
...> Kronos.to_datetime(a) == b
true
Converts a Kronos.t to a DateTime.t. Raise an ArgumentError if
the timestamp is not valid.
iex> ts = 1493119897
...> a = Kronos.new!(ts)
...> b = DateTime.from_unix!(1493119897)
...> Kronos.to_datetime!(a) == b
true
Returns the wrapped values (into a Kronos.t) as an
integer in second. This function is mainly used to convert
Kronos.t toDateTime.t.
iex> x = Kronos.new!(2000)
...> Kronos.to_integer(x)
2000
Convert a Kronos.t into a string, use the
DateTime inspect.
Rounds the given timestamp (timestamp) to the given type (at).
iex> ts = Kronos.new!({2017, 10, 24}, {23, 12, 07})
...> Kronos.truncate(ts, at: Kronos.hour())
Kronos.new!({2017, 10, 24}, {23, 0, 0})
For example :
- truncate of 2017/10/24 23:12:07 at
minutegives : 2017/10/24 23:12:00 - truncate of 2017/10/24 23:12:07 at
hourgives : 2017/10/24 23:00:00 - truncate of 2017/10/24 23:12:07 at
daygives : 2017/10/24 00:00:00
Builds a value into the subtype week