SH: how to convert the date "Nov 26 23:59:00 2022 GMT" to timestamp? - shell

I want to convert the string "Nov 26 23:59:00 2022 GMT" to a timestamp 1669507140000. I checked the man date to get this command:
date -d "Nov 26 23:59:00 2022 GMT" +"%s"
But his returns: 1669507140 (the last 3 zeros are missing).
Can anyone tell me what I am missing? Thank you.

The version of the date command that I have on my system (YMMV) will show us what it is doing if we add the --debug flag to our invocation...
$ date -d "Nov 26 23:59:00 2022 GMT" +"%s" --debug
date: parsed date part: (Y-M-D) 2020-11-26
date: parsed time part: 23:59:00
date: parsed number part: year: 2022
date: parsed zone part: UTC+00
date: input timezone: parsed date/time string (+00)
date: using specified time as starting value: '23:59:00'
date: starting date/time: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00'
date: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00' = 1669507140 epoch-seconds
date: timezone: system default
date: final: 1669507140.000000000 (epoch-seconds)
date: final: (Y-M-D) 2022-11-26 23:59:00 (UTC)
date: final: (Y-M-D) 2022-11-27 07:59:00 (UTC+08)
1669507140
The debug log shows us the %s format will result in displaying seconds since epoch. Alternatively, nanosecond precision can be achieved using a modified format string (I added a .001 in the date string to prove it works):
$ date -d "Nov 26 23:59:00.001 2022 GMT" +"%s.%N" --debug
date: parsed date part: (Y-M-D) 2020-11-26
date: parsed time part: 23:59:00
date: parsed number part: year: 2022
date: parsed zone part: UTC+00
date: input timezone: parsed date/time string (+00)
date: using specified time as starting value: '23:59:00'
date: starting date/time: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00'
date: '(Y-M-D) 2022-11-26 23:59:00 TZ=+00' = 1669507140 epoch-seconds
date: timezone: system default
date: final: 1669507140.001000000 (epoch-seconds)
date: final: (Y-M-D) 2022-11-26 23:59:00 (UTC)
date: final: (Y-M-D) 2022-11-27 07:59:00 (UTC+08)
1669507140.001000000
We can use a pipe to awk to round up to milliseconds for our final form (note I used .0005 in the date string here to show rounding):
$ date -d "Nov 26 23:59:00.0005 2022 GMT" +"%s.%N" | awk '{ printf("%d\n", int($0 * 1000) + (int($0 * 2000) % 2)) }'
1669507140001
One further note.... The following is the version of the date command I am using:
$ date --version
date (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David MacKenzie.

Related

How to calculate date in the past from given date (also in past) using "date -d 'xx/xx/xxxx - xx' "

I have a input date:
01-01-2019 08:30:00
And I would like to back it in time by:
2hours 10minutes
So the new date should be like:
01-01-2019 06:20
I have tried commands:
(1)date -d "2019-01-01 08:30:00 + 1hour"
which return: Tue Jan 1 09:30:00 STD 2019 (ok, but seems to be lucky shot)
(2)date -d "2019-01-01 08:30:00 - 1hour"
which return: Tue Jan 1 11:30:00 STD 2019 that means +3hours (wrong)
(3)date -d "2019-01-01 08:30:00 + 3hours"
which return: Tue Jan 1 07:30:00 STD 2019 that means -1hours (wrong)
(4)date -d "2019-01-01 08:30:00 + 2minutes"
which return: Tue Jan 1 07:31:00 STD 2019 that means -59minutes (wrong)
You probably need to explicitly mention your timezone:
$ TZ=STD date -d '2019-01-01 08:30:00 STD + 1 hour'
Tue Jan 1 09:30:00 STD 2019
$ TZ=STD date -d '2019-01-01 08:30:00 STD - 3 hour'
Tue Jan 1 05:30:00 STD 2019
$ TZ=STD date -d '2019-01-01 08:30:00 STD + 2 minutes'
Tue Jan 1 08:32:00 STD 2019
This seems to alleviate dates confusion.
The is only one most reliable way of doing that:
date -d "#$(( $(date -d '2019-01-01 08:30:00' +%s) - ( 2 * 60 + 10 ) * 60 ))"
First convert to seconds since epoch.
Then subtract the time you want.
Then use date to format the output.

How to convert a timestamp to a different timezone in bash?

I have a time in GMT, that looks like this:
Wed Feb 13 04:46:54 +0000 2019
In a bash script on MacOS 10.14.3, I want to convert it to the user's local timezone. i.e., this one is 04, so changing it to my timezone would be -6, so it would end up something like this:
Tue Feb 12 22:46:54 CST 2019
Thanks in advance.
Is this what you are looking for ?
date -d "Wed Feb 13 04:46:54 +0000 2019 - 6 hour"
dt= "Wed Feb 13 04:46:54 +0000 2019"
tz="-6"
date -d "$dt $tz hour"
I ended up using something like this:
postYear=`TZ="${timeZone}" date -jf "%Y-%m-%d %H:%M:%S %z" "${postYear}-${postMonth}-${postDay} ${postHourGmt}:${postMinute}:${postSecond} +0000" +%Y`
...where the variables were giving input into the date that I wanted to pull something out of...
...and the year (%Y) was what I was pulling out.

Bash script to iterate through a specific time range, one second at a time

I am trying to create some sort of loop in bash that will iterate through a specific time range, one second at a time.
At each interval, it will perform a duty with that timestamp I.e. "Wed Mar 2 12:00:03 CDT 2018"
I am having a hard time wrapping my head around how to make a loop that will iterate every second of time, and when it hits 60 seconds, update the minute, etc.
Thoughts? This seems obvious but the right syntax escapes me.
this is one way of doing it, delegate time computations to date
$ for i in {1..10}; do date -d "+$i seconds"; done
Sun Mar 11 20:40:57 UTC 2018
Sun Mar 11 20:40:58 UTC 2018
Sun Mar 11 20:40:59 UTC 2018
Sun Mar 11 20:41:00 UTC 2018
Sun Mar 11 20:41:01 UTC 2018
Sun Mar 11 20:41:02 UTC 2018
Sun Mar 11 20:41:03 UTC 2018
Sun Mar 11 20:41:04 UTC 2018
Sun Mar 11 20:41:05 UTC 2018
Sun Mar 11 20:41:06 UTC 2018
Sun Mar 11 20:41:07 UTC 2018
if you want to iterate up to a certain time, change the for loop to a while loop and a counter.
To make a loop with time you need to format date/time in such a way that can be comparable. The most easy trick is to transform date to something that will look like an integer and you then use lt,le,eq,gt,ge operators of bash.
Consider this :
$ date
Mon Mar 12 00:16:29 EET 2018 #this format/data type is not comparable by bash
$ date +%Y%m%d%H%M%S
20180312001629 #this is an integer representing the current time
This is a sample loop from now up to a specific time point:
stop=$(date +%Y%m%d%H%M%S --date "2018-03-12 00:20:55")
while [[ $(date +%Y%m%d%H%M%S) -le $stop ]];do #comparing current date & time with stop date & time
echo "date now is $(date)";
sleep 1; #sleep 1 second
done
date now is Mon Mar 12 00:20:51 EET 2018
date now is Mon Mar 12 00:20:52 EET 2018
date now is Mon Mar 12 00:20:53 EET 2018
date now is Mon Mar 12 00:20:54 EET 2018
date now is Mon Mar 12 00:20:55 EET 2018
The trick here is that the command
stop=$(date +%Y%m%d%H%M%S --date "2018-03-12 00:20:55")
converts given date (using the --date flag) to a kind of integer format.
Then the while loop keeps comparing current date in the same integer format with the stop date.
This is a similar script to start/stop at a specific time, but it is a bit "resources hungry" since it keeps comparing current time to start time:
date; #just print the current date & time
compl=false;
start=$(date +%Y%m%d%H%M%S --date "2018-03-12 01:45:50");
stop=$(date +%Y%m%d%H%M%S --date "2018-03-12 01:45:55");
while true;do
while [[ $(date +%Y%m%d%H%M%S) -ge $start ]] && [[ $(date +%Y%m%d%H%M%S) -le $stop ]];do
echo "date now is $(date)";
sleep 1;
compl=true;
done;
($compl) && break;
done
Mon Mar 12 01:45:37 EET 2018
date now is Mon Mar 12 01:45:50 EET 2018
date now is Mon Mar 12 01:45:51 EET 2018
date now is Mon Mar 12 01:45:52 EET 2018
date now is Mon Mar 12 01:45:53 EET 2018
date now is Mon Mar 12 01:45:54 EET 2018
date now is Mon Mar 12 01:45:55 EET 2018
An alternative to start/stop a script at specific time would be to use epoch date / epoch seconds.
Epoch date is measured in seconds since the epoch (1970-01-01 UTC).
Every date can be expressed as epoch seconds, using the format date +%s.
date #just print the current date & time
start="2018-03-12 02:17:52"
stop="2018-03-12 02:17:57"
timerequired=$(( $(date +%s --date "$start") - $(date +%s) ))
sleep $(($timerequired)) #sleep till the starting time
while [[ $(date +%s) -le $(date +%s --date "$stop") ]];do
echo "date now is $(date)";
sleep 1;
done
Mon Mar 12 02:17:39 EET 2018
date now is Mon Mar 12 02:17:52 EET 2018
date now is Mon Mar 12 02:17:53 EET 2018
date now is Mon Mar 12 02:17:54 EET 2018
date now is Mon Mar 12 02:17:55 EET 2018
date now is Mon Mar 12 02:17:56 EET 2018
date now is Mon Mar 12 02:17:57 EET 2018
Finally, you can use external tools to automate things based on date & time like cron, anacron , at, etc
Echo a date for every second in a range:
from=$(date -d "Wed Mar 2 12:59:58 CDT 2018" +%s)
to=$(date -d "Wed Mar 2 13:00:04 CDT 2018" +%s)
for s in $(seq $from $to); do echo $(date -d #$s); done
The date -d takes a well formed date as input. +%s formats it into seconds since epoch. So you get two big values like from=1520013598, to=...3604. The date -d #1520013598 transforms the timestamp back to a human readable date.
Your range might be in a variable like start and stop, and you might like to foobar ($time) instead of echoing it:
start="Wed Mar 2 12:59:58 CDT 2018"
stop="Wed Mar 2 13:00:04 CDT 2018"
from=$(date -d "$start" +%s)
to=$(date -d "$stop" +%s)
for s in $(seq $from $to)
do
timestamp=$(date -d #$s)
foobar $timestamp
done

Bash: get date and time from another time zone

I would want to get the date and time from another time zone (UTC-4) with a bash command, but I don't want to configure it as the default TZ for the system.
Is there a simple way to do it?
E.g 1 (current):
$ date
fri nov 7 13:15:35 UTC 2014
E.g 2 (what I need):
$ date (+ some option for UTC-4)
fri nov 7 09:15:35 UTC 2014
You could use
TZ=America/New_York date
or if you want to do date arithmetic you could use
date -d "+5 hours"
You can use offset value in TZ to get the date for a different timezone:
TZ=UTC date -R
Fri, 07 Nov 2014 13:55:07 +0000
TZ=UTC+4 date -R
Fri, 07 Nov 2014 09:54:52 -0400
I use a little script for that, so I don't have to know or remember the exact name of the timezone I'm looking for. The script takes a search string as argument, and gives the date and time for any timezone matching the search. I named it wdate.
#!/bin/bash
# Show date and time in other time zones
search=$1
format='%a %F %T %z'
zoneinfo=/usr/share/zoneinfo/posix/
if command -v timedatectl >/dev/null; then
tzlist=$(timedatectl list-timezones)
else
tzlist=$(find -L $zoneinfo -type f -printf "%P\n")
fi
grep -i "$search" <<< "$tzlist" \
| while read z
do
d=$(TZ=$z date +"$format")
printf "%-32s %s\n" "$z" "$d"
done
Example output:
$ wdate fax
America/Halifax Fri 2022-03-25 09:59:02 -0300
or
$ wdate canad
Canada/Atlantic Fri 2022-03-25 10:00:04 -0300
Canada/Central Fri 2022-03-25 08:00:04 -0500
Canada/Eastern Fri 2022-03-25 09:00:04 -0400
Canada/Mountain Fri 2022-03-25 07:00:04 -0600
Canada/Newfoundland Fri 2022-03-25 10:30:04 -0230
Canada/Pacific Fri 2022-03-25 06:00:04 -0700
Canada/Saskatchewan Fri 2022-03-25 07:00:04 -0600
Canada/Yukon Fri 2022-03-25 06:00:04 -0700
If the other solutioons don't work, use
date --date='TZ="UTC+4" 2016-08-22 10:37:44' "+%Y-%m-%d %H:%M:%S"
2016-08-22 16:37:44

How to compute a relative date from a given date?

The GNU date command can output date computed from relative items such as -1 hour or 1 month ago. It can also output date from different input format (for instance calendar date, seconds since epoch, etc...).
Is it possible to combine both a date format and a relative time to compute a new date from a given date using the GNU§ date utility?
Something like:
date_epoch=`date "+%s"`
date --date="#$date_epoch -1 month"
The second command gives an "incorrect date" error...
You can do it, sure:
$ date -d"$(date -d#$date_epoch) -1 month" "+%Y-%m-%d"
2013-09-16
Which comes from:
$ date_epoch=$(date "+%s")
$ date -d#$date_epoch
Wed Oct 16 23:46:50 CEST 2013
that you can use it "hardcoded":
$ date -d"Wed Oct 16 23:46:50 CEST 2013 -1 month"
Mon Sep 16 23:46:50 CEST 2013
Or using the variable:
$ date -d"$(date -d#$date_epoch) -1 month"
Mon Sep 16 23:46:50 CEST 2013

Resources