Improve company productivity with a Business Account.Sign Up


converting to my time given Mail Date

Posted on 1998-08-13
Medium Priority
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
  • 3

Accepted Solution

b2pi earned 1600 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
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…
Six Sigma Control Plans

602 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