Skip to content

Instantly share code, notes, and snippets.

@michel47
Created Apr 19, 2022
Embed
What would you like to do?
getting unix time from date (perl)
#!/usr/bin/perl
my $date = '4/12/2022 10:55:24';
printf "%u %s\n",&unixtime($date);
sub unixtime {
use Time::Local qw/timelocal/; # require "timelocal.pl";
my $date = shift;
my ($sec,$min,$hour,$mon,$mday,$year);
if ($date =~ m/ /) {
($date, $time) = split(' ',$date);
($hour,$min,$sec) = split ':', $time;
}
($mon,$mday,$year) = split '/', $date;
if ($mon > 12) { # date of form 1997/7/28 (EU style)
($year,$mon,$mday) = split '/', $date;
}
my $tics = timelocal($sec,$min,$hour,$mday,$mon-1,$year);
return $tics
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment