GMT to MST timestamp conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting GMT to MST timestamp conversion
# 1  
Old 12-15-2014
GMT to MST timestamp conversion

Hi Team,

We have written a perl script to perform the GMT to MST timestamp conversion.

Input: 2013-12-01T05:23:19.374
Output: need the given timestamp in MT (MST/MDT)

Code:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;

#always gmt
#my $tval = '2013-12-01T05:23:19.374';

my ($year,$month,$day,$hour,$min,$sec,$mil) = $ARGV[0] =~ m/^(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2}).(\d{3})$/;
print "$year-$month-$day $hour:$min:$sec\n";
my $seconds = timelocal($sec, $min, $hour, $day, $month-1, $year);
($sec, $min, $hour,$day,$month, $year) = localtime($seconds-25200);

printf "%04d-%02d-%02d %02d:%02d:%02d", $year+1900, $month+1, $day, $hour, $min, $sec;

GMT is 7 hrs ahead of MST and 8 hrs ahead of MDT. Since we have hard coded 25200 the given timestamp is always converted to MST. Can someone help us to make it dynamic.. like.. during day light savings, the 8 hrs has to be deducted else 7 hrs.

Thanks
Krishnakanth
# 2  
Old 12-15-2014
Does it need to be perl? From shell script you could do:

Code:
$ TZ=MST date -d "2013-12-01 05:23:19.374 GMT"
Sat Nov 30 22:23:19 MST 2013

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 12-16-2014
Kmanivan,

The call to timelocal should be to timegmt as the argument is in GMT. You then can eliminate adjusting the value of seconds by 25200 when you call localtime.

For example:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
use POSIX qw{ strftime }

my @T = $ARGV[0] =~ m/^(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})(?:\.\d+)$/;
print strftime("%Y-%m-%d %H:%M:%S %Z%n", localtime timegmt reverse @T);

# 4  
Old 12-22-2014
Hi derekludwig,

Thank you for your reply.

I am getting syntax err.

syntax error at GMT2MST.pl line 7, near "my "

Can you help me to fix the issue?

Thanks
# 5  
Old 12-22-2014
How about this change to your original code:

Code:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
use POSIX qw(tzset);

#always gmt
#my $tval = '2013-12-01T05:23:19.374';

my ($year,$month,$day,$hour,$min,$sec,$mil) = $ARGV[0] =~ m/^(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2}).(\d{3})$/;
print "$year-$month-$day $hour:$min:$sec\n";
$ENV{TZ}="GMT";
tzset;
my $seconds = timelocal($sec, $min, $hour, $day, $month-1, $year);
$ENV{TZ}="MST";
tzset;
($sec, $min, $hour,$day,$month, $year) = localtime($seconds);

printf "%04d-%02d-%02d %02d:%02d:%02d\n", $year+1900, $month+1, $day, $hour, $min, $sec;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

GMT to local Time conversion

How can I convert the following date format: New Log Date = 2016-12-30 23:50:33 GMT from GMT time to local time? Thanks (13 Replies)
Discussion started by: mrn6430
13 Replies

2. Shell Programming and Scripting

Timestamp conversion

Hello All, I have a file with many timestamps as in below format & example: 20150130105120 2015-> Year in YYYY 01-> Month in MM 30-> Day in DD 10-> Hour in HH 51-> Minute in mm 20-> Seconds in SS This is in GMT. I want to convert all these time stamps in GMT+5:30 format.. Can... (4 Replies)
Discussion started by: ailnilanjan
4 Replies

3. Shell Programming and Scripting

Timestamp conversion in PERL

Hi, I have a file as below I need to overwrite the 2 nd column alone to numeric format like "06122011030414012345" as per the timestamp value output file should be the microseconds can be neglected if required. Any help will be appreciated. Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

4. Shell Programming and Scripting

Epoch & Unix Timestamp Conversion Tools

Hi All, Please read the below data carefully. I need an unix command for converting unix timestamp to Epoch timestamp. I need to daily convert this today's unix(UTC) time to epoch time, so i am thinking to make a shellscript for this. Please help me for this by providing... (3 Replies)
Discussion started by: aish11
3 Replies

5. AIX

Need timestamp conversion shell script !!

Can anyone provide me with a ksh or bash script which will accept a timestamp (format is YYYY-MM-DD-HH24.Mi.Ss) and time offset (in hours). The output will be (timestamp passed - time offset passed deducted from it) in the same YYYY-MM-DD-HH24.Mi.Ss format. Basically I am trying to convert the... (1 Reply)
Discussion started by: shibajighosh
1 Replies

6. Programming

timestamp conversion problem.

Hi all. I have the following code: #include<stdio.h> #include<time.h> int main() { struct tm tm; time_t time = 1262322000; /*Jan, 01, 2010*/ char temp; int i = 0; while(i < 4) { memset(temp, 0, 128); localtime_r(&time,... (2 Replies)
Discussion started by: adm1n
2 Replies

7. Shell Programming and Scripting

[Perl] Timestamp conversion

Hi, I have searched, read and tried, but no luck. I have this code: #!/bin/perl -w #-d use strict; use POSIX qw(strftime); my $getprpw_list="/usr/lbin/getprpw -l"; my $host = "nbsol151"; my $user = "genadmin"; my %uid; my %spwchg; my %upwchg; my %slogint; (2 Replies)
Discussion started by: ejdv
2 Replies

8. Shell Programming and Scripting

conversion of different timestamp to standard timestamp

hi i need a scrit to convert one date format to another. for example i have three columns in a file which gets a different format, but lastly i want output with stadard timestamp as "yyyy-mm-dd hh:mm:ss" column1 column2 ... (2 Replies)
Discussion started by: dprakash
2 Replies

9. Shell Programming and Scripting

Timestamp to date conversion in ksh

Hi, I have a file containing timestamp( Example given below). How can i get date(mmd-dd-yyyy) from it? ($> cat file1.txt 2008-11-24 05:17:00.7043) Thanks, Sri (2 Replies)
Discussion started by: srilaxmi
2 Replies

10. Shell Programming and Scripting

conversion from EPOCH timestamp to local time zone

hello gurus, i want a perl/shell script which once invoked should convert a set of EPOCH timestamps to local time ( IST..i want) . how does it work ,i have an idea on that..but writing a perl/shell script for it is not possible for me...so i need help for the same. my exact requirement is... (2 Replies)
Discussion started by: abhijeetkul
2 Replies
Login or Register to Ask a Question