Perl - TimeDate format conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - TimeDate format conversion
# 1  
Old 03-29-2016
Perl - TimeDate format conversion

Dear All,

I need to print the below value from excel column

20160219T05:21:59+0100

to

19-FEB-2016 05:21:11

Can anyone help on this datetime format conversion.

Please suggest in print logic itself.

Regards,
Kumaresan P
# 2  
Old 03-29-2016
Will the seconds in your raw data look like that? (i.e. 59+0100) or will they actually be the seconds?
# 3  
Old 03-29-2016
Confused

Shouldn't this convert to:
19-FEB-2016 05:21:59
although, unsure of the +0100 (its significance)
# 4  
Old 03-30-2016
Yes, +0100 can be neglected, i just wanted to print in this format.
# 5  
Old 03-30-2016
If available:
Code:
DT="20160219T05:21:59+0100"
date -d"${DT/T/ }" +"%d-%b-%Y %T"
19-Feb-2016 05:21:59

# 6  
Old 03-31-2016
Thanks Rudic, it works in shell. how it will be incorporated in perl script,

Consider DT as an array from csv file.
# 7  
Old 03-31-2016
strftime() can do the hard work for you:
Code:
#!/usr/bin/perl -w
use POSIX qw(strftime);

$_ = "20160219T05:21:59+0100";

if (m/(\d{4})(\d{2})(\d{2})T(\d+):(\d+):(\d+)\+\d{4}/) {
   print strftime("%d-%b-%Y %T\n", $6,$5,$4,$3,$2-1,$1-1900);
}


Edit:Or for a bit more readability you can assign your captured groups to local vars:
Code:
#!/usr/bin/perl -w
use POSIX qw(strftime);

$DT = "20160219T05:21:59+0100";

if ($DT =~ m/(\d{4})(\d{2})(\d{2})T(\d+):(\d+):(\d+)\+\d{4}/) {
   my($y, $mt, $d, $h, $m, $s) = ($1, $2, $3, $4, $5, $6);
   print strftime("%d-%b-%Y %T\n", $s, $m, $h, $d, $mt-1, $y-1900);
} else {
   print "Illegal date format $DT\n";
}


Last edited by Chubler_XL; 03-31-2016 at 06:35 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date format conversion

Hi, i have to check the file whether it is created today. here is the ls -l o/p -rw-r----- 20000 50000 130 Dec 12 10:21 file.txt im able to check if file is created today or not if the timestamp is in 2014-12-12 format by comparing $(date +Y-%m-%d) but stuckup as it is of format Dec 12... (6 Replies)
Discussion started by: JSKOBS
6 Replies

2. Shell Programming and Scripting

Date conversion and Format

Hello , I have a record in below format Hostname | Query: 0 | Release: 0 | files: 2 | Files_examined: 2 | SET timestamp=1396778638; | select * from test I need output in below format Hostname | 0 | 0 | 2 | 2 | 04/06/2014|03:03:58 | select * from test I was able to get above output... (1 Reply)
Discussion started by: Tomlight
1 Replies

3. Shell Programming and Scripting

Format conversion

Can you help me get the desired output? Below is the input CONA= 0. 5. 10. 15. 20. 25. 30. 35. 40. 45. 50. 55. 60. 65. 70. 75. 80. 85. 90. 95. 100. 105. 110. PLANA= 0. 15. 30. 45. 60. 75. 90. 105. 120. 135. 150. 165. 180. ITABLE= 87.3 171.4 242.9 297.6 322.8 325.6 306.8 284.5 273.4 272.2 270.2... (6 Replies)
Discussion started by: Ravi S M
6 Replies

4. Shell Programming and Scripting

Date conversion from 24 hr format to 12 hr format

hi i want to convert date procured from sone operation which will be in 24hr format to 12 hr format displaying AM and PM # date -d @1362545068 Tue Mar 5 23:44:28 EST 2013 # this Tue Mar 5 23:44:28 EST 2013 i want to convert it so that output is as below Tue... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Date format conversion

Hi All, Can someone please let me know how can i convert the date format in unix as follow: From: 24 Oct 2011 i.e $(date +'%d %b %Y') To: 111024 i.e $(date +%y%m%d) Thanks in advance (3 Replies)
Discussion started by: davidtd
3 Replies

6. Ubuntu

Conversion of the format of a file in linux

How to convert a rtf file to a ttf file in ubuntu terminal? (2 Replies)
Discussion started by: poonam.gaigole
2 Replies

7. UNIX for Dummies Questions & Answers

date format conversion

hi, i have a file in which i get date format as 22/APR/2010... now i want the date format to be in 22-04-2010 if the month changes to may the file should also have 05 as month.... pls help (3 Replies)
Discussion started by: siva_nagarajan
3 Replies

8. Shell Programming and Scripting

format conversion

Is there any direct way in shell to convert exponential to other formats. For example 1.5e-07 to 0.150u. Or does shell support this microns, nano meter notations? (1 Reply)
Discussion started by: abhijanvt
1 Replies

9. Shell Programming and Scripting

Conversion of .zip to .tar.Z format

Can we have a shell script for this sort of conversion? There are some web-based tools which display the contents of tar.Z format. I am trying to convert zip files to that Thanks in advance (2 Replies)
Discussion started by: eagercyber
2 Replies

10. Programming

Binary to text format conversion

Hi, Please can any one tell me how to convert binary data to text format and vice versa. If possible give me the algorithm or C program. Thanks in advance Waiting for reply Bye:o (5 Replies)
Discussion started by: manjunath
5 Replies
Login or Register to Ask a Question