Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

printing the time

printing the time

printing the time

hey people.  Im looking for a way to print the current time to a file in perl.  I've used the localtime function but to get it to print out, for example Saturday July 8 2000 I would have to write too many lines of code.  

I would say (Sun,Mon.....) [(localtime)[4]] to print out the current day in words.  Now I know that theres got to be an easier way to do this other than that.  Is there a way to do this in one line of code, to print out the current date in this format (Saturday, July 8, 2000)?


RE: printing the time


I know you were saying about too many lines of code but:

print scalar localtime time . "\n";

will print:

Mon Jul 10 08:34:18 2000

So if you then have a file open just pre-pend the file handle:

print OUTFILE scalar localtime;

Which doesn't have a newline - which would be more useful for a timestamp in a file for example.

Hope that helps!

RE: printing the time

there is a cool module for working with time
look for Date::Manip at CPAN

RE: printing the time

thanks guys, but what if I didnt want to print it in army time?  Is there a way to print the time in regular time?

Or would I have to manipulate that with perl's macthing ability.. (/ /);


RE: printing the time

Erm, what's the formats for 'army' time and 'regular' time?

RE: printing the time

Army time goes to 24 in hours, while 'regular time' goes from 1 - 12.  12 midnight is something like 00:00 hours if Im not mistaken.  People would't understand that, well people that don't really know about army time.

RE: printing the time

I understand, you mean 12hr and 24hr clocks. What about:

if ($hour > 12) { $hour-=12; }

how about that? Then you can format the way it is displayed anyway you want! :-)

Hope that helps!
..Thanks to Programming Perl, O'Reilly & Assoc...


RE: printing the time

Hmmm...  Loon's code will work for converting 24hr clock times to 12 hour clock times. Though, how do you tell the difference between 1am and 1pm?

I'd leave it in 24 hour clock format.  If someone doesn't understand that "21:45" is 9:45pm, then should you be letting them near a PC? ;^)

0 1 - Just my two bits

RE: printing the time

I understand what you meen, but Im trying to make this dummy proof as possible.  I had the same code as loon did but I found that I was tyoing too much, just to customize it to the way I want it to look.  For example roll your mouse pointer over the time at the lower right hand corner of your screen thats if your on a pc.  Now that's how I want it to look asthetically, with the date and all.  I have the code written to do that but but for the time, I guess I'll have to leave it in 24 hour clock format for now, till I know ho to make it tell whether it's am or pm.

Thanks for your input guys...

RE: printing the time

Hehe, quite right AndyBo :-) must have been the cold I've got affecting my brain.. (*honest*!)

Leo, on my Win NT machine the time is in format:

HH:MM xx     where xx is either AM or PM.

Then when I move my mouse pointer over I get:

Thursday, July 13, 2000

If you're not displaying seconds could you:

if ($hour > 12) { $hour-=12; $secs=PM; }
else { $secs=AM; }

Good luck with it all!


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close