Solved

converting to my time given Mail Date

Posted on 1998-08-13
6
164 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
  • 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 400 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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 …
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

22 Experts available now in Live!

Get 1:1 Help Now