Forum Moderators: coopster & phranque

Message Too Old, No Replies

Trouble with time conversion

Perl script to convert to timestamps

         

Donboy

6:10 am on Jan 26, 2005 (gmt 0)

10+ Year Member



I've got this script I wrote which is pieced together from other scripts I found, but its not working for me.


#!/usr/bin/perl

use 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.

MichaelBluejay

4:29 pm on Jan 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't know enough about Perl to know what's wrong with your code, but I could code a different way of doing it, if I understood what the resulting format is supposed to look like. I don't know what a "perl/unix timestamp" is. Please post how you want the output to look and I'll see what I can do.

Donboy

1:55 am on Jan 27, 2005 (gmt 0)

10+ Year Member



A perl/unix timestamp is the number of seconds that have elapsed since January first 1970. It's a well-known format. I'm surprised you haven't heard of it.

Anyway, it's only going to run at the command line. Here's an example from yesterday's date...

1106710024

balam

6:38 am on Jan 27, 2005 (gmt 0)

10+ Year Member



> $timestamp isn't producing any output. Why is that?

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...

balam

7:05 am on Jan 27, 2005 (gmt 0)

10+ Year Member



I got bored...

#!/usr/bin/perl

# Calendar time (at midnight) from date
# Enter date as: 12-31-1999

use 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;

wruppert

6:35 pm on Jan 28, 2005 (gmt 0)

10+ Year Member



There are many, many date/time manipulation modules on CPAN. One that I like is Class::Date. It understands just about everything about dates and times. Example:

use Class::Date qw(date);
my $date = date("2005-01-28");
print $date, "\n";
print $date->mdy, "\n";
print $date->epoch, "\n";

Produces

2005-01-28 00:00:00
01/28/2005
1106892000

Donboy

1:39 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



Thanks fellas! I think I'm gonna go with wruppert's suggestion, since his is smaller code, and probably more useful to be using the module since I can do more with it.

Thanks to everyone else who replied.