getting unix time from date (perl)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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