Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

442 views
Skip to first unread message

Tom Van Overbeke

unread,
Oct 31, 2005, 11:57:53 AM10/31/05
to
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.


Gunnar Hjalmarsson

unread,
Oct 31, 2005, 12:04:57 PM10/31/05
to

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Gunnar Hjalmarsson

unread,
Oct 31, 2005, 12:25:54 PM10/31/05
to
Gunnar Hjalmarsson wrote:
> Tom Van Overbeke wrote:
>
>> 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.

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.

Purl Gurl

unread,
Oct 31, 2005, 12:33:19 PM10/31/05
to
Tom Van Overbeke wrote:

(snipped)

> 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

#!perl

@Array = localtime(time - 86400);

print $Array[3], ".", $Array[4] + 1, ".", $Array[5] + 1900;


Purl Gurl

Purl Gurl

unread,
Oct 31, 2005, 2:58:00 PM10/31/05
to
Purl Gurl wrote:
> Tom Van Overbeke wrote:

(snipped)

>> But the format I need is: 30.10.05

> print $Array[3], ".", $Array[4] + 1, ".", $Array[5] + 1900;


#!perl

@Array = localtime(time - 86400);

print $Array[3], ".", $Array[4] + 1, ".0", $Array[5] - 100;


Purl Gurl

John Bokma

unread,
Oct 31, 2005, 3:17:52 PM10/31/05
to
Purl Gurl <purl...@purlgurl.net> wrote:

Which gives a cool bug in a few years.

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)

Purl Gurl

unread,
Oct 31, 2005, 3:31:40 PM10/31/05
to
Purl Gurl wrote:
> Purl Gurl wrote:
>> 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;


Purl Gurl

Glenn Jackman

unread,
Oct 31, 2005, 3:35:55 PM10/31/05
to
At 2005-10-31 11:57AM, Tom Van Overbeke <to...@hahaha.be> wrote:
> 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);

--
Glenn Jackman
NCF Sysadmin
gle...@ncf.ca

Joe Smith

unread,
Oct 31, 2005, 3:42:56 PM10/31/05
to
Purl Gurl wrote:
> ($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

Mothra

unread,
Oct 31, 2005, 3:57:46 PM10/31/05
to
Glenn Jackman wrote:
> 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 :-)

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

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

Paul Lalli

unread,
Oct 31, 2005, 4:00:13 PM10/31/05
to
Purl Gurl wrote:
> #!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. Sinan Unur

unread,
Oct 31, 2005, 4:01:04 PM10/31/05
to
Purl Gurl <purl...@purlgurl.net> wrote in news:43667F2C.5010708
@purlgurl.net:

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

--
A. Sinan Unur <1u...@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Mothra

unread,
Oct 31, 2005, 4:19:15 PM10/31/05
to
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. Sinan Unur

unread,
Oct 31, 2005, 4:22:42 PM10/31/05
to
"Mothra" <mot...@nowhereatall.com> wrote in news:43669114$1
@usenet.ugs.com:

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

Purl Gurl

unread,
Oct 31, 2005, 4:26:54 PM10/31/05
to
Purl Gurl wrote:
> Purl Gurl wrote:
>> Purl Gurl wrote:
>>> 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;

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

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

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


You will discover those code examples are well beyond
the skill levels of most regulars participating here.

If you have difficulties in five years, post and I will
instruct you how to remove the zero from my code, which
is not difficult although beyond the skill levels of most.

I have a hunch, you personally, will figure out how to
remove my zero in five years.

Purl Gurl

David K. Wall

unread,
Oct 31, 2005, 4:40:45 PM10/31/05
to
Glenn Jackman <xx...@freenet.carleton.ca> wrote:

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

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

Abigail

unread,
Oct 31, 2005, 5:36:57 PM10/31/05
to
Tom Van Overbeke (to...@hahaha.be) wrote on MMMMCDXLIV September MCMXCIII
in <URL:news:dk5ime$6le$1...@pop-news.nl.colt.net>:
^^ 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 ?

$ perl -MPOSIX -wle 'print strftime "%d.%m.%y" => localtime time - 86400'
30.10.05

