develooper Front page | perl.perl5.porters | Postings from March 2000

Re: [ID 20000307.005] Date Problem

Thread Previous | Thread Next
From:
Gregor N. Purdy
Date:
March 7, 2000 08:47
Subject:
Re: [ID 20000307.005] Date Problem
Message ID:
4.3.2.20000307113808.0191be10@ipass.one.net
David--

Try this:

my @t = localtime;
$weekday = (Sun,Mon,Tue,Wed,Thur,Fri,Sat)[$t[6]];
$day = $t[3];
$month = (January,February,March,April,May,June,July,August,September,October,November,December)[$t[4]];
print "It is $weekday the $day th of $month\n";

You were using $t[3] for month number instead of $t[4].

 From the localtime man page:

           int  tm_mday;       /* day of the month - [1, 31] */

Simplifying and getting correct $day.

I also changed to call localtime only once to prevent multiple
calls straddling midnight.

Regards,

--Gregor

At 11:24 AM 3/7/2000 -0500, David Gillen wrote:
>Problem. Perl not showing the day correctly, Eg today is Tue March the 7th,
>the program get Tue March the 8th. Seems to be due to the 29 Feb Problem for
>this year.
>
>Regards
>David Gillen
>
>
>
>
>
>Example Code: (Don't laugh I know it ugly)
>
>$weekday = (Sun,Mon,Tue,Wed,Thur,Fri,Sat)[(localtime)[6]];
>$day =
>(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,
>29,30,31)[(localtime)[3]];
>$month =
>(January,February,March,April,May,June,July,August,September,October,Novembe
>r,December)[(localtime)[3]];
>print "It is $weekday the $day th of $month";
>
>
>
>
>Output of perl -V
>
>Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration:
>   Platform:
>     osname=freebsd, osvers=4.0-current, archname=i386-freebsd
>     uname='freebsd freefall.freebsd.org 4.0-current freebsd 4.0-current #0:
>$Date: 1999/05/05 19:42:40 $'
>     hint=recommended, useposix=true, d_sigaction=define
>     usethreads=undef useperlio=undef d_sfio=undef
>   Compiler:
>     cc='cc', optimize='undef', gccversion=egcs-2.91.66 19990314 (egcs-1.1.2
>release)
>     cppflags=''
>     ccflags =''
>     stdchar='char', d_stdstdio=undef, usevfork=true
>     intsize=4, longsize=4, ptrsize=4, doublesize=8
>     d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
>     alignbytes=4, usemymalloc=n, prototype=define
>   Linker and Libraries:
>     ld='cc', ldflags ='-Wl,-E'
>     libpth=/usr/lib
>     libs=-lm -lc -lcrypt
>     libc=/usr/lib/libc.so, so=so, useshrplib=true, libperl=libperl.so.3
>   Dynamic Linking:
>     dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
>     cccdlflags='-DPIC -fpic', lddlflags='-shared'
>
>
>Characteristics of this binary (from libperl):
>   Built under freebsd
>   Compiled at Jan 25 2000 22:13:05
>   @INC:
>     /usr/libdata/perl/5.00503/mach
>     /usr/libdata/perl/5.00503
>     /usr/local/lib/perl5/site_perl/5.005/i386-freebsd
>     /usr/local/lib/perl5/site_perl/5.005
>     .

+--------------------------------------------------------------+
| Gregor N. Purdy                     gregor@focusresearch.com |
|                                                              |
| If I fell in the woods, and nobody was around, would I tell? |
+--------------------------------------------------------------+


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About