Re: Date
by redcloud (Parson) on Mar 12, 2001 at 00:14 UTC
|
Okay, without using modules:
my ($day, $month, $year, $filename);
(undef, undef, undef, $day, $month, $year, undef) = localtime(time() -
+ (24*60*60));
$filename = sprintf("%02d-%02d-%04d.txt", $day, $month+1, $year+1900);
Season to taste.
And, yes, you really should learn to use modules.
They're invaluable.
| [reply] [d/l] |
|
With the tiny caveat that this'll fail during one hour a year if you're honoring daylight savings time. If you think about this a minute or two, there's an easy workaround (which I'll leave as an exercise :)
Retracted. See below.
| [reply] |
|
Wouldn't it fail two hours each year? If it is within the first hour of the day after a 23 hour day, 24 hours ago will be two days ago. If it is within the last hour of a 25 hour day, 24 hours ago will be the same day.
| [reply] |
|
|
Re: Date
by Masem (Monsignor) on Mar 11, 2001 at 23:53 UTC
|
Take a look at the CPAN Module Date::Calc -- it can do 'delta' calculations for standard dates.
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
| [reply] |
Re: How to find yesterdays date (was: Date)
by merlyn (Sage) on Mar 12, 2001 at 15:09 UTC
|
There's always the old standby (joke) answer:
my $yesterdays_date = localtime;
sleep 86400;
print "Yesterday, it was $yesterdays_date\n";
And of course, this code breaks near the DST boundaries.
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] |
|
If you're going to dust this one off, you could also
mention the standard optimisation:
my $yesterdays_date = localtime;
sleep 86400;
print "Yesterday, it was $yesterdays_date\n";
sleep -86400;
Yes, yes, I know it's sad...
Update: Can't claim credit... I believe that
belong to Ilya Zakharevich, otherwise it would have
been even sadder, and I wouldn't've dared post. | [reply] [d/l] |
Re: Date
by CiceroLove (Monk) on Mar 12, 2001 at 00:01 UTC
|
You really should learn how to use modules. They will save you countless hours contemplating suicide. Having said that, you need to look at using
$date = scalar localtime(time);
See what this prints out for you and then go from there. You will have to do things like splitting but without modules, I can't think of another way off the top of my head.
CiceroLove
Fates! We will know your pleasures: That we shall die, we know; 'Tis but the time, and drawing days out, that men stand upon. - Act III,I, Julius Caesar | [reply] [d/l] |
Re: How to find yesterdays date (was: Date)
by dneedles (Sexton) on Mar 12, 2001 at 10:00 UTC
|
Hello,
One last tidbit. I'm not sure of your exact situation
but your question hit on an 'admin' issue. One idea to
ease administration would be to reverse the file schema
to Year-Month-Day. That way when you sort it it will
be chronological (if you use 1999,2000, and numerics for
month).
Just a thought 8~) | [reply] |
|
Agreed. Also if you have any employees like me who come
from outside of the US (Canada in my case) there is an
excellent chance that they will be confused by dd-mm-yyyy
formats. I grew up with mm-dd-yyyy and it can be very
confusing. But nobody is particularly confused by
yyyy-mm-dd.
| [reply] |
|
Okay, why are so many Americans writing dates as m-d-yy and mm-dd-yy?? Am I supposed to be able to figure out what 02-03-01 is supposed to mean??
/me runs screaming down the hall, hands waving in the air, bangs into the wall at the end, and collapses in a heap.
-
tye
(but my friends call me an ambulance)
| [reply] |
Re: Date
by JP Sama (Hermit) on Mar 11, 2001 at 23:59 UTC
|
"today - 1" *g*
the little effort of "reading about it", and you probably have the program already done!
Good luck anyway!
#!/jpsama/bin/perl -w
$tks = `mount`;
$jpsama = $! if $!;
print $jpsama;
| [reply] |
Re: How to find yesterdays date (was: Date)
by DeaconBlues (Monk) on Mar 12, 2001 at 08:59 UTC
|
my $SECS_IN_A_DAY = 86400; # 60 secs * 60 mins * 24 hrs
my ($day, $month, $year) = (localtime(time-$SECS_IN_A_DAY))[3..5];
$month++;
$year += 1900;
# $month = "0".$month if (length($month) == 1); # for dd-mm-yyyy.txt f
+iles.
# $day = "0".$day if (length($day) == 1); # for dd-mm-yyyy.txt files.
print "File is $day-$month-$year.txt\n";
| [reply] [d/l] |
|
As mentioned over in the Dates thread, this will give you the wrong answer at least one hour a year if you're living in a timezone that switches to and from daylight savings time.
Update: After some research into this (and an experiment with Time::Local), I'm retracting my claim above, at least for the U.S. We do the timezone switch by either repeating 1am on the first Sunday of April, or by dropping 2am on the last Sunday of October. In either case, determining yesterday's date (but not time) can be done safely by subtracting 24 hours from the current time.
Subtracing 24 hours will indeed give the wrong answer
during a 1 hour window each year in the U.S. if you're
in an area that's doing a daylight savings switch.
Sat Mar 31 22:00:00 2001 < Sun Apr 1 23:00:00
Sat Mar 31 23:00:00 2001 < Mon Apr 2 00:00:00 *
Sun Apr 1 00:00:00 2001 < Mon Apr 2 01:00:00
| [reply] [d/l] |
Re: Date
by Anonymous Monk on Mar 12, 2001 at 02:01 UTC
|
Thanks redcloud, thats great...
The month is printing out as 03 is there a way to change it to 3 ?
Thanks.
| [reply] |
|
Yes, if you want to remove the leading zero, you can change the
%02d in the sprintf command to %d. You
probably need to look at whether you'll have to do this for
the day, too. (And the %04d for the year was just
overkill on my part, unless you've got files over
1001 years old. 8^P )
| [reply] |
Re: How to find yesterdays date (was: Date)
by Beatnik (Parson) on Mar 12, 2001 at 13:24 UTC
|
Isn't this one of those classic questions, where one of those classic answers might be :
scalar(localtime(time-86400))
Altho I'm pretty sure there are many other "classic answers" those this one.
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur. | [reply] [d/l] |
Re: Date
by Anonymous Monk on Mar 11, 2001 at 23:55 UTC
|
I dont really know how to use modules, is there another way to do this in code ? | [reply] |
|
modules are easier to use than writing your own code, just take a look at the heads in the module to get a grip of how to use it, and use the perldoc and eventually you will learn to use them always and whenever you get the chance, that is if you really enjoy programming and want to continue your way through Perl.
Chady | http://chady.net/
| [reply] |