Datex.Time (datex v2.0.0)

Simple Time which can be used in different formats including elixir format ~T[15:45:56].

It includes different functions to compare time and provide relative time in friendly formats and functions which can format time in multiple ways user needs.

Summary

Functions

It adds time to a given specific time.

It compares two times and provides human friendly results.

It provides difference between 2 times in multiple units. It returns difference as Integer

It converts time in given format.

Get current time with or without timezone.

Returns the time period for the given time. If no time is provided, defaults to current time.

Generates a list of times between start and end time with specified interval.

Functions

add(time, added, unit \\ :second, format \\ :hour_12)

It adds time to a given specific time.

It takes 4 arguments. Last 2 arguments are optional i.e unit and format. To use a specific format, you need to apply unit as well.

Unit defaults to :second and format to :hour_12.

Arguments are

  1. time as elixir time or standard time formats in String

  2. time to add as Integer

  3. unit as atom from :hour, :minute, :second, :millisecond

  4. format as atom from :hour_12, :hour_24 and :elixir

Examples

iex()> Datex.Time.add(~T[16:56:56], 5)
"04:57 PM"

iex()> Datex.Time.add("4:56 PM", 5, :hour)
"09:56 PM"

iex()> Datex.Time.add("4:56:56", 5, :hour, :elixir)
~T[09:56:56.000000]

iex()> Datex.Time.add("4:56:56", 5, :minute, :hour_12)
"05:01 AM"

compare(time1, time2 \\ now())

It compares two times and provides human friendly results.

It takes 2 arguments. Both are time as elixir time or standard time formats in String. Second argument is optional as it provides current time in utc by default.

Examples

iex()> Datex.Time.compare("10:30 am")
"55 minutes later"

iex()> Datex.Time.compare("10:30 am", "15:34:25")
"5 hours and 4 minutes later"

iex()> Datex.Time.compare(~T[23:16:45], "15:34:25")
"7 hours and 42 minutes later"

difference(time1, time2 \\ now(), unit \\ :second)

It provides difference between 2 times in multiple units. It returns difference as Integer

It takes 3 arguments. Last 2 arguments are optional i.e time2 and format. To use a specific format, you need to apply time2 as well or default it to current time in utc i.e Datex.Time.now() function.

Unit defaults to :second and time2 to current time in utc.

Argumets are

1, 2. time as elixir time or standard time formats in String

  1. unit as atom from :hour, :minute, :second, :millisecond

Examples

iex()> Datex.Time.difference("10:30 am")
3480

iex()> Datex.Time.difference("11:10 am", Datex.Time.now())
20040

iex()> Datex.Time.difference("11:10 am", "10:10:10", :minute)
59

format_time(time, format)

It converts time in given format.

It takes 2 arguments. First is time as elixir time or standard time formats in String. Second argument is format which could be HH:MM, HH:MM:SS, HH:MM:SS:uS, 12 HOUR FORMAT or elixir as String.

Examples

iex()> Datex.Time.format_time(~T[23:16:45], "HH:MM:SS")
"23:16:45"

iex(6)> Datex.Time.format_time("9 pm", "HH:MM:SS")
"21:00:00""

iex(7)> Datex.Time.format_time("23:12:34", "12 HOUR FORMAT")
"11:12 PM"

iex(1)> Datex.Time.format_time("23:12:34", "elixir")
~T[23:12:34.000000]

now(zone_name \\ :utc)

Get current time with or without timezone.

It takes 1 optional argument i.e time_zone name. It returns utc time by default when no arguments is passed and time_zone specific time when time_zone is provided as String.

Examples

iex()> Datex.Time.now()
"04:21 AM"

iex()> Datex.Time.now("Pacific/Fakaofo")
"06:24 PM"

time_period(time \\ now())

Returns the time period for the given time. If no time is provided, defaults to current time.

Examples

iex> Datex.Time.time_period()
"morning"  # or whatever period it currently is

iex> Datex.Time.time_period("08:30")
"morning"

iex> Datex.Time.time_period("14:30")
"afternoon"

iex> Datex.Time.time_period("20:30")
"evening"

iex> Datex.Time.time_period("23:30")
"night"

iex> Datex.Time.time_period("05:30")
"night"

time_range(start_time, end_time, interval_minutes)

Generates a list of times between start and end time with specified interval.

Examples

iex> Datex.Time.time_range("09:00", "10:00", 15)
["09:00", "09:15", "09:30", "09:45", "10:00"]

iex> Datex.Time.time_range("14:00", "15:30", 30)
["14:00", "14:30", "15:00", "15:30"]

iex> Datex.Time.time_range("23:00", "01:00", 60)
["23:00", "00:00", "01:00"]