time configuration for localtime

R

Rich Bogle

I need to format the output from localtime below to have a 2 digit year, 2
digit month, and 2 digit day. If my memory serves right I believe there are
formatting options for localtime but cannot find reference to them. If they
don't exist I could format this the long way. I need the above format to
compare to folders that are named by the above date format.


sub currentdate {
#Get Current Date / Time
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year = $year + 1900;
$mon = $mon + 1;
$curdate = $year . $mon . $mday;
print $curdate . "\n";
}


Thanks,

Rich
 
J

John J. Trammell

I need to format the output from localtime below to have a 2 digit
year, 2 digit month, and 2 digit day.

How about something along the lines of:

use POSIX 'strftime';
my $curdate = strftime("%y%m%d",localtime);
 
G

Gunnar Hjalmarsson

Rich said:
I need to format the output from localtime below to have a 2 digit
year, 2 digit month, and 2 digit day. If my memory serves right I
believe there are formatting options for localtime but cannot find
reference to them.

There are quite a few modules available that may help you format
dates; John mentioned one of them. Check out CPAN.
If they don't exist I could format this the long way. I need the
above format to compare to folders that are named by the above date
format.

sub currentdate {
#Get Current Date / Time
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year = $year + 1900;
$mon = $mon + 1;
$curdate = $year . $mon . $mday;
print $curdate . "\n";
}

That does not match your description since it prints a 4 digit year
and does not format the month and day to 2 digits.

Nevertheless, IMO "the long way" isn't very long in this case. And it
can be written even shorter:

sub currentdate {
my ($day,$mon,$year) = (localtime)[3..5];
printf "%02d%02d%02d\n", $year % 100, $mon + 1, $day;
}
 
R

Rich Bogle

John J. Trammell said:
How about something along the lines of:

use POSIX 'strftime';
my $curdate = strftime("%y%m%d",localtime);

This option is perfect. strftime must be what I was thinking of. I will
just add a %H%M into the format and I will be good to go.

THANKS!
 
R

Rich Bogle

John J. Trammell said:
How about something along the lines of:

use POSIX 'strftime';
my $curdate = strftime("%y%m%d",localtime);

I now have:

sub daterange {
#Get Date / Time 1 year ago
use POSIX;
$format = "%y%m%d%H%M";
$curdate = strftime($format, localtime);
print $curdate . "\n";
#Date one year ago
$curdate = $curdate - 100000000;
print $curdate . "\n";

}

I need the date for one year ago, however if I do math on a year alone or as
above the leading 0 is eliminated. I need to preserver this leading 0. Do
you know of any options to do so. printf was mentioned in another post, but
is not suitable as I need to hold the info in a variable for some other
operations.

Thanks,

Rich
 
G

Gunnar Hjalmarsson

Rich said:
printf was mentioned in another post, but is not suitable as I need
to hold the info in a variable for some other operations.

Then you can use sprintf() instead.
 
T

Tore Aursand

I need to format the output from localtime below to have a 2 digit year,
2 digit month, and 2 digit day. If my memory serves right I believe
there are formatting options for localtime but cannot find reference to
them.

No? Why didn't you read 'perldoc -f localtime', then? If _my_ memory
serves me right, there is an example there using 'sprintf' to format the
year to only show two digits.
sub currentdate {
#Get Current Date / Time
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year = $year + 1900;
$mon = $mon + 1;
$curdate = $year . $mon . $mday;
print $curdate . "\n";
}

Untested:

sub currentdate {
my ($day, $month, $year) = (localtime)[3..5];
printf('%02d%02d%02d', $year % 100, $month + 1, $day);
}
 
G

Glenn Jackman

Rich Bogle said:
I need the date for one year ago, however if I do math on a year alone or as
above the leading 0 is eliminated. I need to preserver this leading 0. Do
you know of any options to do so. printf was mentioned in another post, but
is not suitable as I need to hold the info in a variable for some other
operations.

Manipulate the year in the list returned by localtime.

use POSIX 'strftime';
my @time = localtime;
my $fmt = '%y%m%d%H%M';

my $today = strftime($fmt, @time), "\n";

$time[5]--;
my $lastyear = strftime($fmt, @time), "\n";
 

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


Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top