|
29 | 29 | # Random generation
|
30 | 30 | # --------------------------------------------
|
31 | 31 | # 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] |
34 | 36 | # 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)]]] |
38 | 41 | # 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" |
43 | 46 | # 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). |
50 | 52 | 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 |
54 | 55 | 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 |
57 | 58 | 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]' |
59 | 60 | regx+='{1,2}(,[0-9]{1,2},[0-9]{1,2})?)?)?$'
|
60 | 61 | if [[ ! "${1}" =~ ${regx} ]]; then
|
61 | 62 | 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 |
65 | 66 | return 1
|
66 | 67 | fi
|
67 | 68 | local vals=(${1//,/ })
|
|
74 | 75 | [ "${#vals[@]}" -gt 4 ] && ENT+=("${vals[5]}") || ENT+=('31')
|
75 | 76 | [ "${#vals[@]}" -gt 6 ] && ENT+=("${vals[6]}") || ENT+=('0')
|
76 | 77 | [ "${#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 |
79 | 80 | echo 'generateRandomDate: MM(min) and MM(max) must be in the range 1 <='\
|
80 | 81 | 'x <= 12' 1>&2
|
81 | 82 | 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 <='\ |
85 | 86 | 'x <= 31' 1>&2
|
86 | 87 | return 1
|
87 | 88 | elif [ "${ENT[6]}" -ge 24 ] || [ "${ENT[7]}" -ge 24 ]; then
|
88 | 89 | echo 'generateRandomDate: hh(min) and hh(max) must be less than 24' 1>&2
|
89 | 90 | return 1
|
90 | 91 | elif [ "${ENT[0]}" -gt "${ENT[1]}" ] || [ "${ENT[2]}" -gt "${ENT[3]}" ] || \
|
91 | 92 | [ "${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 |
94 | 95 | return 1
|
95 | 96 | fi
|
96 |
| - |
97 | 97 | local year="$(printf "%02d" "$(shuf -i "${ENT[0]}"-"${ENT[1]}" -n 1)")"
|
98 | 98 | local month="$(printf "%02d" "$(shuf -i "${ENT[2]}"-"${ENT[3]}" -n 1)")"
|
99 | 99 | local day="$(printf "%02d" "$(shuf -i "${ENT[4]}"-"${ENT[5]}" -n 1)")"
|
|
105 | 105 | local minute="$(printf "%02d" "$(shuf -i 0-59 -n 1)")"
|
106 | 106 | local second="$(printf "%02d" "$(shuf -i 0-59 -n 1)")"
|
107 | 107 | 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}" |
111 | 115 | else
|
112 |
| - local out="${DFLT_CENTURY_PREFIX}${year}-${month}-${day}T${hour}" |
113 |
| - out+=":${minute}:${second}.${nanosecond}" |
| 116 | + echo "${iso8601Date}" |
114 | 117 | fi
|
115 |
| - exec >&3 2>&4 |
116 |
| - echo "${out}" |
117 | 118 | }
|
118 | 119 |
|
0 commit comments