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
Solved

converting to my time given Mail Date

Posted on 1998-08-13
4
182 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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

861 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