Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm having a brain dump. I need to find out what the timestamp was at midnight of the current day. I know that  my $time = time; will give me the timestamp as of right now, but I will always need the timestamp as of 12:00am. Any ideas how I could do this?

Replies are listed 'Best First'.
Re: Finding Timestamp
by Aristotle (Chancellor) on Apr 15, 2003 at 19:24 UTC
    use Time::Local; my $midnight_time = timelocal(0,0,0, (localtime)[3,4,5] ); print scalar localtime $midnight_time;
    See Time::Local - timelocal expects 6 parameters in the same order as localtime returns them. The first three represent the current time of day; you just pass zeroes instead.

    Makeshifts last the longest.

Re: Finding Timestamp
by dmitri (Priest) on Apr 15, 2003 at 19:21 UTC
    # get current time my $time = time; # get seconds, minutes, and hours since midnight my ($sec, $min, $hr) = (gmtime($time))[0..2]; # subtract the sum of time since midnight $time -= ($hr * 3600 + $min * 60 + $sec); print $time, "\n";
Re: Finding Timestamp
by Improv (Pilgrim) on Apr 15, 2003 at 19:21 UTC
    One way is to call the date command (if you're on unix), as such:
    $stamp = `date --d "0:00" "+%s"`
    
    (note that this usage might be specific to the GNU version of date, so your milage might vary on solaris or other unices)

    Another is to use Date::Manip
    use Date::Manip; print UnixDate("0:00", "%s");
      Both methods are a last resort, at best, though. date is not available on Windows machines (if that's a concern) and requires a new process to be spawned just to get the date. Date::Manip is about the heaviest module you can use for any job and shouldn't be considered before other alternatives have been ruled out.

      Makeshifts last the longest.