Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Last active Jan 18, 2019
Embed
What would you like to do?
Convert local timestamps in text to UTC
#!/usr/bin/env perl
use strict;
use DateTime;
use Date::Parse qw(str2time);
my $pattern = qr/\d?\d:\d\d(:\d\d)?(?: ?[aApP][mM])?/;
while (<>) {
s/$pattern/utc(str2time($&), $1)/eg;
print;
}
sub utc {
my($epoch, $sec) = @_;
my $utc = DateTime->from_epoch(
epoch => $epoch,
time_zone => "UTC",
);
$utc->strftime( defined $sec ? "%H:%M:%S" : "%H:%M" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment