For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > utc timestamp question...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author utc timestamp question...
rohan

2006-01-19, 6:58 pm

I wrote a small function to return the UTC time stamp in the following
format:

20041130T110314

is there a better/simpler way of doing this without using CPAN modules?

#returns the timestamp in in the UTC format... in string format
#example: 20041130T110314
sub getutctime(){

my $timeval;

@{$timeval}{@{[
'sec','min','hour','mday','mon','year']}
}=gmtime(time);

#UTC returns the present year assuming 1900 is base..
@{$timeval}{'year'} +=1900;

#UTC returns 0 for Jan.. etc..
@{$timeval}{'mon'} ++;

#convert all single digit dates to 2 digits by adding "0"
while( my ($k, $v) = each %$timeval ) {
if ($k ne "year"){
if ($v<10) {
@{$timeval}{$k} = "0".$v;

}
}
}

#append all the values.. year..month.. day..hour..etc in a string
my
$timestamp=@{$timeval}{year}.@{$timeval}{mon}.@{$timeval}{mday}."T"
.@{$timeval}{hour}.@{$timeval}{min}.@{$timeval}{sec};

return $timestamp;
}

Paul Lalli

2006-01-19, 6:58 pm

rohan wrote:
> I wrote a small function to return the UTC time stamp in the following
> format:
>
> 20041130T110314
>
> is there a better/simpler way of doing this without using CPAN modules?


perldoc -f localtime
perldoc POSIX (search for strftime)

$ perl -MPOSIX=strftime -le'print strftime(q{%Y%m%dT%H%M%S},
localtime)'
20060119T193520

Yes, POSIX is a standard module.

Paul Lalli

Paul Lalli

2006-01-19, 6:58 pm

Paul Lalli wrote:
> rohan wrote:
>
> perldoc -f localtime
> perldoc POSIX (search for strftime)
>
> $ perl -MPOSIX=strftime -le'print strftime(q{%Y%m%dT%H%M%S},
> localtime)'
> 20060119T193520


Sorry, you said UTC. Replace "localtime" with "gmtime".

Paul Lalli

rohan

2006-01-19, 6:58 pm

Excellent thanks..
I did see the perldoc -f localtime before posting this time and had
gotten their appraoch of

use POSIX qw(strftime);
$now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;

only thing i was not sure was weather i could use the "POSIX".. so i
wanted to try without it.. but yes.. ill keep my function if i am told
that i cant use POSIX.. else ill use the modual approach.. would be
much faster than mine...

Thanks for your time.

...R

...R

Paul Lalli

2006-01-19, 9:55 pm

rohan wrote:
> only thing i was not sure was weather i could use the "POSIX".. so i
> wanted to try without it.. but yes.. ill keep my function if i am told
> that i cant use POSIX..


POSIX is a core module. It would be absurd for anyone to tell you it
can't be used for anything other than academic purposes.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com