?
Solved

converting to my time given Mail Date

Posted on 1998-08-13
6
Medium Priority
?
190 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
[X]
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
  • 2
6 Comments
 
LVL 5

Expert Comment

by:b2pi
ID: 1209257
I don't think you meant to submit this question twice, you should be able to delete it.
0
 

Accepted Solution

by:
kaijen earned 1200 total points
ID: 1209258
Let's try:

$date = "Wed, 12 Aug 1998 13:13:58";
$date =~ m#(\w+,\s+\d+\s+\w+\s+\d+\s+)(\d+):(\d+):(\d+)(?:\s+(\+|-)?(\d{2}))?#;

$begin = $1;
$hour = $2;
$min = $3;
$sec = $4;
$sign = $5;
$diff = $6 if $6 ne "";
$diff = 0 if $6 eq "";

if ( $sign eq "-" )
{
      $hour += $diff - 4;
}
else
{
      $hour -= $diff + 4;
}

$date = "$begin$hour:$min:$sec";

print "$date\n";

should do the job.

Best regards,
Kai.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1209259
Kai, what if the add or subtract of the hours takes you past midnight?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 84

Expert Comment

by:ozo
ID: 1209260
Well, it's too late to delete it now without help from customer support now that kaijen has locked it.
But what if the input is
"Wed, 12 Aug 1998 01:13:58 -0100"
0
 

Expert Comment

by:kaijen
ID: 1209261
Okay, you're right! Sorry for beeing so fast. I think the idea is clear and there's a lot more work to invest to consider midnight, month changing and new years day. If anyone has a shorthand solution for this please reject my answer and I volunteer for accepting this.

Okay?

Best regards,
Kai.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1209262
Actually, agj accidentally submitted the question twice (it's actually easier to do than it should be.  The question has already been answered (basically, the answer was to use the Date::Manip package)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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

719 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