Convert date to different timezone + format change

1

I have a log file with timestamps in format of "22.09.2016 08:22:54" ("+%Y:%m:%d %H:%M:%S"). This timestamp is in UTC timezone.

Now I have to convert this timestamp to Europe/Helsinki timestamp using bash script.

I have this script which makes the conversion as I want.

#!/bin/bash
sec=$(TZ="UTC" date +'%s' -d "2015-05-20 18:05:02")
TZ="Europe/Helsinki" date -d "@$sec" "+%Y:%m:%d %H:%M:%S"

This works fine but the date "input" format ( -d "2015-05-20 18:05:02" ) is different format comparing to my log timestamp format.

I would like to change this script to something like this..

#!/bin/bash
sec=$(TZ="UTC" date +'%s' -d "$1")
TZ="Europe/Helsinki" date -d "@$sec" "+%Y:%m:%d %H:%M:%S"

Where the $1 can be in the original date format of my log file "22.09.2016 08:22:54" ("+%Y:%m:%d %H:%M:%S").

I cannot make this to work.. Can someone help me with this one.. Thank you

EDIT:

I have tried something like this and many other combinations..

#!/bin/sh 
sec=$(TZ="UTC" date +'%s' "+%Y:%m:%d %H:%M:%S" -d "$1") 
TZ="Europe/Helsinki" date -d "@$sec" "+%Y:%m:%d %H:%M:%S" 

but his ends up with error..

date: extra operand ‘+%Y:%m:%d %H:%M:%S’ 
Try 'date --help' for more information. 
date: invalid date ‘@’ 

user3647376

Posted 2016-09-23T05:50:23.350

Reputation: 13

Did you try to set a format for your first line? – Seth – 2016-09-23T05:54:46.020

yes.. I have tried this..

#!/bin/sh sec=$(TZ="UTC" date +'%s' "+%Y:%m:%d %H:%M:%S" -d "$1") TZ="Europe/Helsinki" date -d "@$sec" "+%Y:%m:%d %H:%M:%S"

but his ends up with error..

date: extra operand ‘+%Y:%m:%d %H:%M:%S’ Try 'date --help' for more information. date: invalid date ‘@’ – user3647376 – 2016-09-23T05:58:32.980

Answers

1

It doesn't look like date supports an input format string. As such you won't be able to just go ahead and pass the string to date. The man page state that it accept the usual formats so maybe your locale setting could influence this but I'm not sure about that.

Further more lets have a look at your script:

#!/bin/bash
sec=$(TZ="UTC" date +'%s' -d "$1")
$(TZ="Europe/Helsinki" date -d "@$sec" "+%Y:%m:%d %H:%M:%S")

What happens here is that you convert the $1 parameter to a Linux time stamp and then convert it again into a string you want. One possible approach to make this work with your input would be to reformat the $1 string parameter.

You could do this as an example by using sed with a suitable regex. Beware that you should make sure that you have a valid string beforehand and that any format changes might break this.

#!/bin/bash
intputdate=$(echo $1 | sed -re 's/([0-9]{2})\.([0-9]{2})\.([0-9]{4})/\3-\2-\1/')
sec=$(TZ="UTC" date +'%s' -d "$intputdate")
echo $(TZ="Europe/Helsinki" date -d "@$sec" "+%Y:%m:%d %H:%M:%S")

Edit: Not having the wrong variable names certainly helps.

Seth

Posted 2016-09-23T05:50:23.350

Reputation: 7 657

0

This looks a bit nicer, converting NZ time to Monaco time:

TZ="Europe/Monaco" date --date="TZ=\"Pacific/Auckland\" 2017-10-11 00:51" +%Y-%m-%d\ %H:%M:%S

2017-10-10 13:51:00

#!/bin/bash
new_zealand_time="2017-10-12 00:56"
monaco_time=$(TZ="Europe/Monaco" date --date="TZ=\"Pacific/Auckland\" $new_zealand_time" +%Y-%m-%d\ %H:%M:%S)
echo "Time in Monaco: $monaco_time"

Time in Monaco: 2017-10-11 13:56:00

P6039

Posted 2016-09-23T05:50:23.350

Reputation: 1

Read over "Why do I need 50 reputation to comment" to ensure you understand how you can start commenting.

– Pimp Juice IT – 2017-10-11T12:56:09.463

0

On MacOS (10.14.3) works pretty fine as you can provide your exact format via -f switch:

$ d1="22.09.2016 08:22:54"
$ sec=$(TZ="UTC" date -j -f "%d.%m.%Y %H:%M:%S" "$d1" "+%s")
1474532574
$ TZ="Europe/Helsinki" date -j -f "%s" "$sec" "+%Y:%m:%d %H:%M:%S"
2016:09:22 11:22:54

and by the way, your log format is "+%d.%m.%Y %H:%M:%S", not "+%Y:%m:%d %H:%M:%S" as you state after the 2nd code snippet, otherwise the line where sec is defined in my code above will likely throw illegal time format error or something like that.

RAM237

Posted 2016-09-23T05:50:23.350

Reputation: 126