1#!/usr/bin/env bash
2
3#
4# Copyright (c) 2015 Wind River Systems, Inc.
5#
6# SPDX-License-Identifier: Apache-2.0
7#
8
9exe_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
21date_format="%Y%m%d"
22time_format="%H%M"
23seconds_format="%S"
24seconds_separator=""
25date_time_separator="-"
26
27function usage {
28	printf "usage: %s [-a] [-d] [-u] [-s] [-S]\n" $exe_name >&2
29}
30
31function fail {
32	usage
33	exit -1
34}
35
36function 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
51get_opts $@
52
53if [ x${all_separated} == x1 ]; then
54	date_format="%Y-%m-%d"
55	time_format="%H-%M"
56	seconds_separator="-"
57fi
58
59if [ x${date_only} == x1 ]; then
60	date_time_separator=""
61	time_format=""
62	seconds_format=""
63	seconds_separator=""
64fi
65
66if [ x${no_seconds} == x1 ]; then
67	seconds_format=""
68	seconds_separator=""
69fi
70
71output_date=${date_format}${date_time_separator}
72output_time=${time_format}${seconds_separator}${seconds_format}
73output=$(date +${output_date}${output_time})
74
75echo ${output}
76