#!/usr/bin/perluse POSIX;
$hyphendate = shift;
($StoredMonth,$StoredDay,$StoredYear)=split(/-/,$hyphendate);
$sec = 0;
$min = 0;
$hour = 0;
$mday = $StoredDay;
$mon = $StoredMonth - 1;
$year = $StoredYear - 1900;
$wday = 0;
$timestamp = mktime($sec,$min,$hour,$mday,$mon,$year);
print "-> $timestamp <- $hyphendate\n";
I want to input a hyphenated date on STDIN like 1-25-05 and produce a perl/unix timestamp. Trouble is, the $timestamp isn't producing any output. Why is that? I have used this before in another scenario, and it worked... but now its not working for me.
I know you shouldn't answer a question with a question, but: What is 5 minus 1900? "Negative 1895," you answer.
Ah, but "-1895" isn't a valid value for the mktime function, so it returns an undef value. That is why the program runs, but with no output (from $timestamp).
> I want to input a hyphenated date on STDIN like 1-25-05
The program works as written, if you enter the date as "1-25-2005".
You can remove the "$wday = 0;" line; you're not using that variable in the program.
> It's a well-known format.
Yes, but only if you use perl or live in a *nix world...
#!/usr/bin/perl# Calendar time (at midnight) from date
# Enter date as: 12-31-1999use strict;
use warnings;
use POSIX;my($date) = shift;
my($month, $day, $year) = split(/-/, $date);
$month -= 1;
$year -= 1900;my($timestamp) = mktime(0, 0, 0, $day, $month, $year);
print "\n$date -> $timestamp\n";
exit;
use Class::Date qw(date);
my $date = date("2005-01-28");
print $date, "\n";
print $date->mdy, "\n";
print $date->epoch, "\n";
2005-01-28 00:00:00
01/28/2005
1106892000