Solved

converting to my time given Mail Date

Posted on 1998-08-13
6
152 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

17 Experts available now in Live!

Get 1:1 Help Now