Solved

converting to my time given Mail Date

Posted on 1998-08-13
4
156 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Regular expression 9 189
perl rename 2 137
Perl string replace for refred url 9 57
Move Function in Perl Script 2 58
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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

12 Experts available now in Live!

Get 1:1 Help Now