Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # |
| 4 | # Copyright (c) 2015 Wind River Systems, Inc. |
| 5 | # |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 6 | # SPDX-License-Identifier: Apache-2.0 |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 7 | # |
| 8 | |
| 9 | exe_name=$(basename $0) |
| 10 | |
| 11 | # outputs the date and time in pre-set formats |
| 12 | # default format is: 20150114-181112 |
| 13 | # usage: timestamp [-a] [-d] [-u] [-s] [-S] |
| 14 | # where: -a changes default to: 2015-01-14-18-11-56 |
| 15 | # -d changes default to: 20150114 |
| 16 | # -u changes default to: 20150114_181201 |
| 17 | # -s changes default to: 20150114-1812.04 |
| 18 | # -S changes default to: 20150114-1812 |
| 19 | # Some switches can be mixed and matched, eg. -Sa gives 2015-01-14-18-13 |
| 20 | |
| 21 | date_format="%Y%m%d" |
| 22 | time_format="%H%M" |
| 23 | seconds_format="%S" |
| 24 | seconds_separator="" |
| 25 | date_time_separator="-" |
| 26 | |
| 27 | function usage { |
| 28 | printf "usage: %s [-a] [-d] [-u] [-s] [-S]\n" $exe_name >&2 |
| 29 | } |
| 30 | |
| 31 | function fail { |
| 32 | usage |
| 33 | exit -1 |
| 34 | } |
| 35 | |
| 36 | function get_opts { |
| 37 | declare -r optstr="adusSh" |
| 38 | while getopts ${optstr} opt; do |
| 39 | case ${opt} in |
| 40 | a) all_separated=1 ;; |
| 41 | d) date_only=1 ;; |
| 42 | u) date_time_separator="_" ;; |
| 43 | s) seconds_separator="." ;; |
| 44 | S) no_seconds=1 ;; |
| 45 | h) usage; exit 0 ;; |
| 46 | *) fail ;; |
| 47 | esac |
| 48 | done |
| 49 | } |
| 50 | |
| 51 | get_opts $@ |
| 52 | |
| 53 | if [ x${all_separated} == x1 ]; then |
| 54 | date_format="%Y-%m-%d" |
| 55 | time_format="%H-%M" |
| 56 | seconds_separator="-" |
| 57 | fi |
| 58 | |
| 59 | if [ x${date_only} == x1 ]; then |
| 60 | date_time_separator="" |
| 61 | time_format="" |
| 62 | seconds_format="" |
| 63 | seconds_separator="" |
| 64 | fi |
| 65 | |
| 66 | if [ x${no_seconds} == x1 ]; then |
| 67 | seconds_format="" |
| 68 | seconds_separator="" |
| 69 | fi |
| 70 | |
| 71 | output_date=${date_format}${date_time_separator} |
| 72 | output_time=${time_format}${seconds_separator}${seconds_format} |
| 73 | output=$(date +${output_date}${output_time}) |
| 74 | |
| 75 | echo ${output} |