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!

printing the time

Status
Not open for further replies.

leo2180

IS-IT--Management
Jun 7, 2000
52
US
hey people.&nbsp;&nbsp;Im looking for a way to print the current time to a file in perl.&nbsp;&nbsp;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.&nbsp;&nbsp;<br><br>I would say (Sun,Mon.....) [(localtime)[4]] to print out the current day in words.&nbsp;&nbsp;Now I know that theres got to be an easier way to do this other than that.&nbsp;&nbsp;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)?<br><br>&nbsp;
 
leo<br><br>I know you were saying about too many lines of code but:<br><br><FONT FACE=monospace>print scalar localtime time . &quot;\n&quot;;</font><br><br>will print:<br><FONT FACE=monospace><b><br>Mon Jul 10 08:34:18 2000<br></font></b><br>So if you then have a file open just pre-pend the file handle:<br><br>e.g.<br><FONT FACE=monospace>print OUTFILE scalar localtime;</font><br><br>Which doesn't have a newline - which would be more useful for a timestamp in a file for example. <br><br>Hope that helps!<br>Loon
 
there is a cool module for working with time<br>look for Date::Manip at CPAN
 
thanks guys, but what if I didnt want to print it in army time?&nbsp;&nbsp;Is there a way to print the time in regular time?<br><br><br>Or would I have to manipulate that with perl's macthing ability.. (/ /);<br><br><br>&nbsp;
 
Erm, what's the formats for 'army' time and 'regular' time?<br>
 
Army time goes to 24 in hours, while 'regular time' goes from 1 - 12.&nbsp;&nbsp;12 midnight is something like 00:00 hours if Im not mistaken.&nbsp;&nbsp;People would't understand that, well people that don't really know about army time.
 
I understand, you mean 12hr and 24hr clocks. What about:<br><FONT FACE=monospace><br>($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdat)=localtime(time);<br>if ($hour &gt; 12) { $hour-=12; }<br></font><br>how about that? Then you can format the way it is displayed anyway you want! :)<br><br>Hope that helps!<br>..Thanks to Programming Perl, O'Reilly & Assoc...<br><br>Loon<br>
 
Hmmm...&nbsp;&nbsp;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?<br><br>I'd leave it in 24 hour clock format.&nbsp;&nbsp;If someone doesn't understand that &quot;21:45&quot; is 9:45pm, then should you be letting them near a PC? ;^) <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
I understand what you meen, but Im trying to make this dummy proof as possible.&nbsp;&nbsp;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.&nbsp;&nbsp;For example roll your mouse pointer over the time at the lower right hand corner of your screen thats if your on a pc.&nbsp;&nbsp;Now that's how I want it to look asthetically, with the date and all.&nbsp;&nbsp;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.<br><br>Thanks for your input guys...
 
Hehe, quite right AndyBo :) must have been the cold I've got affecting my brain.. (*honest*!)<br><br>Leo, on my Win NT machine the time is in format:<br><br><FONT FACE=monospace>HH:MM xx</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where xx is either <FONT FACE=monospace>AM</font> or <FONT FACE=monospace>PM</font>.<br><br>Then when I move my mouse pointer over I get:<br><br><FONT FACE=monospace>Thursday, July 13, 2000</font><br><br>If <b>you're</b> not displaying seconds could you:<br><br><FONT FACE=monospace>if ($hour &gt; 12) { $hour-=12; $secs=PM; }<br>else { $secs=AM; }</font><br><br>Good luck with it all!<br>Loon<br><br><br>&nbsp;&nbsp;<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top