Tags: current, digits, helloas, indicated, leading, line, mustwrite, perl, programming, script, subject, time, zero
Year in two digits; Time with leading zero
On Programmer » Perl
7,034 words with 8 Comments; publish: Sun, 18 May 2008 21:51:00 GMT; (20046.02, « »)
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.
http://perl.itags.org/q_perl_92257.html
All Comments
Leave a comment...
- 8 Comments

- Gilles Ganault wrote:
> 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
#1; Sun, 18 May 2008 21:53:00 GMT

- 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
Affijn, Ruud
"Gewoon is een tijger."
#2; Sun, 18 May 2008 21:54:00 GMT

- On Fri, 18 May 2007 11:18:45 +0200, "Dr.Ruud"
<rvtol+news.perl.itags.org.isolution.nl> wrote:
>How about "$year % 100"?
[...]
>You forgot the "02" parts:
That did it. Thanks!
#3; Sun, 18 May 2008 21:55:00 GMT

- "Gilles Ganault" <nospam.perl.itags.org.nospam.com> pse v diskusnm prspevku
news:0mnq431eosb3upimj6g4g7jht6tp752ccs.perl.itags.org.
4ax.com...
> 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.
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));
print $currentdate . "\n";
$currenttime = sprintf("%02d:%02d", $hrs,$min);
print $currenttime . "\n";
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
#4; Sun, 18 May 2008 21:56:00 GMT

- Gilles Ganault wrote:
> 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
#5; Sun, 18 May 2008 21:57:00 GMT

- On 2007-05-18 14:13, Petr Vileta <stoupa.perl.itags.org.practisoft.cz> wrote:
> "Gilles Ganault" <nospam.perl.itags.org.nospam.com> p_se v diskusn_m pr_spevku
> news:0mnq431eosb3upimj6g4g7jht6tp752ccs.perl.itags.org.
4ax.com...
> 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
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Symin WSR | with an emu on his shoulder.
| | | hjp.perl.itags.org.hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
#6; Sun, 18 May 2008 21:58:00 GMT

- "Peter J. Holzer" <hjp-usenet2.perl.itags.org.hjp.at> p_?e v diskusn_m p?_spěvku
news:slrnf4rumd.9f1.hjp-usenet2.perl.itags.org.zeno.hjp.at...
> On 2007-05-18 14:13, Petr Vileta <stoupa.perl.itags.org.practisoft.cz> wrote:
> ^^^^^^^^^^^^^^^^^^
> That's just plain ugly. What's wrong with ($year % 100)?
>
This is my groove only ;-) Yes in this case are both the same, but you can
write it like this:
($min, $hrs, $day, $month, $year) = (localtime) [1,2,3,4,5];
$currentdate = sprintf("%02d/%02d/%s", $day, $month+1, substr($year,-2,2));
--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
#7; Sun, 18 May 2008 21:59:00 GMT

- On Fri, 18 May 2007 10:40:32 -0400, Tony Curtis
<tony_curtis32.perl.itags.org.yahoo.com> wrote:
>I'd go with strftime from POSIX, e.g.
Thanks for the alternative solution.
#8; Sun, 18 May 2008 22:00:00 GMT