using perl to print yesterday's date, but with formatting options ?

T

Tom Van Overbeke

Hi,

I need a way to use yesterday's date in a variable. The tricks with 'date'
don't work on my system, but perl does.

so i've got:
perl -le 'print scalar localtime time - 86400' which outputs:
Sun Oct 30 17:56:52 2005

But the format I need is: 30.10.05

Any ideas on how to add this to the above piece of code ?

thanks,

tom.
 
G

Gunnar Hjalmarsson

Tom said:
I need a way to use yesterday's date in a variable. The tricks with 'date'
don't work on my system, but perl does.

so i've got:
perl -le 'print scalar localtime time - 86400' which outputs:
Sun Oct 30 17:56:52 2005

But the format I need is: 30.10.05

Any ideas on how to add this to the above piece of code ?

Capture the date string in a variable and use the tr/// operator.
 
G

Gunnar Hjalmarsson

Gunnar said:
Capture the date string in a variable and use the tr/// operator.

Sorry, didn't read the question carefully enough.

Either you can get the date components by evaluating localtime() in list
context

perldoc -f localtime

and create the date string you want 'by hand', or you can use one of the
many date modules.
 
G

Glenn Jackman

At 2005-10-31 11:57AM said:
Hi,

I need a way to use yesterday's date in a variable. The tricks with 'date'
don't work on my system, but perl does.

so i've got:
perl -le 'print scalar localtime time - 86400' which outputs:

subtracting a fixed number of seconds is guaranteed to break twice a
year, for places that have daylight savings.
But the format I need is: 30.10.05

use Date::Calc qw(Today Add_Delta_Days);
my ($year, $month, $day) = Add_Delta_Days(Today(), -1);
printf "%02d.%02d.%02d", $day, $month, substr($year, 2, 2);
 
J

Joe Smith

Purl said:
($day, $month, $year) = (localtime(time - 86400)) [3,4,5];
print $day, ".", $month + 1, ".0", $year - 100;

If I put your solution into a program, will it work properly
four and a half years from now? The year after "09" is not "010".
-Joe
 
M

Mothra

Glenn said:
subtracting a fixed number of seconds is guaranteed to break twice a
year, for places that have daylight savings.

Agreed, That's why there is DateTime :)
use Date::Calc qw(Today Add_Delta_Days);
my ($year, $month, $day) = Add_Delta_Days(Today(), -1);
printf "%02d.%02d.%02d", $day, $month, substr($year, 2, 2);

Or the DateTime version

use strict;
use warnings;
use DateTime;
use DateTime::Format::Strptime;

my $Strp = new DateTime::Format::Strptime(
pattern => '%d.%m.%y',
);

my $dt = DateTime->now()->subtract( days => 1 );


print $Strp->format_datetime($dt);
 
P

Paul Lalli

Purl said:
#!perl

($day, $month, $year) = (localtime(time - 86400)) [3,4,5];

print $day, ".", $month + 1, ".0", $year - 100;

Wow, PurlGurl has discovered the Year 2010 bug.

printf ("%02d.%02d.%02d", $day, $month+1, $year % 100);

Paul Lalli
 
A

A. Sinan Unur

Purl said:
Purl said:
Tom Van Overbeke wrote:
(snipped)
But the format I need is: 30.10.05
print $Array[3], ".", $Array[4] + 1, ".", $Array[5] + 1900;
print $Array[3], ".", $Array[4] + 1, ".0", $Array[5] - 100;

Somewhat obfuscated but named variables help with clarity
in using different formats, as you request.

#!perl

($day, $month, $year) = (localtime(time - 86400)) [3,4,5];

print $day, ".", $month + 1, ".0", $year - 100;

print "\n\n";

print $month + 1, "-", $day, "-", $year + 1900;

This is your umpteenth wrong "solution" you have posted on this thread.

Assuming the OP really does want two digit years, there are quite a few
ways of doing this correctly.

The problem has two parts: (1) Correctly figure out yesterday's date,
(2) Print it correctly in a way that does not become invalid in 5 years.
I hate two digit dates with a passion as well as ambiguous date formats,
but since the OP asked for two digit dates, here is one way of doing it:

On (1), let's assume that subtracting 86400 seconds from current time is
the correct procedure for getting yesterday's date (I am sure there are
issues that messes this up in some cases).

As for (2), this is the perfect reason to use printf:

use strict;
use warnings;

my ($day, $month, $year) = (localtime(time - 86400)) [3,4,5];
printf "%2.2d.%2.2d.%2.2d\n", $day, $month + 1, $year % 100;

__END__

