Krug.DateUtil (Krug v0.1.0) 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 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

diffSqlDatesInSeconds(sqlDateStart, sqlDateFinish)

View Source

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

Examples

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

getDateAndTimeNowString()

View Source

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

Example

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

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

Example

iex > Krug.DateUtil.getDateNowString()
05/12/2020

Obtain the actual datetime in numeric milliseconds.

Example

iex > Krug.DateUtil.getDateTimeNowMillis()
1607193124063

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

Return a Elixir Date Object.

Example

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

getNowToSql(diffDays, beginDay, endDay)

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 diffDays > 0, add this number of days in date (date + diffDays).

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

If beginDay == true, set time to 00:00:00, otherwise if endDay == true set time to 23:59:59.

Examples

iex > Krug.DateUtil.getNowToSql(0,false,false)
2020-12-05 17:35:58
iex > Krug.DateUtil.getNowToSql(1,false,false)
2020-12-06 17:35:58
iex > Krug.DateUtil.getNowToSql(-1,false,false)
2020-12-04 17:35:58
iex > Krug.DateUtil.getNowToSql(0,true,false)
2020-12-05 00:00:00
iex > Krug.DateUtil.getNowToSql(0,false,true)
2020-12-05 23:59:59
iex > Krug.DateUtil.getNowToSql(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.getTimeNowString()
17:35:58
Link to this function

minusMinutesSql(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.minusMinutesSql(1)
2020-12-04 23:59:35
Link to this function

sameDay(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

sameHour(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

sameMinute(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

sameMonth(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

sameSecond(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

sameYear(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

sqlDateToTime(sqlDate, whitTime \\ 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.sqlDateToTime("2020-12-05 17:35:58")
05/12/2020 17:35:58
iex > Krug.DateUtil.sqlDateToTime("2020-12-05 17:35:58",false)
05/12/2020
iex > Krug.DateUtil.sqlDateToTime("2020-12-05")
05/12/2020
Link to this function

timeToSqlDate(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.timeToSqlDate(timestamp)
2055-11-15 07:29:59