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
Related
I need to retrieve the month of a date (dd.month.year) and transform it into the digit YYYY-MM
the source month is in french, so i try :
# date --date="$(printf "01 %s" "January 2020")" +"%Y-%m"
2020-01
# LC_TIME=fr_FR date --date="$(printf "01 %s" "Janvier 2020")" +"%Y-%m"
date: invalid date '01 Janvier 2020'
# LANG=fr_FR date --date="$(printf "01 %s" "Janvier 2020")" +"%Y-%m"
date: invalid date '01 Janvier 2020'
it works when I use the month in English.
But with the month in French even by forcing the language or local environment parameters it does not recognize the month correctly.
even with date simply it still announces in us :
# LANG=fr_FR date
Sun May 17 23:45:40 CEST 2020
# LC_TIME=fr_FR date
Sun May 17 23:45:48 CEST 2020
# date
Sun 17 May 2020 11:45:53 PM CEST
any idea ?
info :
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-8-amd64
Architecture: x86-64
Although the parsing done by date is flexible and amazing at times, it doesn't handle localization (here and the array is static here). You have to build an translation array that would translate Janvier to January yourself, and then pass the result to date.
It will be easy to use an associative bash array for that:
arr=([Janvier]=January etc)
echo "${arr[Janvier]}"
Getting the date in bash works something like this:
now=$(date)
echo "Current date: $now"
It returns:
Current date: Mon Nov 28 11:34:55 NZDT 2016
I need to store the date in a variable where the date is in a format like this:
20161128
How can I convert the date format into the format I need?
You use any format with +%
date +%Y%m%d
Output
20161127
When using email.date.to_s
I receive a date in this format
Wed, 3 Jun 2015 14:57:46 -0700
but I want it to be in this format
06/03/15
You can use strftime to format the string:
Time.now.strftime("%m/%d/%y")
#=> 09/16/15
I am trying to convert month number to name, but it is giving output as current month instead of the date given in the variable.
KornShell (ksh) Code:
datep= 2013-10-22
echo $datep |printf "%(%B)T\n"
printf doesn't read from standard input, so it is assuming today's date as the default argument for the %T format; you need to provide the date as an argument instead.
printf "%(%B)T\n" "$datep"
Do it like this:
$ datep="2013-10-22"
$ date -d"$datep" "+%B"
October
As per man date,
-d, --date=STRING
display time described by STRING, not 'now'
So we get:
$ date -d"$datep"
Tue Oct 22 00:00:00 CEST 2013
Then you say you want the %B, that is, also from man date:
%B
locale's full month name (e.g., January)
So it is just a matter of using the format at the end of the string.
Other examples:
$ date -d"$datep" "+%Y" #year
2013
$ date -d"$datep" "+%F" #date
2013-10-22
$ date -d"$datep" "+%T" #time (if not given, gets the default)
00:00:00
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