Skip to content

Commit

Permalink
add timestampToDate(InUserFormat)
Browse files Browse the repository at this point in the history
  • Loading branch information
robstoll committed Jan 24, 2025
1 parent 1786453 commit 550f422
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1447,9 +1447,16 @@ source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
sourceOnce "$dir_of_tegonal_scripts/utility/date-utils.sh"
# converts the unix timestamp to a date in format Y-m-dTH:M:S
# converts the unix timestamp to a date with time in format Y-m-dTH:M:S
timestampToDateTime 1662981524 # outputs 2022-09-12T13:18:44
# converts the unix timestamp to a date in format Y-m-d
timestampToDate 1662981524 # outputs 2022-09-12
# converts the unix timestamp to a date in format as defined by LC_TIME
# (usually as defined by the user in the system settings)
timestampToDateInUserFormat 1662981524 # outputs 12.09.2022 for ch_DE
dateToTimestamp "2024-03-01" # outputs 1709247600
dateToTimestamp "2022-09-12T13:18:44" # outputs 1662981524
```
Expand Down
9 changes: 8 additions & 1 deletion src/utility/date-utils.doc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"

sourceOnce "$dir_of_tegonal_scripts/utility/date-utils.sh"

# converts the unix timestamp to a date in format Y-m-dTH:M:S
# converts the unix timestamp to a date with time in format Y-m-dTH:M:S
timestampToDateTime 1662981524 # outputs 2022-09-12T13:18:44

# converts the unix timestamp to a date in format Y-m-d
timestampToDate 1662981524 # outputs 2022-09-12

# converts the unix timestamp to a date in format as defined by LC_TIME
# (usually as defined by the user in the system settings)
timestampToDateInUserFormat 1662981524 # outputs 12.09.2022 for ch_DE

dateToTimestamp "2024-03-01" # outputs 1709247600
dateToTimestamp "2022-09-12T13:18:44" # outputs 1662981524

27 changes: 25 additions & 2 deletions src/utility/date-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
####### Description #############
#
# utility functions for dealing with date
# utility functions for dealing with date(-time) and unix timestamps
#
####### Usage ###################
#
Expand All @@ -25,9 +25,16 @@
#
# sourceOnce "$dir_of_tegonal_scripts/utility/date-utils.sh"
#
# # converts the unix timestamp to a date in format Y-m-dTH:M:S
# # converts the unix timestamp to a date with time in format Y-m-dTH:M:S
# timestampToDateTime 1662981524 # outputs 2022-09-12T13:18:44
#
# # converts the unix timestamp to a date in format Y-m-d
# timestampToDate 1662981524 # outputs 2022-09-12
#
# # converts the unix timestamp to a date in format as defined by LC_TIME
# # (usually as defined by the user in the system settings)
# timestampToDateInUserFormat 1662981524 # outputs 12.09.2022 for ch_DE
#
# dateToTimestamp "2024-03-01" # outputs 1709247600
# dateToTimestamp "2022-09-12T13:18:44" # outputs 1662981524
#
Expand All @@ -50,6 +57,22 @@ function timestampToDateTime() {
date -d "@$timestamp" +"%Y-%m-%dT%H:%M:%S"
}

function timestampToDate() {
local timestamp
# shellcheck disable=SC2034 # is passed by name to parseFnArgs
local -ra params=(timestamp)
parseFnArgs params "$@"
date -d "@$timestamp" +"%Y-%m-%d"
}

function timestampToDateInUserFormat() {
local timestamp
# shellcheck disable=SC2034 # is passed by name to parseFnArgs
local -ra params=(timestamp)
parseFnArgs params "$@"
date -d "@$timestamp" +"%x"
}

function dateToTimestamp() {
local dateAsString
# shellcheck disable=SC2034 # is passed by name to parseFnArgs
Expand Down
2 changes: 1 addition & 1 deletion src/utility/replace-help-snippet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function replaceHelpSnippet() {

local snippet cleanedUpSnippet markdownSnippet
snippet=$("$script" "${varargs[@]}") || true
# remove ansi colour codes form snippet
# remove ansi colour codes from snippet
cleanedUpSnippet=$(perl -0777 -pe "s/\033\[([01];\d{2}|0)m//g" <<<"$snippet") || die "could not quote snippet for %s" "$script"
markdownSnippet=$(printf "\`\`\`text\n%s\n\`\`\`" "$cleanedUpSnippet") || die "could not create markdownSnippet for %s" "$script"

Expand Down

0 comments on commit 550f422

Please sign in to comment.