Solved

Help with Date in perl date script.

Posted on 2006-07-17
4
400 Views
Last Modified: 2012-06-27
Hi All,

I could do with some help...

I have a perl script which prints the time and todays date:

@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
$theTime = "$hour:$minute, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";


When it prints the time, ten minutes to two O'clock and 23 milliseconds for example it prints : 13:50:23 and this is fine

But when the time is say, five minutes past two o'clock and 8 milliseconds for example it prints:

14:5:8 and this looks awfull.

Basically whenever either the minute or milliseconds time is less than 10 (two digits) it only prints one digit what i need it to print is: 14:05:08 for example - put a zero before the digit if less than 10.

How do i change the above script to print the corretly formatted time?

P.s i have shortented my example but i do want the full day month and year including in as well.

Thanks
0
Comment
Question by:spiroosx
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 175 total points
ID: 17122234
Use sprintf instead of to format your $theTime string:


$theHour = sprintf("%02u:%02u",$hour,$minute);
0
 

Expert Comment

by:harry_bela
ID: 17122545
Thanks adam,

That works great.

0
 

Author Comment

by:spiroosx
ID: 17122563
Adam314 thanks for your help.

SpiroOSX
0
 
LVL 39

Expert Comment

by:Adam314
ID: 17123396
glad it works.  For more formatting options, see here:
http://perldoc.perl.org/functions/sprintf.html
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL Web Database 5 115
stftime format 4 55
problem with using the glob function 1 69
parse a file and get data out 11 42
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question