Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the day of the week - perl 1

Status
Not open for further replies.

VolatileAcid

Programmer
Jan 24, 2003
4
GB
Hi,

got a small problem/question here with regards to getting the day of the week.

To get the day of the week I use:

$day=(Sun,Mon,Tue,Wed,Thu,Fri,Sat,Sun)[(localtime(time))[6]];

which returns the correct day of the week, however, my script needs the following library and declares:

use Time::localtime;

which causes my 'day of the week' to always be returned as sunday ? :( !?

help me !
 
Is sunday meant to be in the list twice ?
 
I do apologise, no it's not :- that should read:

$day=(Sun,Mon,Tue,Wed,Thu,Fri,Sat)[(localtime(time))[6]];
 
Try:

printf "Day is %d\n", localtime->wday();

then map that to a day of the week.
 
thanks Tony,

I appreciate you taking the time out to respond - it works!

THANKS!!
 
I'm not sure what the "use Time::localtime" is doing but I did some testing and found that localtime(time)[6] is empty but position [0] = Time::tm=ARRAY(0x160061c)

oh well at leat you got the result you want in the end, regardless of what is going on there.
 
Just to explain - when you use the Time::localtime module, it exports its own localtime() function, which overrides the normal Perl one. Thus, the behaviour you'd expect from Perl's native localtime() isn't going to happen, as it's the function in the Time::localtime module that's being called instead.

Hope that made sense.
 
modules, modules, modules

why not just use `date`? (backticks)

... and steal the first 3 characters?


Kind Regards
Duncan
 
why on earth did someone write a module to do the same as an already existing function.
 
With the builtin localtime() function, you have to rely on remembering what order the data is output in. The module is so that you can refer to them by name i.e. - rather than having to remember that `year' is returned as element 5 in the list, you can get it with localtime->year().
 
how about someone rewrite the built in localtime() function to be more user friendly!

but still work with the old way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top