Skip to content

Commit 550f422

Browse files
committed
add timestampToDate(InUserFormat)
1 parent 1786453 commit 550f422

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,9 +1447,16 @@ source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
14471447
14481448
sourceOnce "$dir_of_tegonal_scripts/utility/date-utils.sh"
14491449
1450-
# converts the unix timestamp to a date in format Y-m-dTH:M:S
1450+
# converts the unix timestamp to a date with time in format Y-m-dTH:M:S
14511451
timestampToDateTime 1662981524 # outputs 2022-09-12T13:18:44
14521452
1453+
# converts the unix timestamp to a date in format Y-m-d
1454+
timestampToDate 1662981524 # outputs 2022-09-12
1455+
1456+
# converts the unix timestamp to a date in format as defined by LC_TIME
1457+
# (usually as defined by the user in the system settings)
1458+
timestampToDateInUserFormat 1662981524 # outputs 12.09.2022 for ch_DE
1459+
14531460
dateToTimestamp "2024-03-01" # outputs 1709247600
14541461
dateToTimestamp "2022-09-12T13:18:44" # outputs 1662981524
14551462
```

src/utility/date-utils.doc.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
1010

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

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

16+
# converts the unix timestamp to a date in format Y-m-d
17+
timestampToDate 1662981524 # outputs 2022-09-12
18+
19+
# converts the unix timestamp to a date in format as defined by LC_TIME
20+
# (usually as defined by the user in the system settings)
21+
timestampToDateInUserFormat 1662981524 # outputs 12.09.2022 for ch_DE
22+
1623
dateToTimestamp "2024-03-01" # outputs 1709247600
1724
dateToTimestamp "2022-09-12T13:18:44" # outputs 1662981524
1825

src/utility/date-utils.sh

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
####### Description #############
1111
#
12-
# utility functions for dealing with date
12+
# utility functions for dealing with date(-time) and unix timestamps
1313
#
1414
####### Usage ###################
1515
#
@@ -25,9 +25,16 @@
2525
#
2626
# sourceOnce "$dir_of_tegonal_scripts/utility/date-utils.sh"
2727
#
28-
# # converts the unix timestamp to a date in format Y-m-dTH:M:S
28+
# # converts the unix timestamp to a date with time in format Y-m-dTH:M:S
2929
# timestampToDateTime 1662981524 # outputs 2022-09-12T13:18:44
3030
#
31+
# # converts the unix timestamp to a date in format Y-m-d
32+
# timestampToDate 1662981524 # outputs 2022-09-12
33+
#
34+
# # converts the unix timestamp to a date in format as defined by LC_TIME
35+
# # (usually as defined by the user in the system settings)
36+
# timestampToDateInUserFormat 1662981524 # outputs 12.09.2022 for ch_DE
37+
#
3138
# dateToTimestamp "2024-03-01" # outputs 1709247600
3239
# dateToTimestamp "2022-09-12T13:18:44" # outputs 1662981524
3340
#
@@ -50,6 +57,22 @@ function timestampToDateTime() {
5057
date -d "@$timestamp" +"%Y-%m-%dT%H:%M:%S"
5158
}
5259

60+
function timestampToDate() {
61+
local timestamp
62+
# shellcheck disable=SC2034 # is passed by name to parseFnArgs
63+
local -ra params=(timestamp)
64+
parseFnArgs params "$@"
65+
date -d "@$timestamp" +"%Y-%m-%d"
66+
}
67+
68+
function timestampToDateInUserFormat() {
69+
local timestamp
70+
# shellcheck disable=SC2034 # is passed by name to parseFnArgs
71+
local -ra params=(timestamp)
72+
parseFnArgs params "$@"
73+
date -d "@$timestamp" +"%x"
74+
}
75+
5376
function dateToTimestamp() {
5477
local dateAsString
5578
# shellcheck disable=SC2034 # is passed by name to parseFnArgs

src/utility/replace-help-snippet.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function replaceHelpSnippet() {
8080

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

0 commit comments

Comments
 (0)