Note that during 2 hours a year, subtracting 86400 seconds will *not* get
you yesterdays date (for one hour, you'll get the day before yesterday,
and for one hour, you'll get todays day).

Abigail
--
BEGIN {$^H {join "" => ("a" .. "z") [8, 13, 19, 4, 6, 4, 17]} = sub
{["", "Just ", "another ", "Perl ", "Hacker\n"] -> [shift]};
$^H = hex join "" => reverse map {int ($_ / 2)} 0 .. 4}
print 1, 2, 3, 4;

use...@davidfilmer.com

unread,
Oct 31, 2005, 5:42:06 PM10/31/05
to
Tom Van Overbeke wrote:
> 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");

--
http://DavidFilmer.com

use...@davidfilmer.com

unread,
Oct 31, 2005, 5:47:02 PM10/31/05
to
use...@DavidFilmer.com wrote:
> my $yesterday = UnixDate('yesterday', "%d.$m.$y");

sorry, make that

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

> --
> http://DavidFilmer.com

chris-...@roaima.co.uk

unread,
Oct 31, 2005, 5:22:54 PM10/31/05
to
A. Sinan Unur <1u...@llenroc.ude.invalid> wrote:
> 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

Purl Gurl

unread,
Oct 31, 2005, 6:27:18 PM10/31/05
to
Purl Gurl wrote:
> Purl Gurl wrote:
>> Purl Gurl wrote:
>>> Purl Gurl wrote:
>>>> Tom Van Overbeke wrote:

(snipped)

> You will discover those code examples are well beyond


> the skill levels of most regulars participating here.

Enough time has elapsed my presumption the regular beginners
here do not plan to afford you an explanation about problems
with daylight savings time. I did note some childishly petty
trolls, nothing of value. I also noted some system specific
codes which are not portable. Also noted is a POSIX method
which is inherently buggy and broken for none *nix systems.

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. Your best option is to code in such
a way to reduce error, and keep in mind if your timestamp
is critical, you will need to make adjustments by hand
along with hoping others involved adhere to conformity
in behavior.

A clear presumption by the beginners here is a Daylight
time change takes place at midnight. This is not so per
accepted standards. Even the standards are not consistent.
In some areas, the switch is made at two in the morning,
others at three in the morning, many areas make no change.

You will note regular beginners here dictate you must
use Daylight Savings Time compensation, although they
have no clue if you actually need to do so.

If you code for two in the morning, add add an hour for
"Spring Ahead" your date is still correct. Opposite for
"Fall Back" changes, but your date is still correct.

Regardless if two or three in the morning, for your task,
your date remains correct, until a subsequent midnight hour.
It is that Witching Hour which will bugger you. Nonetheless,
if you are not creating mission critical timestamps around
midnight, the Witching Hour is irrelevant; change your code
at your leisure, by hand or included coding which is triggered
by the localtime DST (Daylight Savings Time) binary flag.

To help you, which the beginners here tend not to do,
look at element eight in your localtime list context.
That element is a daylight savings flag. If element
eight is zero, Daylight Savings Time is not in effect.
If one, you are within Daylight Savings Time, making
a presumption you are in a region which employs such.

($dst_flag) = (localtime) [8];
print $dst_flag;

Regardless of how well you code, how many modules you
pull in, you will never get it perfect, despite claims
of the regular beginners in this group.

Below my signature is a snippet from an old FAQ,
known as Perl FAQ 4 which imparts technical information.

Exercise caution when reading articles by the regular
beginners here; they will almost always mislead you.

Purl Gurl

____

Perl FAQ 4

How do I find yesterday's date?

(snipped)

Most people try to use the time rather than the calendar to figure
out dates, but that assumes that your days are twenty-four hours each.
For most people, there are two days a year when they aren't: the switch
to and from summer time throws this off. Russ Allbery offers this solution.

sub yesterday {
my $now = defined $_[0] ? $_[0] : time;
my $then = $now - 60 * 60 * 24;
my $ndst = (localtime $now)[8] > 0;
my $tdst = (localtime $then)[8] > 0;
$then - ($tdst - $ndst) * 60 * 60;
}

Should give you "this time yesterday" in seconds since epoch relative to
the first argument or the current time if no argument is given and
suitable for passing to localtime or whatever else you need to do with
it. $ndst is whether we're currently in daylight savings time; $tdst is
whether the point 24 hours ago was in daylight savings time. If $tdst
and $ndst are the same, a boundary wasn't crossed, and the correction
will subtract 0. If $tdst is 1 and $ndst is 0, subtract an hour more
from yesterday's time since we gained an extra hour while going off
daylight savings time. If $tdst is 0 and $ndst is 1, subtract a
negative hour (add an hour) to yesterday's time since we lost an hour.

All of this is because during those days when one switches off or onto DST,
a ``day'' isn't 24 hours long; it's either 23 or 25.

The explicit settings of $ndst and $tdst are necessary because localtime only
says it returns the system tm struct, and the system tm struct at least on Solaris
doesn't guarantee any particular positive value (like, say, 1) for isdst, just a
positive value. And that value can potentially be negative, if DST information isn't
available (this sub just treats those cases like no DST).

Note that between 2am and 3am on the day after the time zone switches off daylight
savings time, the exact hour of ``yesterday'' corresponding to the current hour is
not clearly defined. Note also that if used between 2am and 3am the day after the
change to daylight savings time, the result will be between 3am and 4am of the
previous day; it's arguable whether this is correct.

This sub does not attempt to deal with leap seconds (most things don't).

Purl Gurl

unread,
Oct 31, 2005, 6:53:10 PM10/31/05
to
Abigail wrote:
> Tom Van Overbeke wrote:

(snipped)

> $ perl -MPOSIX -wle 'print strftime "%d.%m.%y" => localtime time - 86400'
> 30.10.05

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


No surprise you are not being trolled for not compensating for Daylight Savings Time.


Purl Gurl

Mothra

unread,
Oct 31, 2005, 7:23:20 PM10/31/05
to
Purl Gurl wrote:
> 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


Purl Gurl

unread,
Oct 31, 2005, 7:43:52 PM10/31/05
to
Mothra wrote:
> Purl Gurl wrote:

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

D:\storage\APACHE2\perl\bin>perl580.exe -MPOSIX -wle "print strftime '%d.%m.%y' => localtime time - 86400"
m.05

D:\storage\APACHE2\perl\bin>perl -v

This is perl, v5.8.0 built for MSWin32-x86-multi-thread

***

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

C:\APACHE\USERS\TEST>perl -v

This is perl, v5.6.1 built for MSWin32-x86-multi-thread

**

POSIX is buggy depending on operating system, not the Perl version,
or at least it seems so.

My test results are on a 9.x machine. I have not tested this on
my XP machine. Eventually, when motivated, I will fire up my XP
machine and test this presented "sometimes" broken code.

It is %d.%m.%y which varies with operating system.

This problem was discussed here years back, something about capital letters,
different letters. I want to write a W or w is needed but my memory is not
good on this. I do recall this was related to NMS sourceforge site claiming
perfect replacement scripts for Matt's, and NMS scripts being more
buggy than Matt's scripts.

Lot of heated denial but facts are facts. Some NMS scripts are broken
because the writer does not have sense enough to not use POSIX for
timestamps and related functions, without warning users about NMS
(POSIX) scripts not being cross platform portable.

Purl Gurl

A. Sinan Unur

unread,
Oct 31, 2005, 8:00:44 PM10/31/05
to
Purl Gurl <purl...@purlgurl.net> wrote in
news:4366AE66...@purlgurl.net:

> Abigail wrote:
>> Tom Van Overbeke wrote:
>
> (snipped)
>
>> $ perl -MPOSIX -wle 'print strftime "%d.%m.%y" => localtime time
>> - 86400' 30.10.05
>
> 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 ...

ax...@white-eagle.invalid.uk

unread,
Nov 1, 2005, 1:35:46 PM11/1/05
to
chris-...@roaima.co.uk wrote:
> A. Sinan Unur <1u...@llenroc.ude.invalid> wrote:
>> 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.

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

Axel

use...@davidfilmer.com

unread,
Nov 1, 2005, 4:01:50 PM11/1/05
to
Purl Gurl wrote:

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

--
http://DavidFilmer.com

Mothra

unread,
Nov 1, 2005, 4:17:49 PM11/1/05
to
use...@DavidFilmer.com wrote:
> Purl Gurl wrote:
>
>> 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
^^^^^^^^^^^^^^^
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


Purl Gurl

unread,
Nov 1, 2005, 4:47:55 PM11/1/05
to

Mothra wrote:


> usenet wrote:
>>Purl Gurl wrote:

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

> I don't know about that. from the docs:

> KNOWN BUGS

> Daylight Savings Times

> Another reason to use DateTime. DateTime will handle


> DST changes, leap seconds, leap years, other calender systems
> and much more :-)

So, you pack up your machine and move to Arizona.
Your DateTime is now broken.

It is impossible to produce any software which will handle
Daylight Savings Time with perfection. Many software will
do an ok job, no major problems while a user is aware.

You boys need to think in terms of both longitudal regions
and geopolitical regions, related to machines "talking"
to each other.

Another forgotton notion is "when" these Daylight Savings
Time changes take effect. Some change clocks a day ahead,
some the next day, and some, like myself, never change
the clocks; Windows changes its clocks, but not mine.

Litte known notion is our Congress is considering a bill
to change the effective dates of Daylight Saving Time under
a Bush idiocy guise this will cut energy costs. Whoopie.

George Bush <-> Forrest Gump

You can get it right most of the time, but not all the time.

So tell me, what happens when a machine resides in Jerusalem?

http://www.technologyreview.com/articles/05/08/ap/ap_3080805.asp?p=1

Purl Gurl

Purl Gurl

unread,
Nov 1, 2005, 5:28:12 PM11/1/05
to
Purl Gurl wrote:
> Mothra wrote:
>> usenet wrote:
>>> Purl Gurl wrote:

(snipped)

> You can get it right most of the time, but not all the time.

http://www.google.com/search?hl=en&q=tm_isdst+problem&btnG=Google+Search

Purl Gurl

From Microsoft:

// ----------------- start of sample program -------------------------

// Compiler switches needed : none.

#include <time.h>;
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

int main ()
{
struct tm ltm;
time_t tstamp = 0;

ltm.tm_year = 90; /* First valid year .*/
ltm.tm_mon = 3; /* April. */
ltm.tm_mday = 1; /* 1st of the month. */
ltm.tm_hour = 3; /* At 3:00 am. */
ltm.tm_min = 0;
ltm.tm_sec = 0;
ltm.tm_wday =0;
ltm.tm_yday = 0;

while (ltm.tm_year <= 2005)
{
ltm.tm_isdst = -1;

// mktime indirectly calls cvtdate which has the logic bug.
tstamp = mktime(&ltm);

/* If it's Sunday April 1st, it should also be DST */
if (!ltm.tm_wday && !ltm.tm_isdst)
{
printf("April 01 is the first Sunday in April, %d \
-it should be DST but tm_isdst says it's \
not!\n", 1900+ltm.tm_year);
while (!ltm.tm_isdst)
{
ltm.tm_mday++;
ltm.tm_isdst=-1;
tstamp = mktime(&ltm);
}
printf("\tDST is reported as starting on \
%d/%d/%d\n", 1+ltm.tm_mon, ltm.tm_mday, \
1900+ltm.tm_year);

ltm.tm_mday = 1;
}

ltm.tm_year++;

}

return 0;
}


// ----------------- end of sample program ---------------------------

Eric J. Roode

unread,
Nov 11, 2005, 10:01:09 PM11/11/05
to
"Tom Van Overbeke" <to...@hahaha.be> wrote in
news:dk5ime$6le$1...@pop-news.nl.colt.net:

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

It doesn't get much simpler than:

use Time::Format;
print "Yesterday was $time{'dd.mm.yy', time-86400}\n"

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`

robic0

unread,
Nov 26, 2005, 6:12:01 AM11/26/05
to
On Mon, 31 Oct 2005 17:57:53 +0100, "Tom Van Overbeke"
<to...@hahaha.be> wrote:

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

Tom, if your into thinking and roll-your-own, try this:

sub FmtTime
{
my ($ttype, $since) = @_;
$since = time() unless defined $since;
$ttype = 0 unless (defined $ttype);
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday,
$isdst) = localtime($since);
my %months = (1 => 'Jan', 2 => 'Feb', 3 => 'Mar', 4 => 'Apr',
5 => 'May', 6 => 'Jun',
7 => 'Jul', 8 => 'Aug', 9 => 'Sep', 10 => 'Oct',
11 => 'Nov', 12 => 'Dec');
return sprintf ("%s %s %d:%0.2d:%0.2d", $months{$mon+1},
$mday, $hour, $min, $sec) if ($ttype == 1);
return sprintf ("%0.2d%0.2d%4d", $mon+1, $mday, ($year+1900))
if ($ttype == 2);
return sprintf ("%d/%d/%0.2d", $mon+1, $mday, ($year-100)) if
($ttype == 3);
return sprintf ("%0.2d/%0.2d/%0.2d", $mon+1, $mday,
($year-100)) if ($ttype == 4);
if ($ttype == 5) {
my $am_pm = ($hour >= 12 && $min >= 0 && $sec >= 0) ?
"PM" : "AM";
my $hour_12 = ($hour < 13) ? $hour : $hour-12;
return sprintf ("%0.2d/%0.2d/%0.2d - %2s:%0.2d %s",
$mon+1, $mday, ($year-100), $hour_12, $min, $am_pm)
}
return sprintf ("%0.2d/%0.2d/%d", $mon+1, $mday, ($year+1900))
if ($ttype == 6);
#return sprintf ("%0.2d/%0.2d/%0.2d", $mon+1, $mday,
($year-100)) if ($ttype == 6);
return "";
}

0 new messages