Converting Unix time stamp to a date format.

Victor Odhner plug-discuss@lists.plug.phoenix.az.us
Wed, 15 Jan 2003 20:16:03 -0700


The "localtime" or "gmtime" function breaks down the
timestamp into its component parts.  See below for
a Perl function that generates a full timestamp,
just for example.  (These functions are similar in
Perl to the corresponding C functions.)

Vic

Carl Parrish wrote:
> Does anyone know where I can find the formula for converting a timestamp
> to a date format? I can't use any of the functions I typically would. I
> get the timestamp in an xml format. So far I can't find a xslt formula
> but I can hack one up in mozilla if I can figure out how. 
>  

#!/usr/local/bin/perl
#
# TITLE timestamp.pl - Emits a timestamp to STDOUT, for use
# by shell scripts.  Returns current local time in ISO format
# minus the time zone.
#
# Output from the shell script below was:
#      TIME = [1999-07-13T11:29:42]
#
# Sample usage:
#       #!/bin/ksh
#       X=`./timestamp.pl`
#       echo "TIME = [$X]"
#
#
     my ( $sec, $min, $hour, $mday, $mon, $year );

     ( $sec, $min, $hour, $mday, $mon, $year, undef, undef, undef ) =
         localtime( time );

     printf( "%0.4d-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d\n",
         $year + 1900, $mon + 1, $mday, $hour, $min, $sec );

     exit 0;

-- 

Vic

http://members.cox.net/vodhner/
   -- or --
http://www.newearth.org/~victor/resume.html