converting to my time given Mail Date

Posted on 1998-08-13
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.
Question by:agj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Accepted Solution

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.

Expert Comment

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";


Expert Comment

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}).

Author Comment

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!!!


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl script to parse log and delete the file 17 179
perl script to count sepecial characters in a file 7 148
Regular Expression for URL 10 109
File Find regex problem 4 68
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
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…

762 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