17 May, 2003
Converting entry dates to UTC in Movable Type with the PerlScript plugin
Posted by: Jennifer In: MT hacks
I was recently frustrated by Movable Type's basic support of time zones. You can select a time zone as a number of hour plus or minus UTC, but there's no adjustment for Daylight Savings Time, and entry dates and times are saved in whatever localtime was at the time. The default RSS 1.0 template just tacks on the +/- hours, a global setting. If you ware exporting a year's worth of entries, there's no way to get the correct time for both summer and winter entries.
I then remembered some perl functions that can convert times from local time to UTC and then to whatever timezone you want. A Unix system can keep track of DST, even from the past, so the new UTC times will be correct. With Brad Choate's PerlScript plugin, you can put perl code right in your template. With 4 lines of code, I was able to print out a UTC date and time suitable for RSS.
<dc:date><MTPerlScript>
use Time::Local;
my $mytime = timelocal(<$MTEntryDate format=
"('%S', '%M', '%H', '%e', '%m'-1, '%Y'-1900)"$>);
my ($sec,$min,$hour,$mday,$mon,$year,
$wday,$yday,$isdst)=gmtime($mytime);
printf('%d-%02d-%02dT%02d:%02d:%02dZ',
$year+1900,$mon+1,$mday,$hour,$min,$sec);
</MTPerlScript></dc:date>
A couple of notes. The arguments to timelocal are in quotes, since the values may have a leading 0 that will confuse perl. If your time zone is different from your server, you'll have to adjust the %H parameter by the difference. Also, in perl months are numbered 0 to 11, and years are numbered from 1900, so we have to adjust accordingly. We then have a Unix timestamp in $mytime, and can obtain the individual components in UTC. I'm using printf for the output since the format for dc:date in RSS is so strict. Each %02d formats the output of an integer to two places padded with a 0 if necessary.
(This code was originally posted www.papascott.de/2003/05/17/2256.php)
Guest authored by:
Scott Hanson – papascott.de