Skip to content

Instantly share code, notes, and snippets.

@michel47
Created April 19, 2022 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michel47/28885708744ee1dbdb46422f39e305a1 to your computer and use it in GitHub Desktop.
Save michel47/28885708744ee1dbdb46422f39e305a1 to your computer and use it in GitHub Desktop.
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