| ||||
| ||||
| Question: Shell Corner: Unix Time and Perl Script |
Shell Corner: Unix Time and Perl ScriptHosted by Ed SchaeferIn the April 2001 Shell Corner, Andrew Barber submitted TCL script amtime.tcl, which switches the number of seconds since the Epoch to a Gregorian date string, and back again. Andy now presents the same utility in Perl. amtime.pl is a Perl script to convert Unix time-in-seconds (UTIS) to a date/time format and back. Unix Time and Perl Since Sun Solaris doesn't possess a UTIS utility, I created amtime.tcl to replicate a program with a similar name distributed with the SGI Irix operating system in the availability monitoring package, amtime1970 (a UTIS application). I use several applications where storing the time in UTIS is more effecient than storing a date/time string and having to parse it later. amtime.pl is the conversion of this script to Perl, as well as the addition of some necessary features. Here is the usage:
Usage: amtime.pl {-t "
amtime.tcl uses the TCL clock function to generate the Unix time-in-seconds to a date/time value (clock format From the Perl POSIX manual page (Perl version 5.8.0):
strftime
Convert date and time information to string. Returns the string.
Synopsis:
strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
From the Perl POSIX manual page (Perl version 5.8.0):
mktime
Convert date/time info to a calendar time.
Synopsis:
mktime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = 0)
The arguments to both functions looks similar. Beginning with Perl 5.6, strftime even supports the "%s" format, output time in seconds, thereby allowing the script to only use one function from the POSIX library. Why have two functions if the one can suffice? The Why create this as a Perl script instead of a C program? C code may be portable, but the ability to compile C on your platform of choice is also a factor in the portability of code. If you have only a production system, Perl is probably installed, but a C compiler is probably not. Therefore, you may not be able to compile a C program. On the other hand, this Perl script requires only the base Perl libraries and modules — no additional compilation is necessary, and it should run on POSIX-compliant operating systems with any Perl version >= 5.005 (which was the oldest I had available to test).
refer: http://www.unixreview.com/documents/s=1344/ur0307g/ by: EasyZone วันที่ 11/9/2553 - 03:53:23 |