jdamae
jdamae

Reputation: 3909

How do I get yesterday's date using localtime?

How do I tweak this to get yesterday's date using localtime?

use strict;

sub spGetCurrentDateTime;
print spGetCurrentDateTime;

sub spGetCurrentDateTime {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $currentDateTime = sprintf "%s %02d %4d", $abbr[$mon], $mday, $year+1900; #Returns => 'Aug 17 2010' 
return $currentDateTime;
}

~

Upvotes: 14

Views: 44869

Answers (8)

Nemo
Nemo

Reputation: 1

my $yesterday = time(); $yesterday = $yesterday - (24*60*60);

24 as 24 hours, 60 as 60 minutes in hour and 60 as 60 seconds in minute time() will return actual timestamp, and 246060 will remove seconds for exactly one day After this simply do: localtime($yesterday);

Upvotes: 0

Pronin Oleg
Pronin Oleg

Reputation: 71

Solution suggested by most users is wrong!

localtime(time() - 24*60*60)

The worst thing you can do is to assume that 1 day = 86400 seconds.

Example: Timezone is America/New_York, date is Mon Apr 3 00:30:00 2006

timelocal gives us 1144038600

localtime(1144038600 - 86400) = Sat Apr 1 23:30:00 EST 2006

oops!

The right and the only solution is to let system function normalize values

$prev_day = timelocal(0, 0, 0, $mday-1, $mon, $year);

Or let datetime frameworks (DateTime, Class::Date, etc) do the same.

That's it.

Upvotes: 5

user3769176
user3769176

Reputation: 1

This is how I do it.

#!/usr/bin/perl

use POSIX qw(strftime);

$epoc = time();
$epoc = $epoc - 24 * 60 * 60;

$datestring = strftime "%F", localtime($epoc);

print "Yesterday's date is $datestring \n";

Upvotes: -1

mscha
mscha

Reputation: 6840

use Time::Piece.

use strict;
use warnings;
use 5.010;

# These are core modules in Perl 5.10 and newer
use Time::Piece;
use Time::Seconds;

my $yesterday = localtime() - ONE_DAY;
say $yesterday->strftime('%b %d %Y');

Note that this can go wrong in certain borderline cases, such as the start of daylight saving time. The following version does behave correct in such cases:

use strict;
use warnings;
use 5.010;

# These are core modules in Perl 5.10 and newer
use Time::Piece;
use Time::Seconds;

my $now = localtime();
my $yesterday = $now - ONE_HOUR*($now->hour + 12);
say $yesterday->strftime('%b %d %Y');

Alternatively, you can use the DateTime module as described in a different answer. That is not a core module, though.

Upvotes: 6

bigiain
bigiain

Reputation: 809

The DST problem can be worked around by taking 3600s from midday today instead of the current time:

#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;

sub spGetYesterdaysDate;
print spGetYesterdaysDate;

sub spGetYesterdaysDate {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my $yesterday_midday=timelocal(0,0,12,$mday,$mon,$year) - 24*60*60;
($sec, $min, $hour, $mday, $mon, $year) = localtime($yesterday_midday);
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $YesterdaysDate = sprintf "%s %02d %4d", $abbr[$mon], $mday, $year+1900;
return $YesterdaysDate;
}

In light of the "unspecified" documented behaviour of the strftime solution suggested by Chas, this approach might be better if you're not able to test for expected-but-not-guaranteed results across multiple platforms.

Upvotes: 13

Chas. Owens
Chas. Owens

Reputation: 64929

As tempting as it is to just subtract a day's worth of seconds from the current time, there are times when this will yield the wrong answer (leap seconds, DST, and possibly others). I find it easier to just let strftime (available in the Perl 5 core module POSIX) take care of all of that for me.

#!/usr/bin/perl

use strict;
use warnings;

use Time::Local;
use POSIX qw/strftime/;

#2010-03-15 02:00:00
my ($s, $min, $h, $d, $m, $y) = (0, 0, 0, 15, 2, 110);

my $time      = timelocal $s, $min, $h, $d, $m, $y;    
my $today     = strftime "%Y-%m-%d %T", localtime $time;
my $yesterday = strftime "%Y-%m-%d %T", $s, $min, $h, $d - 1, $m, $y;
my $oops      = strftime "%Y-%m-%d %T", localtime $time - 24*60*60;
print "$today -> $yesterday -> $oops\n";

Upvotes: 18

daxim
daxim

Reputation: 39158

use DateTime qw();
DateTime->now->subtract(days => 1); 

The expression on the second line returns a DateTime object.

Upvotes: 24

user3458
user3458

Reputation:

localtime(time() - 24*60*60)

Upvotes: 1

Related Questions