[newbie] Year in two digits; Time with leading zero

G

Gilles Ganault

Hello

As indicated in the subject line, I don't Perl, but I must
write a short script in it.

I just need to get the current year in two digits, and time should be
HH:MM with leading zeros if needed, ie. 10:01 instead of 10:1.

Here's what I found:

------------------------------
($min, $hrs, $day, $month, $year) = (localtime) [1,2,3,4,5];

#BAD : 2007
$currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year+1900);
#BAD : 107!
$currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year);
print $currentdate . "\n";

#BAD : 10:1 instead of 10:01
$currenttime = sprintf("%d:%d", $hrs,$min);
print $currenttime . "\n";
 
T

Thomas J.

Youre using "leading zero"-format for $currentdate, why not for
$currenttime ?
for 2-digit-date use the modulo-function (%)

$currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year % 100);
$currenttime = sprintf("%02d:%02d", $hrs,$min);

hth, Thomas
 
T

Tim Southerwood

Gilles said:
Hello

As indicated in the subject line, I don't Perl, but I must
write a short script in it.

I just need to get the current year in two digits, and time should be
HH:MM with leading zeros if needed, ie. 10:01 instead of 10:1.

Here's what I found:

------------------------------
($min, $hrs, $day, $month, $year) = (localtime) [1,2,3,4,5];

#BAD : 2007
$currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year+1900);
#BAD : 107!
$currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year);
print $currentdate . "\n";

#BAD : 10:1 instead of 10:01
$currenttime = sprintf("%d:%d", $hrs,$min);
print $currenttime . "\n";
------------------------------

Should I use an other library?

Thank you.

You could "use POSIX" and make use of the standard strftime() function
assuming you are on a POSIX compliant system where that works.

But there's nothing wrong with your method either - I'd use either with no
sense that one is better or worse than the other. Your method is definately
portable, which I seldom care about because I work with nothing but *nix,
but I'm an old grumbleweed and portability is good...Honest :)

Cheers

Tim
 
D

Dr.Ruud

Gilles Ganault schreef:
As indicated in the subject line, I don't Perl, but I must
write a short script in it.

I just need to get the current year in two digits, and time should be
HH:MM with leading zeros if needed, ie. 10:01 instead of 10:1.

Here's what I found:

------------------------------
($min, $hrs, $day, $month, $year) = (localtime) [1,2,3,4,5];

#BAD : 2007
$currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year+1900);
#BAD : 107!
$currentdate = sprintf("%02d/%02d/%02d", $day, $month+1, $year);
print $currentdate . "\n";

How about "$year % 100"?

#BAD : 10:1 instead of 10:01
$currenttime = sprintf("%d:%d", $hrs,$min);
print $currenttime . "\n";

You forgot the "02" parts:

$currenttime = sprintf("%02d:%02d", $hrs, $min);


#!/usr/bin/perl
use strict;
use warnings;

my ($min, $hrs, $day, $month, $year) = (localtime)[1..5];

my $currentdate = sprintf( "%02d/%02d/%02d",
$day,
$month + 1,
$year % 100,
);

print "$currentdate\n";

my $currenttime = sprintf( "%02d:%02d",
$hrs,
$min,
);
print "$currenttime\n";
__END__

Consider the ISO 8601 date format:
http://en.wikipedia.org/wiki/ISO_8601
 
T

Tony Curtis

Gilles said:
Hello

As indicated in the subject line, I don't Perl, but I must
write a short script in it.

I just need to get the current year in two digits, and time should be
HH:MM with leading zeros if needed, ie. 10:01 instead of 10:1.

I'd go with strftime from POSIX, e.g.

use POSIX qw( strftime );

my $now = strftime( '%y %H:%M', localtime() );

print "$now\n";

Nicely mnemonic solution.

hth
t
 
P

Peter J. Holzer

Gilles Ganault said:
I just need to get the current year in two digits,

This is simple:

($min, $hrs, $day, $month, $year) = (localtime) [1,2,3,4,5];
$currentdate = sprintf("%02d/%02d/%02d", $day, $month+1,
substr($year,-2,2));
^^^^^^^^^^^^^^^^^^
That's just plain ugly. What's wrong with ($year % 100)?

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

localtime(time()) 16
past time 2
Returned wrong Year 2
What date was so many months and years before 18
time configuration for localtime 8
time format +1 hour 15
Help with a script.. 10
PERL Skipping a Call to a Sub. 6

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top