Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Last active January 18, 2019 00:19
Show Gist options
  • Save miyagawa/4ec107552f3457236f80f40c202c52b4 to your computer and use it in GitHub Desktop.
Save miyagawa/4ec107552f3457236f80f40c202c52b4 to your computer and use it in GitHub Desktop.
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