0

I have a string of date and time of format: Day, Month DAY_OF_MONTH, Year HH:MM:SS AM/PM GMT[+/-]hh:mm .

But I am unable to parse it. I have used this:

Time::Piece->strftime($string, "%A, %B %d, %Y %I:%M:%S %p %Z%z");

I have converted the string into the format : Day, Month DAY_OF_MONTH, Year HH:MM:SS AM/PM [+/-]hhmm . But still its not working for me with

Time::Piece->strftime($string, "%A, %B %d, %Y %I:%M:%S %p %z");

The string is getting parsed but the time zone is not considered in the Time::Piece object as tzoffset is 0000 and the HH of the date has the same value as that in the string. Please anyone help.

Eg: String to be parsed: Friday, July 25, 2008 12:15:57 PM GMT-0700

user1799308
  • 186
  • 1
  • 8
  • 3
    Why not include an actual date string with your example? Make it as easy as possible for people to help you. – oalders Nov 05 '12 at 06:09
  • So it's **double** colon between Time and Piece... – Dan Dascalescu Nov 05 '12 at 06:44
  • Oh yeah.. I have used Time::Piece only. double colon between Time and Piece Actually, I am not getting the error, the string is getting parsed successfully, but when I print it, the tzoffset have hours and minutes as 0000 and the hours of the Time::Piece object is still 12. But in v 5.16 it is 19 with tzoffest 0000. So, the thing is that it is parsing but the timezone, but the timezone is not in the Time::Piece object. Any other way to do this. – user1799308 Nov 05 '12 at 06:57

2 Answers2

0

If Time::Piece doesn't work, try using Date::Parse.

And as oalders mentioned, do include an actual date string example in your question.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • Hi, I am using Perl v 5.10, and it doesn't have Date::Parse. Eg : Actual string to parse is : Friday, July 25, 2008 12:15:57 PM GMT-07:00 . Which I converted into Friday, July 25, 2008 12:15:57 PM -0700 . It is working in v 5.16 and not in v 5.10. Please help. – user1799308 Nov 05 '12 at 06:39
  • Install it from CPAN? One of the main reasons to use Perl is CPAN's 20,000 modules. `cpan Date::Parse`. – Dan Dascalescu Nov 05 '12 at 06:41
  • I am forced to use v 5.10 and can not install any new packages. :( – user1799308 Nov 05 '12 at 06:42
  • 1
    @user1799308, not being able to use CPAN is a ridiculous restriction. You can either use it and solve your problem in 15 minutes, or not use it and spend a week coming to, more often than not, an inferior solution. – titanofold Nov 05 '12 at 12:02
0

Perhaps the following will help:

use strict;
use warnings;
use Time::Piece;

my $string = 'Sunday, November 4, 2012 10:25:15 PM -0000';
my $t = Time::Piece->strptime( $string, "%A, %B %d, %Y %I:%M:%S %p %z" );
print $t;

Output:

Sun Nov  4 22:25:15 2012
Kenosis
  • 6,196
  • 1
  • 16
  • 16
  • Actually, this piece of code works in Perl v 5.16 but not in 5.10. And I am forced to use version 5.10 without installing any packages. Please help. – user1799308 Nov 05 '12 at 06:44
  • @user1799380 - Time::Piece was around before Perl v5.10. – Kenosis Nov 05 '12 at 06:54
  • yeah.. it is there, but there is problem with the timezone. – user1799308 Nov 05 '12 at 07:01
  • @user1799380 - Maybe the following thread will help: [How can I parse dates and convert time zones in Perl?](http://stackoverflow.com/questions/411740/how-can-i-parse-dates-and-convert-time-zones-in-perl) – Kenosis Nov 05 '12 at 07:14
  • Note that the function you want to call has a 'p' in it rather than an 'f'. strptime is not the same as strftime. – titanofold Nov 05 '12 at 12:04
  • @titanofold - Perhaps I'm missing something, as [the documentation](http://search.cpan.org/~jhi/perl-5.7.2/ext/Time/Piece/Piece.pm) shows that Time::Piece supports the %p token. In fact, the documentation states that "The available format characters [for Time::Piece] are as in the standard strftime() function..." – Kenosis Nov 05 '12 at 16:44