Krug.DateUtil (Krug v1.1.44) View Source

Utilitary module to handle date and datetime conversions.

Link to this section Summary

Functions

Calculates the diff in seconds between 2 date and time objects as string in format yyyy-mm-dd H:i:s.

Obtain the actual date and time as string in format dd/mm/yyyy H:i:s.

Obtain the actual date as string in format dd/mm/yyyy.

Obtain the actual datetime in numeric milliseconds.

Obtain the actual date in format yyyy-mm-dd.

Obtain the actual date and time as string in format yyyy-mm-dd H:i:s, making some operations according received parameters.

Obtain the actual hour of day as string in format H:i:s.

Calculates the atual date minus X minutes in format yyyy-mm-dd H:i:s.

Verify if two nano seconds obtained by System.os_time() match in same day of same month of same year.

Verify if two nano seconds obtained by System.os_time() match in same hour of same day of same month of same year.

Verify if two nano seconds obtained by System.os_time() match in same minute of same hour of ... of same year.

Verify if two nano seconds obtained by System.os_time() match in same month of same year.

Verify if two nano seconds obtained by System.os_time() match in same second of same minute of same ... of same year.

Verify if two nano seconds obtained by System.os_time() match in same year.

Obtain one date and time as string in format yyyy-mm-dd or yyyy-mm-dd H:i:s

Convert a numeric timestamp value in miliseconds to a sql date string in format yyyy-mm-dd H:i:s.

Link to this section Functions

Link to this function

diff_sql_dates_in_seconds(sql_date_start, sql_date_finish)

View Source

Calculates the diff in seconds between 2 date and time objects as string in format yyyy-mm-dd H:i:s.

Examples

iex > Krug.DateUtil.diff_sql_dates_in_seconds("2020-12-05 17:35:58","2020-12-05 16:35:58")
-3600
iex > Krug.DateUtil.diff_sql_dates_in_seconds("2020-12-05 17:35:58","2020-12-05 18:35:58")
3600
Link to this function

get_date_and_time_now_string()

View Source

Obtain the actual date and time as string in format dd/mm/yyyy H:i:s.

Example

iex > Krug.DateUtil.get_date_and_time_now_string()
05/12/2020 17:35:58

Obtain the actual date as string in format dd/mm/yyyy.

Example

iex > Krug.DateUtil.get_date_now_string()
05/12/2020
Link to this function

get_date_time_now_millis()

View Source

Obtain the actual datetime in numeric milliseconds.

Example

iex > Krug.DateUtil.get_date_time_now_millis()
1607193124063

Obtain the actual date in format yyyy-mm-dd.

Return a Elixir Date Object.

Example

iex > Krug.DateUtil.get_now()
~D[2020-12-05]
Link to this function

get_now_to_sql(diff_days, begin_day, end_day)

View Source

Obtain the actual date and time as string in format yyyy-mm-dd H:i:s, making some operations according received parameters.

If diff_days > 0, add this number of days in date (date + diff_days).

If diff_days < 0, subtract this number in date (date - diff_days).

If begin_day == true, set time to 00:00:00, otherwise if end_day == true set time to 23:59:59.

Examples

iex > Krug.DateUtil.get_now_to_sql(0,false,false)
2020-12-05 17:35:58
iex > Krug.DateUtil.get_now_to_sql(1,false,false)
2020-12-06 17:35:58
iex > Krug.DateUtil.get_now_to_sql(-1,false,false)
2020-12-04 17:35:58
iex > Krug.DateUtil.get_now_to_sql(0,true,false)
2020-12-05 00:00:00
iex > Krug.DateUtil.get_now_to_sql(0,false,true)
2020-12-05 23:59:59
iex > Krug.DateUtil.get_now_to_sql(3,true,false)
2020-12-08 00:00:00

Obtain the actual hour of day as string in format H:i:s.

Example

iex > Krug.DateUtil.get_time_now_string()
17:35:58
Link to this function

minus_minutes_sql(minutes)

View Source

Calculates the atual date minus X minutes in format yyyy-mm-dd H:i:s.

Useful when you need verify if an event run in last minute, for example clear a cache, or count a number of requests of a service since last minute.

Example

Imagine that actual date is 2020-12-05 00:00:35.

iex > Krug.DateUtil.minus_minutes_sql(1)
2020-12-04 23:59:35
Link to this function

same_day(nanoseconds1, nanoseconds2)

View Source

Verify if two nano seconds obtained by System.os_time() match in same day of same month of same year.

Useful if you want controll use of resources by time interval.

Link to this function

same_hour(nanoseconds1, nanoseconds2)

View Source

Verify if two nano seconds obtained by System.os_time() match in same hour of same day of same month of same year.

Useful if you want controll use of resources by time interval.

Link to this function

same_minute(nanoseconds1, nanoseconds2)

View Source

Verify if two nano seconds obtained by System.os_time() match in same minute of same hour of ... of same year.

Useful if you want controll use of resources by time interval.

Link to this function

same_month(nanoseconds1, nanoseconds2)

View Source

Verify if two nano seconds obtained by System.os_time() match in same month of same year.

Useful if you want controll use of resources by time interval.

Link to this function

same_second(nanoseconds1, nanoseconds2)

View Source

Verify if two nano seconds obtained by System.os_time() match in same second of same minute of same ... of same year.

Useful if you want controll use of resources by time interval.

Link to this function

same_year(nanoseconds1, nanoseconds2)

View Source

Verify if two nano seconds obtained by System.os_time() match in same year.

Useful if you want controll use of resources by time interval.

Link to this function

sql_date_to_time(sql_date, with_time \\ true)

View Source

Obtain one date and time as string in format yyyy-mm-dd or yyyy-mm-dd H:i:s

and convert to format dd/mm/yyyy H:i:s or dd/mm/yyyy

conform the value and parameter received.

Examples

iex > Krug.DateUtil.sql_date_to_time("2020-12-05 17:35:58")
05/12/2020 17:35:58
iex > Krug.DateUtil.sql_date_to_time("2020-12-05 17:35:58",false)
05/12/2020
iex > Krug.DateUtil.sql_date_to_time("2020-12-05")
05/12/2020
Link to this function

time_to_sql_date(timestamp)

View Source

Convert a numeric timestamp value in miliseconds to a sql date string in format yyyy-mm-dd H:i:s.

If receive a nil or <= 0 value, return nil.

Example

iex > timestamp = 2709876599456
iex > Krug.DateUtil.time_to_sql_date(timestamp)
2055-11-15 07:29:59