I want to convert a date like
Sat Dec 31 22:42:58 CET 2011
to a valid rss date which normaly looks like
Mon, 06 Sep 2009 16:45:00 +0000
Seconds should always be 00. Is there anyway to do so? Besides it would be could to find out the current as a valid rss but that's optional ;)
Update
With your new sample input, you don't even need to reformat. It's as simple as
$ time="Sat Dec 31 22:42:58 CET 2011"; date -Rd "$time"
Sat, 31 Dec 2011 13:42:58 -0800
The trick is to reformat your date into a string that date can understand and take as input. In this case it is 2011-12-31 13:37. I'm using awk to do this, but there are number of different utilities that will suffice.
#!/bin/bash
time="12-31-11 13:37"
date -Rd "$(awk -F'[- ]' '{printf("20%s-%s-%s %s\n", $3,$1,$2,$4)}' <<<"$time")"
Output
$ time="12-31-11 13:37"; date -Rd "$(awk -F'[- ]' '{printf("20%s-%s-%s %s\n", $3,$1,$2,$4)}' <<<"$time")"
Sat, 31 Dec 2011 13:37:00 -0800
Related
Is there a bash command that will tell me the current time in a given time zone while accounting for daylight savings? For example, I'm thinking of something like this:
$ getDateTime --region Seattle
2021-01-01-13-30-00
Importantly, I would like it to give me the standard time during the winter and daylight time during the summer, and switch over on the correct days of the year in accordance with the region. Does something like this exist in bash? If not, what about another language?
Set the TZ environment variable to the desired time zone and use the date command.
TZ=US/Pacific date
There are some city names in the timezone database, such as America/New_York and America/Los_Angeles, but it's not very complete and doesn't include Seattle. See https://data.iana.org/time-zones/tzdb-2019c/zone.tab for the master list.
Building on Barmar's answer, here's a bash function you can use:
getDateTime() {
TZ="$1" date '+%Y-%m-%d-%H-%M-%S %Z %z'
}
Sample usage:
getDateTime America/Los_Angeles
getDateTime America/New_York
getDateTime Pacific/Honolulu
getDateTime Asia/Hong_Kong
2022-07-08-18-06-50 PDT -0700
2022-07-08-21-06-50 EDT -0400
2022-07-08-15-06-50 HST -1000
2022-07-09-09-06-50 HKT +0800
Daylight savings times are handled automatically by the system, since the timezone names are timezone-aware.
Your system most likely uses the most up-to-date timezone information from IANA:
https://data.iana.org/time-zones/tzdb/zone.tab
https://ftp.iana.org/tz/tzdb/zone.tab
You can find your system's file of timezone names in:
/usr/share/zoneinfo/zoneinfo.tab
It seems like a really nice idea to map individual country/state/city names or latitude/longitude pairs to IANA timezone TZ names, there are probably third-party software out there that can do this.
Only with bash >= version 4.2:
TZ=US/Pacific printf -v unixtime "%(%Y-%m-%d-%H-%M-%S)T\n" -1
echo "$unixtime"
Output (e.g.)
2022-07-08-15-18-51
To demonstrate how the other answers work with DST:
"spring forward"
date:
$ TZ=America/Los_Angeles date -d '2022-03-13 01:59:59'
Sun Mar 13 01:59:59 PST 2022
$ TZ=America/Los_Angeles date -d '2022-03-13 + 1 second 01:59:59'
Sun Mar 13 03:00:00 PDT 2022
bash builtin printf
$ epoch=$(TZ=America/Los_Angeles date -d '2022-03-13 01:59:59' +%s)
$ TZ=America/Los_Angeles printf '%(%F %T %Z)T\n' $epoch
2022-03-13 01:59:59 PST
$ TZ=America/Los_Angeles printf '%(%F %T %Z)T\n' $((epoch + 1))
2022-03-13 03:00:00 PDT
"fall behind"
$ TZ=America/Los_Angeles date -d '2022-11-06 01:59:59'
Sun Nov 6 01:59:59 PDT 2022
$ TZ=America/Los_Angeles date -d '2022-11-06 + 1 second 01:59:59'
Sun Nov 6 01:00:00 PST 2022
$ epoch=$(TZ=America/Los_Angeles date -d '2022-11-06 01:59:59' +%s)
$ TZ=America/Los_Angeles printf '%(%F %T %Z)T\n' $epoch
2022-11-06 01:59:59 PDT
$ TZ=America/Los_Angeles printf '%(%F %T %Z)T\n' $((epoch + 1))
2022-11-06 01:00:00 PST
I have a String variable (datetime with GMT offset) in the following format.
How can I convert this to a MST in bash shell script?
Input GMT :- 08/Sep/2020:11:38:01 +0000
Output MST :- 08/Sep/2020:04:38:01 -0700
We can get the offset like this
offset=$(date +%-z)
I dont want to again convert into date from string and then use offset and minus offset to reach MST.Is there a way to convert it in a better way?
Convert 08/Sep/2020:11:38:01 +0000 with bash to 08 Sep 2020 11:38:01 +0000:
gmt="08/Sep/2020:11:38:01 +0000"
gmt="${gmt//\// }" # replace all / with spaces
gmt="${gmt/:/ }" # replace first : with space
Then use it with GNU date:
TZ="MST" date --date="$gmt" +'%d/%h/%Y:%H:%M:%S %z'
Output:
08/Sep/2020:04:38:01 -0700
I have a human readable time as
08-18-2016 09:18:25
I want it to be converted into epoch time using shell script.
I tried with date "+%s" but I am getting the error
date: invalid date `08-18-2016 09:32:42'
The canonical way to convert a datetime into epoch is to use:
date "+%s" # for this moment's date
date -d" some date" "+%s" # for a specific date
However, in this case the format is not valid:
$ date -d"08 18 2016 09:18:25" "+%s"
date: invalid date ‘08 18 2016 09:18:25’
You need, then, to massage the string a bit before passing it to date -d.
This converts the two first spaces into slashes:
$ sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25"
08/18/2016 09:18:25
So this works:
$ date -d"$(sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25")" "+%s"
1471504705
Or using variables:
$ nice_date=$(sed 's# #/#;s# #/#' <<< "08 18 2016 09:18:25")
$ date -d"$nice_date" "+%s"
1471504705
Thanks for the explanation fedorqui. But 1471511905 is the epoch time
for 08 18 2016 09:18:25, not 1471504705. – Mohit Rane
date -u … will print Coordinated Universal Time.
I want to convert 18-Aug-2015 date format to '2015-08-18' using shell script
Try this formatting:
$ date +"%Y-%m-%d"
http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/
The -d option is GNU specific.
Here, you don't need to do date calculation, just rewrite the string which already contains all the information:
a=$(printf '%s\n' "$Prev_date" | awk '{
printf "%04d-%02d-%02d\n", $6, \
(index("JanFebMarAprMayJunJulAugSepOctNovDec",$2)+2)/3,$3}')
Without awk, assuming your initial date is in $mydate:
IFS=- d=($mydate)
months=(Zer Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
z=1
while [[ ${months[$z]} != ${d[1]} ]]; do z=$((z+1)); done
printf "%s-%02d-%s\n" ${d[2]} $z ${d[0]}
I need to somehow use the date command in bash or another utility to print out the date and time, 5 minutes before and 5 minutes after a given value.
For example:
input:
Thu Dec 19 14:10
output:
Thu Dec 19 14:05 Thu Dec 19 14:10 Thu Dec 19 14:15
I see that the date command can be used to do this on the current date/time, can it be used with a passed value, i.e. not the current date/time?
You can achieve this, for the current time, by typing.
$ date --date='5 minutes ago'; date; date --date='5 minutes'
Qui Dez 19 16:09:17 BRST 2013
Qui Dez 19 16:14:17 BRST 2013
Qui Dez 19 16:19:17 BRST 2013
To use a specific date (ex 1978/01/10).
$ date --date='1978-01-10 + 5 minutes'
Ter Jan 10 00:05:00 BRT 1978
With GNU date, you can do a simple form of date/time arithmetic with the argument to the --date option:
$ date --date 'now + 5 minutes'
With BSD date (at least, the version that ships with Mac OS X), the -v option allows you to do something similar:
$ date -v +5M
$ date -v -5M
If you're using bash under linux, you can use the -d parameter to perform date manipulation on an existing date:
Get the EPOCH time for the date in question:
EPOCH=$(date -d 'Thu Dec 19 14:10' '+%s')
This gives you the time, in seconds, since the EPOCH (typically 01/01/1970)
Now you can use simple math to subtract or add 5 minutes (in seconds) to the EPOCH time
NEW_EPOCH=$(($EPOCH - 300))
obviously, there are 300 seconds in 5 minutes
Now convert this NEW_EPOCH back into a human readable date
NEW_DATE=$(date -d "1970-01-01 ${NEW_EPOCH} sec")
NOTE that this only works on unix systems which support the date -d option (i.e. Linux)
If you want to do this for the current time +/-5 minutes and you use Bash 4.2 or newer, you can do it without external tools:
$ printf -v now '%(%s)T'
$ echo "$now"
1516161094
$ f='%a %b %d %R'
$ printf "%($f)T %($f)T %($f)T\n" "$((now-300))" "$now" "$((now+300))"
Tue Jan 16 22:46 Tue Jan 16 22:51 Tue Jan 16 22:56
The %(datefmt)T formatting string of printf allows to print date-time strings. If the argument is skipped (like here) or is -1, the current time is used.
%s formats the time in seconds since the epoch, and -v now stores the output in now instead of printing it.
f is just a convenience variable so I don't have to repeat the formatting string for the output three times.
Since the argument for this usage of printf has to be in seconds since the epoch, you're stuck with external tools to convert an input string like Thu Dec 19 14:10 into that format and you'd replace
printf -v now '%(%s)T'
with, for example, any of
now=$(date '+%s' -d 'Thu Dec 19 14:10') # GNU date
now=$(date -j -f '%a %b %d %T' 'Thu Dec 19 14:10' '+%s') # macOS date