Skip to content

Commit acfe39b

Browse files
author
adrianbartyczak
committed
Remove DFLT_CENTURY_PREFIX and add Unix timestamp option in function generateRandomDate
1 parent 3fd12d8 commit acfe39b

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

functions_scripts/programming_languages/bash/dateandtimeutils.bash

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,40 @@
2929
# Random generation
3030
# --------------------------------------------
3131
# Generate a random date between two dates specified by year and optionally
32-
# month, day of month and hour and output in ISO 8601 format (e.g.
33-
# "2004-04-25T16:18:13.489392193").
32+
# month, day of month and hour and output as one of the following:
33+
# -A date in ISO-8601 format (e.g. 2004-04-25T16:18:13.489392193; default)
34+
# -A Unix timestamp in seconds
35+
# -A timestamp in the fomat [[CC]YY]MMDDhhmm[.ss]
3436
# Usage:
35-
# generateRandomDate <min and max date entities>
36-
# min and max date entities: format in "YY(min),YY(max),[MM(min),
37-
# MM(max),[DD(min),DD(max),[hh(min),hh(max)]]]"
37+
# generateRandomDate <minimum/maximum date entities>
38+
# <minimum/maximum date entities> format:
39+
# yyyy(min),yyyy(max)[,MM(min),MM(max)[,dd(min),dd(max)[,hh(min),
40+
# hh(max)]]]
3841
# Examples:
39-
# generateRandomDate '05,09,04,11,15,31'
40-
# # "2007-04-25T16:18:13.489392193"
41-
# generateRandomDate '42,56,09,10,03,08,19,22'
42-
# # "2045-10-07T22:33:42.730192851"
42+
# generateRandomDate '1942,1956,09,10,03,08,19,22'
43+
# # output: "1945-10-06T21:33:42.730192851"
44+
# generateRandomDate -u '2005,2009,04,11,15,31'
45+
# # output: "1227236576"
4346
# Options:
44-
# -t get date as timestamp in the fomat [[CC]YY]MMDDhhmm[.ss]
45-
# Notes:
46-
# -All generated years are within the century defined by
47-
# DFLT_CENTURY_PREFIX.
48-
# -Each date entity is inclusive of the previous entity (e.g. if the max
49-
# hour is 15, the max time with minutes can be 15:59).
47+
# -u return a Unix timestamp in seconds
48+
# -t return a timestamp in the fomat [[CC]YY]MMDDhhmm[.ss]
49+
# Note:
50+
# The time of each date entity is inclusive of the previous entity (e.g. if
51+
# the maximum hour is 15, the maximum time in hours and minutes is 15:59).
5052
generateRandomDate() {
51-
local DFLT_CENTURY_PREFIX='21'
52-
local OPT OPTIND
53-
getopts :t OPT
53+
local OPT OPTIND OPTS
54+
while getopts :ut OPT; do OPTS+="${OPT}"; done
5455
shift $((OPTIND - 1))
55-
# redirect all output to >/dev/tty so errors will be printed to the terminal
56-
# when this function is used in a subprocess.
56+
# redirect all output to >/dev/tty to print errors to the terminal when this
57+
# function is used in a subprocess
5758
exec 3>&1 4>&2; exec >/dev/tty 2>&1
58-
local regx='^[0-9]{1,2},[0-9]{1,2}(,[0-9]{1,2},[0-9]{1,2}(,[0-9]{1,2},[0-9]'
59+
local regx='^[0-9]{1,4},[0-9]{1,4}(,[0-9]{1,2},[0-9]{1,2}(,[0-9]{1,2},[0-9]'
5960
regx+='{1,2}(,[0-9]{1,2},[0-9]{1,2})?)?)?$'
6061
if [[ ! "${1}" =~ ${regx} ]]; then
6162
local errMsg='generateRandomDate: first argument must be in the format'
62-
errMsg+=' YY(min),YY(max),[MM(min),MM(max),[DD(min),DD(max),[hh(min),'
63-
errMsg+='hh(max)]]]'
64-
echo "${errMsg}" 1>&2
63+
errMsg+=' yyyy(min),yyyy(max)[,\n MM(min),MM(max)[,dd(min),dd(max)[,'
64+
errMsg+='hh(min),hh(max)]]]'
65+
echo -e "${errMsg}" 1>&2
6566
return 1
6667
fi
6768
local vals=(${1//,/ })
@@ -74,26 +75,25 @@
7475
[ "${#vals[@]}" -gt 4 ] && ENT+=("${vals[5]}") || ENT+=('31')
7576
[ "${#vals[@]}" -gt 6 ] && ENT+=("${vals[6]}") || ENT+=('0')
7677
[ "${#vals[@]}" -gt 6 ] && ENT+=("${vals[7]}") || ENT+=('23')
77-
if [ "${ENT[2]}" -gt 12 ] || [ "${ENT[3]}" -gt 12 ] || [ "${ENT[2]}" -eq 0 \
78-
] || [ "${ENT[3]}" -eq 0 ]; then
78+
if [ "${ENT[2]}" -gt 12 ] || [ "${ENT[3]}" -gt 12 ] || [ "${ENT[2]}" \
79+
-eq 0 ] || [ "${ENT[3]}" -eq 0 ]; then
7980
echo 'generateRandomDate: MM(min) and MM(max) must be in the range 1 <='\
8081
'x <= 12' 1>&2
8182
return 1
82-
elif [ "${ENT[4]}" -gt 31 ] || [ "${ENT[5]}" -gt 31 ] || [ "${ENT[4]}" -eq \
83-
0 ] || [ "${ENT[5]}" -eq 0 ]; then
84-
echo 'generateRandomDate: DD(min) and DD(max) must be in the range 1 <='\
83+
elif [ "${ENT[4]}" -gt 31 ] || [ "${ENT[5]}" -gt 31 ] || [ "${ENT[4]}" \
84+
-eq 0 ] || [ "${ENT[5]}" -eq 0 ]; then
85+
echo 'generateRandomDate: dd(min) and dd(max) must be in the range 1 <='\
8586
'x <= 31' 1>&2
8687
return 1
8788
elif [ "${ENT[6]}" -ge 24 ] || [ "${ENT[7]}" -ge 24 ]; then
8889
echo 'generateRandomDate: hh(min) and hh(max) must be less than 24' 1>&2
8990
return 1
9091
elif [ "${ENT[0]}" -gt "${ENT[1]}" ] || [ "${ENT[2]}" -gt "${ENT[3]}" ] || \
9192
[ "${ENT[4]}" -gt "${ENT[5]}" ] || [ "${ENT[6]}" -gt "${ENT[7]}" ]; then
92-
echo 'generateRandomDate: ranges must be YY(min)<=YY(max),'\
93-
'MM(min)<=MM(max), DD(min)<=DD(max), hh(min)<=hh(max)' 1>&2
93+
echo 'generateRandomDate: ranges must be yyyy(min)<=yyyy(max),'\
94+
'MM(min)<=MM(max), dd(min)<=dd(max), hh(min)<=hh(max)' 1>&2
9495
return 1
9596
fi
96-
9797
local year="$(printf "%02d" "$(shuf -i "${ENT[0]}"-"${ENT[1]}" -n 1)")"
9898
local month="$(printf "%02d" "$(shuf -i "${ENT[2]}"-"${ENT[3]}" -n 1)")"
9999
local day="$(printf "%02d" "$(shuf -i "${ENT[4]}"-"${ENT[5]}" -n 1)")"
@@ -105,14 +105,15 @@
105105
local minute="$(printf "%02d" "$(shuf -i 0-59 -n 1)")"
106106
local second="$(printf "%02d" "$(shuf -i 0-59 -n 1)")"
107107
local nanosecond="$(printf "%09d" "$(shuf -i 0-999999999 -n 1)")"
108-
if [ "${OPT}" = 't' ]; then
109-
local out="${DFLT_CENTURY_PREFIX}${year}${month}${day}${hour}${minute}"
110-
out+=".${second}"
108+
exec >&3 2>&4
109+
local iso8601Date="${year}-${month}-${day}T${hour}:${minute}:${second}"
110+
iso8601Date+=".${nanosecond}"
111+
if [ -n "${OPTS}" ] && [ -z "${OPTS##*u*}" ]; then
112+
date --utc --date="${iso8601Date}" +"%s"
113+
elif [ -n "${OPTS}" ] && [ -z "${OPTS##*t*}" ]; then
114+
echo "${year}${month}${day}${hour}${minute}.${second}"
111115
else
112-
local out="${DFLT_CENTURY_PREFIX}${year}-${month}-${day}T${hour}"
113-
out+=":${minute}:${second}.${nanosecond}"
116+
echo "${iso8601Date}"
114117
fi
115-
exec >&3 2>&4
116-
echo "${out}"
117118
}
118119

0 commit comments

Comments
 (0)