Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing format of "localtime" ? 1

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi,

I'm trying to make this code:

Code:
	my @stats = stat("$folder/$file");
	my $date = localtime $stats[9];

..so that instead of showing something like:

Tue Aug 16 19:23:30 2005

..I want more like:

2008-10-15 12:43:00

Is this even possible?

TIA!

Andy
 
Hi,

Thanks - I ended up using:

Code:
	my @stats = stat("$folder/$file");
	my @date = split / /, localtime $stats[9];
 
    my $date = qq|$date[4] $date[1] $date[2] $date[3]|;

Cheers
 
Did you look at perldoc -f localtime?
Code:
my @stats = stat("$folder/$file");
my $date = format_time($stats[9]);

sub format_time {
	my @time = localtime $_[0];
	$time[4]+=1;	# Adjust Month
	$time[5]+=1900;	# Adjust Year
	return sprintf '%4i-%02i-%02i %02i:%02i:%02i', @time[reverse 0..5];
}
 
Hi,

Nah, afraid I didn't. I'll have to remember that command - looks like it will be helpful in the future :)

For what I need ATM, the above code I came up with is working perfectly =)

Cheers

Andy
 
By the way, the last line could be shorter :
CODE
my $date = qq|@date[4,1,2,3]|;

Would that add spaces too? (or just join them as one string)

Cheers

Andy
 
Just my two cents

to complete the above examples of displaying time. Using Posix "strftime", one can easy display date & time in any form and way, either by fixed parameter of by a global setting like ($config{'timezonediff'}* 3600), where $config{'timezonediff'} would add or deduct eventual time difference between real Server time & Site Location time .

$config{'timezonediff'} = '1'; # +1 hour

# This line
# use POSIX qw(strftime); # Perl interface to IEEE Std 1003.1
# goes usually here:
-----
#!/usr/bin/perl
use POSIX qw(strftime);
use strict;
e.t.c.
...
...

-----

# Use something like this to define Content of the variable "$date"

# POSIX TIME SAMPLE german Time Version with text "um & Uhr"
my $date = strftime "%d.%m.%y um %T Uhr",gmtime($stats[9]);

# POSIX TIME SAMPLE US AM/PM-Time + $variable "systemtime"
my $date = strftime "%a %b %d, %I:%M %p", gmtime($stats[9] + ($config{'timezonediff'} * 3600 ));

-----

Related Links:


Sorry for my bad english...

Ernie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top