« Tortillas | Main | Sphere III »

convert-date - match dates and perform TZ conversions

Quick Perl script to match dates in input data by regex, and convert between the specified time zones.

#!/usr/bin/perl -w # # Usage: convert-date < input > output use strict; use Date::Manip qw(UnixDate ParseDate Date_ConvTZ); # should be arguments my $from_tz = 'UTC'; my $to_tz = 'PDT'; my $output_format = '%Y-%m-%d %H:%M:%S'; while (<>) { # How to match the date in the input data. May # need to munge to something Date::Manip can # grok, or add HH:MM:SS, depending. s/ (\d{4}-\d\d-\d\d [T\s] \d\d:\d\d:\d\d) /fix_date($1)/ex; print; } sub fix_date { my $input = shift; my $date_in = ParseDate($input); warn "$0: error: could not parse: date=$input\n" if length $date_in < 1; my $date_out = Date_ConvTZ( $date_in, $from_tz, $to_tz ); warn "$0: error: could not convert: " . "date=$input, from=$from_tz, to=$to_tz\n" if length $date_out < 1; my $output = UnixDate( $date_out, $output_format ); return $output; }

Technorati Tags: ,