Change datetime to timestamp in bash script - bash

I have a json in file myTime
{
"beginTime": "2014-Mar-19 02:15:00",
"endTime": "2014-Mar-29 02:00:00"
}
I want to get beginTime and change it to timestamp.
I get beginTime by the following code:
beginTime=($(jq -r '.beginTime' myTime))
I replace Mar by 03 :
beginTime=($(echo "$beginTime" | sed -r 's/[Mar]+/03/g'))
I change it to time stamp :
date -d "$beginTime" "+%s"
I got 1395162000 , it mean only change 2014-03-19 because $beginTime give 2014-03-19 , first element of array.
so I tried another code
date -d "${beginTime[#]}" "+%s"
now I got
date: extra operand `+%s'
but this code is ok
date -d "2014-03-19 02:15:00" "+%s"
Could anyone help me?

Do not use array variables - you need to capture date and time as a single string:
beginTime=$(jq -r '.beginTime' myTime | sed 's/Mar/03/') # "2014-03-19 02:15:00"
Note that I've combined your first two commands into one.
(As an aside: regex /[Mar]+/ happens to work in this case, but is not what you intended - use /Mar/ instead. /[Mar]+/ matches any span of at least length 1 composed of any of the following characters in any sequence: M, a, r.)

Related

Not able to get the expected date output after adding a second

What I'm trying to do below
echo -e "input start_time(YYYYMMDDHHMMSS)"
read word1
dt= `date "--date=${word1} -s + 1sec" +%Y%m%d%H%M%S`
echo ${dt}
If input : 20181201090909 then I got an output : 20181201090909030001. The expected output is quite incorrect. I was expecting it to be 20181201090910
You need to format differently the string you pass to date:
~$ date +%Y%m%d%H%M%S --date="20181201 09:09:09 + 1sec"
20181201090910
~$
You see a space between the date and the time, and the colon separating hour minutes seconds. See info date for more information.

Create a set of files based off a date range

How would you go about doing a few lines of code in Bash to accomplish the following. I'm trying to build up my skills in Bash and learn how to handle more small tasks directory from the command line.
Steps:
Specify a start date and an end date. Load all the dates in between including the start and end date into a "list"
Loop over the list creating a file like this each time.
(requires date formatting)
2017-11-10.w
2017-11-11.w
2017-11-12.w
You could convert the input dates to Unix timestamps, then add the number of seconds per day and touch a file named after the result until you are past the end date:
#!/bin/bash
startstamp=$(date -d "$1" +'%s')
endstamp=$(date -d "$2" +'%s')
secs_per_day=$(( 24 * 3600 ))
for (( thedate = startstamp; thedate <= endstamp; thedate += secs_per_day )); do
touch "$(date -d "#$thedate" '+%F.w')"
done
The %s formatting string (a GNU extension) prints the number of seconds since the Unix epoch, and # in the argument to the -d option indicates that the date is in that format. %F is short for %Y-%m-%d, which translates to YYYY-MM-DD.
Example usage:
$ ./dates 2017-11-10 2017-11-15
$ ls -1
2017-11-10.w
2017-11-11.w
2017-11-12.w
2017-11-13.w
2017-11-14.w
2017-11-15.w
dates

Custom date verification - BASH - using just grep or awk etc

I saw couple of posts (some depends upon date -d $xyz to verify) but I'm trying to create an until loop where the user should be re-prompted to enter the value of a date format until it matches the custom date format.
My date format (what I need for Splunk) is m/d/yyyy:h:m:s or mm/dd/yyyy:hh:mm:ss
which means, if m (month number) is a single digit lets say 1 for January, then both 1 or 01 values are possible for date format but 0 or 00 is NOT a valid value. Value range is 01-to->12 or 1-to->12 but not greater than 12.
Similarly, the same rule applies to d (day number), it can be 01-to->10-to->31 or 1-to->31 but not 00 or more than 31 and all other yyyy (year), h (hour), m (minute), s (second) part.
What could be a minimal code (obfuscated is fine) to do this verification in BASH? It seems like date -d ??? doesn't provides this custom kind of verification for date/times!
OK, I can write one verifyDateFormatfunc() to do this, but I know there are people who have already written a one-liner / minimal snippet to verify this for sure. grep -f .. (where bunch of regex are listed line by line for all possible combinations, again the main code will look very minimal if I follow this? as the patterns sitting in -f file for grep will be transparent to a user) -or creating a map funcation (based on delimiters) for value ranges?
Possible values:
1/03/2017:23:0:15
02/4/2017:0:1:2
09/05/2017:10:10:0
10/6/2017:12:14:16
Here's an unholy extended regular expression (POSIX ERE):
^([1-9]|1[0-2]|0[1-9])/([1-9]|0[1-9]|[12][0-9]|3[01])/[0-9]{4}:([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$
that will test for the date/time patterns you specified (m/d/yyyy:h:m:s and mm/dd/yyyy:hh:mm:ss), with:
month: 1-12, 01-12
day: 1-31, 01-31
year: 0000-9999
hour: 0-23, 00-23
minute: 0-59, 00-59
second: 0-59, 00-59
You can use in an awk program that will exit with success (exit code 0) if the (first) line is a valid date/time (wrapped in a shell function that tests the first argument, for convenience):
#!/bin/bash
is_datetime_valid() {
awk '{exit $0!~"^([1-9]|1[0-2]|0[1-9])/([1-9]|0[1-9]|[12][0-9]|3[01])/[0-9]{4}:([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$"}' <<<"$1"
}
Or, if you prefer a pure bash solution (with ERE support in bash v3.0+):
#!/bin/bash
is_datetime_valid() {
local pat='^([1-9]|1[0-2]|0[1-9])/([1-9]|0[1-9]|[12][0-9]|3[01])/[0-9]{4}:([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$'
[[ $1 =~ $pat ]]
}
You can use it like:
if is_datetime_valid "1/03/2017:23:0:15"; then
# yup, it's valid
else
# ney, it's invalid
fi
Tested on a few examples:
#!/bin/bash
samples=(
"1/03/2017:23:0:15" "02/4/2017:0:1:2" "09/05/2017:10:10:0" "10/6/2017:12:14:16"
"00/03/2017:23:0:15" "1/33/2017:23:0:15"
)
for dt in "${samples[#]}"; do
if is_datetime_valid "$dt"; then
echo "$dt is valid"
else
echo "$dt is invalid"
fi
done
Gives:
1/03/2017:23:0:15 is valid
02/4/2017:0:1:2 is valid
09/05/2017:10:10:0 is valid
10/6/2017:12:14:16 is valid
00/03/2017:23:0:15 is invalid
1/33/2017:23:0:15 is invalid
I do not know whether using BSD date is an option for you, but it has what you are looking for.
There the date checker function can look like this
is_datetime_valid() {
date -j -f "%m/%d/%Y:%T" $1 1> /dev/null 2>&1
return $?
}

Add mins in existing custom date format via shell

I have problem that is using custom date format like date +%y%j.%H%M%S,
And what i want is to add 15 mins on the this date on just on current date of system. so that i can use for further calculation into my process.
I have tried with below code -
$uprBond=`date +%y%j.%H%M%S`
$ echo $uprBond
16079.031135
$ date -d "$(uprBond) + 5 minutes" +%y%j.%H%M%S
op > bash: uprBond: command not found
16079.035920
I am failing while passing the above date format , Can anybody please help on this.
Just for note, below is the piece of code is working when i used date function instead of defined date variable i.e. $uprBond (I don't want to use predefined date because we have some old same formatted date which needs that adding of mins).
date +%y%j.%H%M%S -d "`date` + 5 minutes";
op > 16079.040724
With GNU date, GNU bash 4 and its Parameter Expansion:
#!/bin/bash
uprBond="$(date +%y%j.%H%M%S)"
year="20${uprBond:0:2}"
doy="${uprBond#${uprBond:0:2}}"
doy="${doy%.*}"
time="${uprBond#*.}"
time="${time:0:2}:${time:2:2}:${time:4:2}"
in5min=$(date -d "${year}-01-01 +${doy} days -1 day +5 minutes ${time}" "+%y%j.%H%M%S")
echo "now: $uprBond"
echo "in 5min: $in5min"
Output:
now: 16079.145026
in 5min: 16079.145526

Get next Sunday after a given date (bash)

Is there any built-in function to get next Sunday (next Monday, Tuesday, etc.) from a given date using bash shell script?
For example, what is the first Sunday after 1-Sep-2014? I expect 7-Sep-2014.
I have searched the answer in google, but only found this function :
date "+%Y%m%d" -d Sun
which is to get next Sunday after today.
I fear date can't do it directly. But you can use brace expansion to get the dates for all the following 7 days and select Sunday from them:
echo '1-Sep-2014\ +'{1..7}'\ days' | xargs -n1 date -d | grep Sun
You can use this little script:
#!/bin/bash
w=$(date -d"$1" +%w)
diff=$(( (7-$w)%7 ))
date -d"+$diff day $1" +%F
saved as sun.sh then:
kent$ ./sun.sh 01-Sep-2014
2014-09-07
kent$ ./sun.sh 31-Aug-2014
2014-08-31
kent$ ./sun.sh 3-Sep-2014
2014-09-07
kent$ ./sun.sh 23-Sep-2014
2014-09-28
Note that, argument validation was not done, I just showed how to calculate the date.
It is possible to make date do all the math without using shell code for math functions or to iterate though output looking for answers. The date function can do basic math like "x + 1 days" or "x - 1 month + 2 days".
It can also output formatted strings with arbitrary values.
The trick here is to use one date invocation to output the string used to calculate your final answer. We'll input your input date and ask for what day of the week that is, but at the same time output some math to subtract that number of days. By adding 7 days first we'll end up an the next week, then by subtracting the day-of-the-week number from that we'll have the next Sunday:
INPUTDATE="20140901"
date +"%Y%m%d" -d "$(date +"%Y%m%d + 7 days - %w days" -d "$INPUTDATE")"
This will spit out your expected answer: 20140907. Of course you can change the output format using the format string on the outer date invocation.

Resources