From 8b56d1572fec145d2bafc21c319de3ee6230a37d Mon Sep 17 00:00:00 2001 From: labbots Date: Sun, 7 Jun 2020 20:10:07 +0100 Subject: [PATCH] Added date functions --- README.md | 895 +++++++++++++++++++++++++++++++++++++++++ bash_utilities.sh | 1 + bin/generate_readme.sh | 2 + src/date.sh | 769 +++++++++++++++++++++++++++++++++++ src/file.sh | 4 +- 5 files changed, 1669 insertions(+), 2 deletions(-) create mode 100644 src/date.sh diff --git a/README.md b/README.md index d74aed1..a190813 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,39 @@ Bash library which provides utility functions and helpers for functional program - [array::join()](#arrayjoin) - [array::reverse()](#arrayreverse) - [array::random_element()](#arrayrandom_element) +- [Date](#date) + - [date::now()](#datenow) + - [date::epoc()](#dateepoc) + - [date::add_days_from()](#dateadd_days_from) + - [date::add_months_from()](#dateadd_months_from) + - [date::add_years_from()](#dateadd_years_from) + - [date::add_weeks_from()](#dateadd_weeks_from) + - [date::add_hours_from()](#dateadd_hours_from) + - [date::add_minutes_from()](#dateadd_minutes_from) + - [date::add_seconds_from()](#dateadd_seconds_from) + - [date::add_days()](#dateadd_days) + - [date::add_months()](#dateadd_months) + - [date::add_years()](#dateadd_years) + - [date::add_weeks()](#dateadd_weeks) + - [date::add_hours()](#dateadd_hours) + - [date::add_minutes()](#dateadd_minutes) + - [date::add_seconds()](#dateadd_seconds) + - [date::sub_days_from()](#datesub_days_from) + - [date::sub_months_from()](#datesub_months_from) + - [date::sub_years_from()](#datesub_years_from) + - [date::sub_weeks_from()](#datesub_weeks_from) + - [date::sub_hours_from()](#datesub_hours_from) + - [date::sub_minutes_from()](#datesub_minutes_from) + - [date::sub_seconds_from()](#datesub_seconds_from) + - [date::sub_days()](#datesub_days) + - [date::sub_months()](#datesub_months) + - [date::sub_years()](#datesub_years) + - [date::sub_weeks()](#datesub_weeks) + - [date::sub_hours()](#datesub_hours) + - [date::sub_minutes()](#datesub_minutes) + - [date::sub_seconds()](#datesub_seconds) + - [date::format()](#dateformat) + - [date::human_readable_seconds()](#datehuman_readable_seconds) - [File](#file) - [file::make_temp_file()](#filemake_temp_file) - [file::name()](#filename) @@ -209,6 +242,868 @@ c - Random item out of the array. +## Date + +Functions for manipulating dates. + +### date::now() + +Get current time in unix timestamp. + +#### Example + +```bash +echo "$(date::now)" +#Output +1591554426 +``` + +*Function has no arguments.* + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- current timestamp. + +### date::epoc() + +convert datetime string to unix timestamp. + +#### Example + +```bash +echo "$(date::epoc "2020-07-07 18:38")" +#Output +1594143480 +``` + +#### Arguments + +- **$1** (string): date time in any format. + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp for specified datetime. + +### date::add_days_from() + +Add number of days from specified timestamp. +If number of days not specified then it defaults to 1 day. + +#### Example + +```bash +echo "$(date::add_days_from "1594143480")" +#Output +1594229880 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of days (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::add_months_from() + +Add number of months from specified timestamp. +If number of months not specified then it defaults to 1 month. + +#### Example + +```bash +echo "$(date::add_months_from "1594143480")" +#Output +1596821880 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of months (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::add_years_from() + +Add number of years from specified timestamp. +If number of years not specified then it defaults to 1 year. + +#### Example + +```bash +echo "$(date::add_years_from "1594143480")" +#Output +1625679480 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of years (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::add_weeks_from() + +Add number of weeks from specified timestamp. +If number of weeks not specified then it defaults to 1 week. + +#### Example + +```bash +echo "$(date::add_weeks_from "1594143480")" +#Output +1594748280 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of weeks (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::add_hours_from() + +Add number of hours from specified timestamp. +If number of hours not specified then it defaults to 1 hour. + +#### Example + +```bash +echo "$(date::add_hours_from "1594143480")" +#Output +1594147080 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of hours (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::add_minutes_from() + +Add number of minutes from specified timestamp. +If number of minutes not specified then it defaults to 1 minute. + +#### Example + +```bash +echo "$(date::add_minutes_from "1594143480")" +#Output +1594143540 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of minutes (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::add_seconds_from() + +Add number of seconds from specified timestamp. +If number of seconds not specified then it defaults to 1 second. + +#### Example + +```bash +echo "$(date::add_seconds_from "1594143480")" +#Output +1594143481 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of seconds (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::add_days() + +Add number of days from current day timestamp. +If number of days not specified then it defaults to 1 day. + +#### Example + +```bash +echo "$(date::add_days "1")" +#Output +1591640826 +``` + +#### Arguments + +- **$1** (int): number of days (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::add_months() + +Add number of months from current day timestamp. +If number of months not specified then it defaults to 1 month. + +#### Example + +```bash +echo "$(date::add_months "1")" +#Output +1594146426 +``` + +#### Arguments + +- **$1** (int): number of months (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::add_years() + +Add number of years from current day timestamp. +If number of years not specified then it defaults to 1 year. + +#### Example + +```bash +echo "$(date::add_years "1")" +#Output +1623090426 +``` + +#### Arguments + +- **$1** (int): number of years (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::add_weeks() + +Add number of weeks from current day timestamp. +If number of weeks not specified then it defaults to 1 year. + +#### Example + +```bash +echo "$(date::add_weeks "1")" +#Output +1592159226 +``` + +#### Arguments + +- **$1** (int): number of weeks (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::add_hours() + +Add number of hours from current day timestamp. +If number of hours not specified then it defaults to 1 hour. + +#### Example + +```bash +echo "$(date::add_hours "1")" +#Output +1591558026 +``` + +#### Arguments + +- **$1** (int): number of hours (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::add_minutes() + +Add number of minutes from current day timestamp. +If number of minutes not specified then it defaults to 1 minute. + +#### Example + +```bash +echo "$(date::add_minutes "1")" +#Output +1591554486 +``` + +#### Arguments + +- **$2** (int): number of minutes (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::add_seconds() + +Add number of seconds from current day timestamp. +If number of seconds not specified then it defaults to 1 second. + +#### Example + +```bash +echo "$(date::add_seconds "1")" +#Output +1591554427 +``` + +#### Arguments + +- **$2** (int): number of seconds (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::sub_days_from() + +Subtract number of days from specified timestamp. +If number of days not specified then it defaults to 1 day. + +#### Example + +```bash +echo "$(date::sub_days_from "1594143480")" +#Output +1594057080 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of days (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::sub_months_from() + +Subtract number of months from specified timestamp. +If number of months not specified then it defaults to 1 month. + +#### Example + +```bash +echo "$(date::sub_months_from "1594143480")" +#Output +1591551480 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of months (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::sub_years_from() + +Subtract number of years from specified timestamp. +If number of years not specified then it defaults to 1 year. + +#### Example + +```bash +echo "$(date::sub_years_from "1594143480")" +#Output +1562521080 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of years (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::sub_weeks_from() + +Subtract number of weeks from specified timestamp. +If number of weeks not specified then it defaults to 1 week. + +#### Example + +```bash +echo "$(date::sub_weeks_from "1594143480")" +#Output +1593538680 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of weeks (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::sub_hours_from() + +Subtract number of hours from specified timestamp. +If number of hours not specified then it defaults to 1 hour. + +#### Example + +```bash +echo "$(date::sub_hours_from "1594143480")" +#Output +1594139880 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of hours (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::sub_minutes_from() + +Subtract number of minutes from specified timestamp. +If number of minutes not specified then it defaults to 1 minute. + +#### Example + +```bash +echo "$(date::sub_minutes_from "1594143480")" +#Output +1594143420 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of minutes (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::sub_seconds_from() + +Subtract number of seconds from specified timestamp. +If number of seconds not specified then it defaults to 1 second. + +#### Example + +```bash +echo "$(date::sub_seconds_from "1594143480")" +#Output +1594143479 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (int): number of seconds (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. +- **2**: Function missing arguments. + +#### Output on stdout + +- timestamp. + +### date::sub_days() + +Subtract number of days from current day timestamp. +If number of days not specified then it defaults to 1 day. + +#### Example + +```bash +echo "$(date::sub_days "1")" +#Output +1588876026 +``` + +#### Arguments + +- **$1** (int): number of days (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::sub_months() + +Subtract number of months from current day timestamp. +If number of months not specified then it defaults to 1 month. + +#### Example + +```bash +echo "$(date::sub_months "1")" +#Output +1559932026 +``` + +#### Arguments + +- **$1** (int): number of months (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::sub_years() + +Subtract number of years from current day timestamp. +If number of years not specified then it defaults to 1 year. + +#### Example + +```bash +echo "$(date::sub_years "1")" +#Output +1591468026 +``` + +#### Arguments + +- **$1** (int): number of years (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::sub_weeks() + +Subtract number of weeks from current day timestamp. +If number of weeks not specified then it defaults to 1 week. + +#### Example + +```bash +echo "$(date::sub_weeks "1")" +#Output +1590949626 +``` + +#### Arguments + +- **$1** (int): number of weeks (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::sub_hours() + +Subtract number of hours from current day timestamp. +If number of hours not specified then it defaults to 1 hour. + +#### Example + +```bash +echo "$(date::sub_hours "1")" +#Output +1591550826 +``` + +#### Arguments + +- **$1** (int): number of hours (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::sub_minutes() + +Subtract number of minutes from current day timestamp. +If number of minutes not specified then it defaults to 1 minute. + +#### Example + +```bash +echo "$(date::sub_minutes "1")" +#Output +1591554366 +``` + +#### Arguments + +- **$1** (int): number of minutes (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::sub_seconds() + +Subtract number of seconds from current day timestamp. +If number of seconds not specified then it defaults to 1 second. + +#### Example + +```bash +echo "$(date::sub_seconds "1")" +#Output +1591554425 +``` + +#### Arguments + +- **$1** (int): number of seconds (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate timestamp. + +#### Output on stdout + +- timestamp. + +### date::format() + +Format unix timestamp to human readable format. +If format string is not specified then it defaults to "yyyy-mm-dd hh:mm:ss" format. + +#### Example + +```bash +echo echo "$(date::format "1594143480")" +#Output +2020-07-07 18:38:00 +``` + +#### Arguments + +- **$1** (int): unix timestamp. +- **$2** (string): format control characters based on `date` command (optional). + +#### Exit codes + +- **0**: If successful. +- **1**: If unable to generate time string. +- **2**: Function missing arguments. + +#### Output on stdout + +- formatted time string. + +### date::human_readable_seconds() + +Format seconds to human readable format. + +#### Example + +```bash +echo "$(date::human_readable_seconds "356786")" +#Output +4 days 3 hrs 6 minute(s) and 26 seconds +``` + +#### Arguments + +- **$1** (int): number of seconds. + +#### Exit codes + +- **0**: If successful. +- **2**: Function missing arguments. + +#### Output on stdout + +- formatted time string. + ## File Functions for handling files. diff --git a/bash_utilities.sh b/bash_utilities.sh index 71e3e3b..358863c 100644 --- a/bash_utilities.sh +++ b/bash_utilities.sh @@ -5,3 +5,4 @@ source src/string.sh source src/variable.sh source src/file.sh source src/misc.sh +source src/date.sh diff --git a/bin/generate_readme.sh b/bin/generate_readme.sh index 384d5e8..533e474 100755 --- a/bin/generate_readme.sh +++ b/bin/generate_readme.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +#https://github.com/bkrem/make-toc.sh/blob/master/make-toc.sh +#https://gitlab.com/pedrolab/doctoc.sh/-/blob/master/doctoc.sh _usage() { printf " Script to autogenerate markdown based on bash source code.\n diff --git a/src/date.sh b/src/date.sh new file mode 100644 index 0000000..255f99d --- /dev/null +++ b/src/date.sh @@ -0,0 +1,769 @@ +#!/usr/bin/env bash + +# @file Date +# @brief Functions for manipulating dates. + +# @description Get current time in unix timestamp. +# +# @example +# echo "$(date::now)" +# #Output +# 1591554426 +# +# @noargs +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout current timestamp. +date::now() { + declare now + now="$(date --universal +%s)" || return $? + printf "%s" "${now}" +} + +# @description convert datetime string to unix timestamp. +# +# @example +# echo "$(date::epoc "2020-07-07 18:38")" +# #Output +# 1594143480 +# +# @arg $1 string date time in any format. +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp for specified datetime. +date::epoc() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare input date + date=$(date -d "${1}" +"%s") || return $? + printf "%s" "${date}" +} + +# @description Add number of days from specified timestamp. +# If number of days not specified then it defaults to 1 day. +# +# @example +# echo "$(date::add_days_from "1594143480")" +# #Output +# 1594229880 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of days (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::add_days_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp day + timestamp="${1}" + day=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T')+${day} day" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of months from specified timestamp. +# If number of months not specified then it defaults to 1 month. +# +# @example +# echo "$(date::add_months_from "1594143480")" +# #Output +# 1596821880 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of months (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::add_months_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp month + timestamp="${1}" + month=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T')+${month} month" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of years from specified timestamp. +# If number of years not specified then it defaults to 1 year. +# +# @example +# echo "$(date::add_years_from "1594143480")" +# #Output +# 1625679480 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of years (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::add_years_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp year + timestamp="${1}" + year=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T')+${year} year" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of weeks from specified timestamp. +# If number of weeks not specified then it defaults to 1 week. +# +# @example +# echo "$(date::add_weeks_from "1594143480")" +# #Output +# 1594748280 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of weeks (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::add_weeks_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp week + timestamp="${1}" + week=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T')+${week} week" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of hours from specified timestamp. +# If number of hours not specified then it defaults to 1 hour. +# +# @example +# echo "$(date::add_hours_from "1594143480")" +# #Output +# 1594147080 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of hours (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::add_hours_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp hour + timestamp="${1}" + hour=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T')+${hour} hour" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of minutes from specified timestamp. +# If number of minutes not specified then it defaults to 1 minute. +# +# @example +# echo "$(date::add_minutes_from "1594143480")" +# #Output +# 1594143540 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of minutes (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::add_minutes_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp minute + timestamp="${1}" + minute=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T')+${minute} minute" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of seconds from specified timestamp. +# If number of seconds not specified then it defaults to 1 second. +# +# @example +# echo "$(date::add_seconds_from "1594143480")" +# #Output +# 1594143481 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of seconds (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::add_seconds_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp minute + timestamp="${1}" + second=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T')+${second} second" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of days from current day timestamp. +# If number of days not specified then it defaults to 1 day. +# +# @example +# echo "$(date::add_days "1")" +# #Output +# 1591640826 +# +# @arg $1 int number of days (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::add_days() { + declare timestamp new_timestamp day + timestamp="$(date::now)" + day=${1:-1} + new_timestamp="$(date::add_days_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of months from current day timestamp. +# If number of months not specified then it defaults to 1 month. +# +# @example +# echo "$(date::add_months "1")" +# #Output +# 1594146426 +# +# @arg $1 int number of months (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::add_months() { + declare timestamp new_timestamp month + timestamp="$(date::now)" + month=${1:-1} + new_timestamp="$(date::add_months_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of years from current day timestamp. +# If number of years not specified then it defaults to 1 year. +# +# @example +# echo "$(date::add_years "1")" +# #Output +# 1623090426 +# +# @arg $1 int number of years (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::add_years() { + declare timestamp new_timestamp year + timestamp="$(date::now)" + year=${1:-1} + new_timestamp="$(date::add_years_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of weeks from current day timestamp. +# If number of weeks not specified then it defaults to 1 year. +# +# @example +# echo "$(date::add_weeks "1")" +# #Output +# 1592159226 +# +# @arg $1 int number of weeks (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::add_weeks() { + declare timestamp new_timestamp week + timestamp="$(date::now)" + week=${1:-1} + new_timestamp="$(date::add_weeks_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of hours from current day timestamp. +# If number of hours not specified then it defaults to 1 hour. +# +# @example +# echo "$(date::add_hours "1")" +# #Output +# 1591558026 +# +# @arg $1 int number of hours (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::add_hours() { + declare timestamp new_timestamp hour + timestamp="$(date::now)" + hour=${1:-1} + new_timestamp="$(date::add_hours_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of minutes from current day timestamp. +# If number of minutes not specified then it defaults to 1 minute. +# +# @example +# echo "$(date::add_minutes "1")" +# #Output +# 1591554486 +# +# @arg $2 int number of minutes (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::add_minutes() { + declare timestamp new_timestamp minute + timestamp="$(date::now)" + minute=${1:-1} + new_timestamp="$(date::add_minutes_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Add number of seconds from current day timestamp. +# If number of seconds not specified then it defaults to 1 second. +# +# @example +# echo "$(date::add_seconds "1")" +# #Output +# 1591554427 +# +# @arg $2 int number of seconds (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::add_seconds() { + declare timestamp new_timestamp minute + timestamp="$(date::now)" + second=${1:-1} + new_timestamp="$(date::add_seconds_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of days from specified timestamp. +# If number of days not specified then it defaults to 1 day. +# +# @example +# echo "$(date::sub_days_from "1594143480")" +# #Output +# 1594057080 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of days (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::sub_days_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp day + timestamp="${1}" + day=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T') ${day} days ago" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of months from specified timestamp. +# If number of months not specified then it defaults to 1 month. +# +# @example +# echo "$(date::sub_months_from "1594143480")" +# #Output +# 1591551480 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of months (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::sub_months_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp month + timestamp="${1}" + month=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T') ${month} months ago" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of years from specified timestamp. +# If number of years not specified then it defaults to 1 year. +# +# @example +# echo "$(date::sub_years_from "1594143480")" +# #Output +# 1562521080 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of years (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::sub_years_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp year + timestamp="${1}" + year=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T') ${year} years ago" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of weeks from specified timestamp. +# If number of weeks not specified then it defaults to 1 week. +# +# @example +# echo "$(date::sub_weeks_from "1594143480")" +# #Output +# 1593538680 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of weeks (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::sub_weeks_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp week + timestamp="${1}" + week=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T') ${week} weeks ago" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of hours from specified timestamp. +# If number of hours not specified then it defaults to 1 hour. +# +# @example +# echo "$(date::sub_hours_from "1594143480")" +# #Output +# 1594139880 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of hours (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::sub_hours_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp hour + timestamp="${1}" + hour=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T') ${hour} hours ago" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of minutes from specified timestamp. +# If number of minutes not specified then it defaults to 1 minute. +# +# @example +# echo "$(date::sub_minutes_from "1594143480")" +# #Output +# 1594143420 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of minutes (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::sub_minutes_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp minute + timestamp="${1}" + minute=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T') ${minute} minutes ago" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of seconds from specified timestamp. +# If number of seconds not specified then it defaults to 1 second. +# +# @example +# echo "$(date::sub_seconds_from "1594143480")" +# #Output +# 1594143479 +# +# @arg $1 int unix timestamp. +# @arg $2 int number of seconds (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# @exitcode 2 Function missing arguments. +# +# @stdout timestamp. +date::sub_seconds_from() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp new_timestamp minute + timestamp="${1}" + second=${2:-1} + new_timestamp="$(date -d "$(date -d "@${timestamp}" '+%F %T') ${second} seconds ago" +'%s')" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of days from current day timestamp. +# If number of days not specified then it defaults to 1 day. +# +# @example +# echo "$(date::sub_days "1")" +# #Output +# 1588876026 +# +# @arg $1 int number of days (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::sub_days() { + declare timestamp new_timestamp day + timestamp="$(date::now)" + day=${1:-1} + new_timestamp="$(date::sub_days_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of months from current day timestamp. +# If number of months not specified then it defaults to 1 month. +# +# @example +# echo "$(date::sub_months "1")" +# #Output +# 1559932026 +# +# @arg $1 int number of months (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::sub_months() { + declare timestamp new_timestamp month + timestamp="$(date::now)" + month=${1:-1} + new_timestamp="$(date::sub_months_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of years from current day timestamp. +# If number of years not specified then it defaults to 1 year. +# +# @example +# echo "$(date::sub_years "1")" +# #Output +# 1591468026 +# +# @arg $1 int number of years (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::sub_years() { + declare timestamp new_timestamp year + timestamp="$(date::now)" + year=${1:-1} + new_timestamp="$(date::sub_years_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of weeks from current day timestamp. +# If number of weeks not specified then it defaults to 1 week. +# +# @example +# echo "$(date::sub_weeks "1")" +# #Output +# 1590949626 +# +# @arg $1 int number of weeks (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::sub_weeks() { + declare timestamp new_timestamp week + timestamp="$(date::now)" + week=${1:-1} + new_timestamp="$(date::sub_weeks_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of hours from current day timestamp. +# If number of hours not specified then it defaults to 1 hour. +# +# @example +# echo "$(date::sub_hours "1")" +# #Output +# 1591550826 +# +# @arg $1 int number of hours (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::sub_hours() { + declare timestamp new_timestamp hour + timestamp="$(date::now)" + hour=${1:-1} + new_timestamp="$(date::sub_hours_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of minutes from current day timestamp. +# If number of minutes not specified then it defaults to 1 minute. +# +# @example +# echo "$(date::sub_minutes "1")" +# #Output +# 1591554366 +# +# @arg $1 int number of minutes (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::sub_minutes() { + declare timestamp new_timestamp minute + timestamp="$(date::now)" + minute=${1:-1} + new_timestamp="$(date::sub_minutes_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Subtract number of seconds from current day timestamp. +# If number of seconds not specified then it defaults to 1 second. +# +# @example +# echo "$(date::sub_seconds "1")" +# #Output +# 1591554425 +# +# @arg $1 int number of seconds (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate timestamp. +# +# @stdout timestamp. +date::sub_seconds() { + declare timestamp new_timestamp minute + timestamp="$(date::now)" + second=${1:-1} + new_timestamp="$(date::sub_seconds_from "${timestamp}" "${second}")" || return $? + printf "%s" "${new_timestamp}" +} + +# @description Format unix timestamp to human readable format. +# If format string is not specified then it defaults to "yyyy-mm-dd hh:mm:ss" format. +# +# @example +# echo echo "$(date::format "1594143480")" +# #Output +# 2020-07-07 18:38:00 +# +# @arg $1 int unix timestamp. +# @arg $2 string format control characters based on `date` command (optional). +# +# @exitcode 0 If successful. +# @exitcode 1 If unable to generate time string. +# @exitcode 2 Function missing arguments. +# +# @stdout formatted time string. +date::format() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare timestamp format out + timestamp="${1}" + format="${2:-"%F %T"}" + out="$(date -d "@${timestamp}" +"${format}")" || return $? + printf "%s" "${out}" + +} + +# @description Format seconds to human readable format. +# +# @example +# echo "$(date::human_readable_seconds "356786")" +# #Output +# 4 days 3 hrs 6 minute(s) and 26 seconds +# +# @arg $1 int number of seconds. +# +# @exitcode 0 If successful. +# @exitcode 2 Function missing arguments. +# +# @stdout formatted time string. +date::human_readable_seconds() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare T="${1}" + declare DAY="$((T / 60 / 60 / 24))" HR="$((T / 60 / 60 % 24))" MIN="$((T / 60 % 60))" SEC="$((T % 60))" + [[ ${DAY} -gt 0 ]] && printf '%d days ' "${DAY}" + [[ ${HR} -gt 0 ]] && printf '%d hrs ' "${HR}" + [[ ${MIN} -gt 0 ]] && printf '%d minute(s) ' "${MIN}" + [[ ${DAY} -gt 0 || ${HR} -gt 0 || ${MIN} -gt 0 ]] && printf 'and ' + printf '%d seconds\n' "${SEC}" +} diff --git a/src/file.sh b/src/file.sh index 9ad526c..8879ede 100644 --- a/src/file.sh +++ b/src/file.sh @@ -25,7 +25,7 @@ file::make_temp_file() { } # @description Get only the filename from string path. - +# # @example # echo "$(file::name "/path/to/test.md")" # #Output @@ -43,7 +43,7 @@ file::name() { } # @description Get the basename of file from file name. - +# # @example # echo "$(file::basename "/path/to/test.md")" # #Output