Solved

converting to my time given Mail Date

Posted on 1998-08-13
6
169 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl search and replace 6 168
hard perl script 16 156
work on ods spreadsheet with perl in ubuntu 4 76
Get a Perl script to return content from a module 7 80
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 …
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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