Shell Corner: Unix Time and Perl Script EasyZone - บริการ Software จัดการ อินเตอร์เน็ตในหอพัก, อพาร์ทเมนท์, แมนชั่น, คอนโด, โรงแรม
 


หน้าแรก | ลงทะเบียน | เข้าสู่ระบบ | สินค้าและบริการ | ลูกค้าของเรา | Dealer | Install | Support | ติดต่อ | DEMO
02-691-6382 (Office), 089-758-0691(ฝ่ายขาย), 086-644-2243 (ฝ่ายเทคนิค)

EasyZone Pro30 : บริการโปรแกรม Radius, Hotspot, PPPoE billing


  

 Question: Shell Corner: Unix Time and Perl Script

 

Shell Corner: Unix Time and Perl Script

Hosted by Ed Schaefer

In 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 "" | -d "" [+]}
     -t     Calculate Unix time in seconds from a date.
          Format is "wday mon day hh:mm:ss yyyy"
          ex: amtime.pl -t "Wed Jan 29 11:16:32 2003"
     -d     Formats a date-time string from a give unix time in seconds
          ex: amtime.pl -d 1043857131
     +     Format of the date-time string of option -d
          Default is %c.
          Optionally can be any supported format string of the 'date' command.
          ex: amtime.pl -d 1043857131 +%Y%m%d

amtime.tcl uses the TCL clock function to generate the Unix time-in-seconds to a date/time value (clock format ), as well as converts a date/time value into UTIS (clock scan ). Since Perl doesn't have such a function, writing the script in Perl involves using the POSIX module to give access to two standard time functions: strftime and mktime.

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)

fmt is the output format that you would like. Check the POSIX manual page or the strftime/ctime "C" function manual page for the usable output format specifications. The Unix "date" program uses the same format specifications.

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 %s argument is a GNU addition and may not be supported on all operating systems. Since both strftime and mktime are POSIX functions, the basic POSIX options for both functions operate correctly on all POSIX-compliant operating systems. Also, Perl version 5.6 does not support the %s argument to strftime. I have included the use of strftime as a comment before the mktime function in the script so you can compare the functions.

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  




Question and Comment
name:
Email:
message:
Security Code :
    * กรุณากรอก Security Code เพื่อป้องการกันการโจมตีเว็บบอร์ด
 

Back