Solved

converting to my time given Mail Date

Posted on 1998-08-13
4
167 Views
Last Modified: 2010-03-05
I need to be able to convert a date as shown by RFC822 (mail headers), to my localtime. I am working in Perl 5.

Say the input is:

"Wed, 12 Aug 1998 13:13:58 -0400"

I want the output to be:

Wed, 12 Aug 1998 13:13:58

but if the input is:

"Wed, 12 Aug 1998 13:13:58 -0300"

I want the output to be:

Wed, 12 Aug 1998 12:13:58

and be able to handle all the different timezones. I looked at GNUs and they don't do it, they just put the date as it comes with GMT, +0100, -0500. I would like to be able to convert the time to my local time.

anybody knows how I can do this in Perl?

BTW, I'm in Montreal, so I am -0400. I looked in tin code and they have a 400-line parser (yacc) to do this. Is there a way to do it in Perl. I have a function already that gives me the -0400 so take that as a given as well.
0
Comment
Question by:agj
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 400 total points
ID: 1209263
It's relatively easy to do, actually.  You can directly parse it yourself, or you can

use Date::Manip;

and then use ParseDate to parse it.  For a more explicit answer, you'll need to redo the question, as (this is a quote)

################
    "Wed, 12 Aug 1998 13:13:58 -0400"

    I want the output to be:

    Wed, 12 Aug 1998 13:13:58

    but if the input is:

    "Wed, 12 Aug 1998 13:13:58 -0300"
############

doesn't really say what the difference between the input strings is.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1209264
Sorry, I meant to put in an example, but my fingers were too clumsy.  Herewith (note no error checking)

use Date::Manip;
$d = "Wed, 12 Aug 1998 13:13:50 -0400";
$d =~ /^[^,]*,\s*(.*)\s+([+-]\d\d){\d\d)$/;
$d = $1;
$hrs = $2;
$min = $3;

$date = DateCalc(ParseDate($d), "$hrs Hours $min Minutes");
$d = UnixDate($date, "%a, %e %b %Y %H:%M:%S");
print "Ended up with \"$d\"\n";

0
 
LVL 5

Expert Comment

by:b2pi
ID: 1209265
Ack.. I just figured out what you want to do... you really do want to convert to local time.
In that case, in the code above, replace the DateCalc line with the following:


$hrs = -1*$hrs;
$date = DateCalc(ParseDate($d), "$hrs Hours $min Minutes");
$date = DateCalc($date, "- 4 hours");


If you were going to be really nice, you'd pull the - 4 hours from the environment (it should be under $ENV{TZ}).
0
 

Author Comment

by:agj
ID: 1209266
Well, with the pointer to Date::Manip I got it up and running in no time! Thanks!!!
BUT, I found a simpler way of doing it since Date::Manip already does the changes for me. Look:

$date = "Wed, 12 Aug 1998 13:13:58 -0300";
$new_date = UnixDate (ParseDate ($date), "%a %b %d %H:%M");

then $new_date has: Wed Aug 12 12:13

as you can see the time has been modified accordingly.

I tried this:
$date = "Wed, 12 Aug 1998 15:13:29 GMT";

then date is: Wed Aug 12 11:13

so as you see, two-liner and got me there.
Anyway, thanks for the pointer to Date::Manip!!!


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now