Small date question

T

Tuxedo

I would like to return the date and use in daily file names as in
somefile-2009-09-03.txt etc. Anyway. the date part is where I got stuck....

Where in a Bash shell I would simply do: ...

date +%Y-%m-%d

.... to return 2009-09-03

How could the same be done in a fairly short Perl code and without use of
modules? (I guess by the localtime builtin procedures).

Any examples would be much appreciated.

Thanks,
Tudedo
 
J

Jürgen Exner

Tuxedo said:
I would like to return the date and use in daily file names as in
somefile-2009-09-03.txt etc. Anyway. the date part is where I got stuck....

Where in a Bash shell I would simply do: ...

date +%Y-%m-%d

... to return 2009-09-03

How could the same be done in a fairly short Perl code

One way:
use POSIX qw(strftime);
my $today = strftime "%Y-%m-%d", localtime;
and without use of modules?

Why would you want to tie your hands behind your back and wear a
blindfold?
(I guess by the localtime builtin procedures).

Oh, if you already knew the answer, then why were you asking?

Yes, indeed you could do it that way if you absolutely wanted to
reinvent the wheel. Just capture the 4th, 5th and 6th elements of the
return value of localtime, adjust for the different starting values as
described in the man page of localtime, and use sprintf to enforce your
desired 4-2-2 digit format.

jue
 
T

Tuxedo

Ben Morrow wrote:

[...]
just points you to the C documentation for the same function; if you are
on a system that doesn't have C documentation installed, it's easy to
find it on the Web.

Thanks for pointing this out!
 
T

Tuxedo

Tad J McClellan wrote:

[...]

sub today {
my($day, $mon, $year) = (localtime)[3,4,5];

return sprintf "%4d-%02d-%02d", $year+1900, $mon+1, $day;
} # end sub today

Thanks for this example, it works perfectly, and is just what I needed!
 
T

Tuxedo

Jürgen Exner wrote:

[...]
One way:
use POSIX qw(strftime);
my $today = strftime "%Y-%m-%d", localtime;


Why would you want to tie your hands behind your back and wear a
blindfold?

I mean modules that aren't included in perl as pre-installed standard
modules. If a module is already included, that's good. But I don't want the
hassle of installing a variety of modules for only small script procedures
unless it is really needed, for easier portability.
Oh, if you already knew the answer, then why were you asking?

Far from being well versed in perl, I wasn't sure and wanted suggestions by
those who know, like yourself. The only way to know by experience without
having the experience :)

I definitely didn't know the answer. I only guessed.
Yes, indeed you could do it that way if you absolutely wanted to
reinvent the wheel. Just capture the 4th, 5th and 6th elements of the
return value of localtime, adjust for the different starting values as
described in the man page of localtime, and use sprintf to enforce your
desired 4-2-2 digit format.

Thanks for this and the above POSIX example!
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top