tags:

views:

65

answers:

1

I'm using the DateTime Perl module to get the time in a particular timezone. The result of the time is as follows

2010-09-24T02:18:52

How can I convert this to HTTP format before printing?

+4  A: 

CPAN has what you need: DateTime::Format::HTTP.

Description:

This module provides functions that deal [with] the date formats used by the HTTP protocol (and then some more).

Synopsis:

use DateTime::Format::HTTP;

my $class = 'DateTime::Format::HTTP';
$string = $class->format_datetime($dt); # Format as GMT ASCII time
$time = $class->parse_datetime($string); # convert ASCII date to machine time

Update

You can pass in the timezone of your source data, use DateTime set_time_zone() to change the timezone, and then use strftime to generate the string in 'HTTP' format. For example:

my $dt = DateTime::Format::HTTP->parse_datetime( '2010-03-09T12:34:56', 'EST' );
warn $class->format_datetime( $dt );
warn $dt->strftime( "%a, %d %b %Y %H:%M:%S %Z" );
$dt->set_time_zone( 'CET' );
warn $dt->strftime( "%a, %d %b %Y %H:%M:%S %Z" );

Tue, 09 Mar 2010 17:34:56 GMT
Tue, 09 Mar 2010 12:34:56 EST
Tue, 09 Mar 2010 18:34:56 CET

(line numbers deleted for clarity.)

martin clayton
I've tried that. It converts everything to GMT. I want it in my timezone
Rajesh
@Powertieke, DateTime::Format::HTTP's `format_datetime` method does _not_ allow you to specify the timezone. It always produces GMT, in accordance with [RFC 2616 section 3.3.1](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1): "All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception."
cjm
You are right, I was looking at parse_datetime()... deleted my comment to avoid confusion.
Powertieke
@cjm - do you want to post a modified comment, or edit your note into the answer/add an answer for 'completeness'?
martin clayton