On the other hand, the OP should be made aware of POSIX::strftime as
well.

Sinan
 
M

Mothra

A. Sinan Unur wrote:
(snipped)
On (1), let's assume that subtracting 86400 seconds from current time
is the correct procedure for getting yesterday's date (I am sure
there are issues that messes this up in some cases).

You are correct, DST will mess this up leap seconds will as
well. In cases like this it is best to use one of the Date/Time
modules on CPAN. like DateTime :)


Mothra
 
A

A. Sinan Unur

A. Sinan Unur wrote:
(snipped)

You are correct, DST will mess this up leap seconds will as
well. In cases like this it is best to use one of the Date/Time
modules on CPAN. like DateTime :)

Thanks, especially since I had forgotten about DateTime. Yes, I have used
that module for this particular reason, but forgot to mention it.

Sinan
 
D

David K. Wall

Glenn Jackman said:
subtracting a fixed number of seconds is guaranteed to break twice
a year, for places that have daylight savings.


use Date::Calc qw(Today Add_Delta_Days);
my ($year, $month, $day) = Add_Delta_Days(Today(), -1);
printf "%02d.%02d.%02d", $day, $month, substr($year, 2, 2);

printf "%02d.%02d.%02d", $day, $month, $year % 100;

Y10K bug. Your code might be in use longer than you think. :)
 
C

chris-usenet

A. Sinan Unur said:
On (1), let's assume that subtracting 86400 seconds from current time is
the correct procedure for getting yesterday's date (I am sure there are
issues that messes this up in some cases).

One is the daylight savings switch - for those time zones that have
such a thing: the test will fail during the last hour of the 25 hour
day. Another is the inclusion of occasional leap seconds: this test will
fail during the last second of 2005.

Chris
 
U

usenet

Tom said:
I need a way to use yesterday's date in a variable.
...
But the format I need is: 30.10.05

use Date::Manip;
my $yesterday = UnixDate('yesterday', "%d.$m.$y");
 
M

Mothra

Purl said:
C:\APACHE\USERS\TEST>perl -MPOSIX -wle "print strftime '%d.%m.%y' =>
localtime time - 86400"
m.05

Odd, it works on my Win32 system
Z:\>perl -MPOSIX -wle "print strftime '%d.%m.%y' => localtime time - 86400"
30.10.05

Z:\>perl -v

This is perl, v5.8.6 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

It may be you are running an older verison of Perl?
No surprise you are not being trolled for not compensating for
Daylight Savings Time.

In my example I used DateTime suite of modules. It supports
both DST conversion and leap seconds. You may want to take a
look at the project
http://datetime.perl.org

Hope this helps

Mothra
 
A

A. Sinan Unur

For trivia, your method results for Win32. Readers are cautioned POSIX
is buggy; results will vary greatly depending on operating system.

C:\APACHE\USERS\TEST>perl -MPOSIX -wle "print strftime '%d.%m.%y' =>
localtime time - 86400"

m.05

D:\Home\asu1\UseNet\clpmisc> perl -MPOSIX -wle "print strftime q{%d.%m.%
y} => localtime time - 86400"
30.10.05

Hmmmm ...

Sinan
 
A

axel

One is the daylight savings switch - for those time zones that have
such a thing: the test will fail during the last hour of the 25 hour
day.

And for the first hour of the day following the 23 hour day.

Axel
 
U

usenet

Purl said:
All-in-all, you have yet to receive any intelligent responses
regarding Daylight Savings Time.

On Daylight Savings Time, you will never write code which
will work correctly.

I beg to differ on both points. I posted this reply:
http://tinyurl.com/8uwwb. It works for daylight savings time, and leap
years, and Y10K, and years ending in "00" and it won't necessarily
break in Autumn of 2037, and everything else.

Simple:

use Date::Manip;
my $yesterday = UnixDate('yesterday', "%d.%m.%y");

If this solution does not meet the OP's requirements while avoiding the
pitfalls of cronowackiness, I'd like to know in what scenario it would
fail.
 
M

Mothra

I beg to differ on both points. I posted this reply:
http://tinyurl.com/8uwwb. It works for daylight savings time, and leap
^^^^^^^^^^^^^^^
I don't know about that. from the docs:

KNOWN BUGS

Daylight Savings Times

Date::Manip does not handle daylight savings time,
though it does handle timezones to a certain extent.
Converting from EST to PST works fine.
Going from EST to PDT is unreliable.

Another reason to use DateTime. DateTime will handle
DST changes, leap seconds, leap years, other calender systems
and much more :)

Hope this helps

Mothra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top