Getting Started
Introduction
Perl IDEs
History
Advice
Tools
Mini-Tutorial
Tutorial
Code Snippets

Resources
Top Sites
More Tutorials
Books
Magazines
Articles
NewsLetters
Mailing Lists
NewsGroups
Forums
User Groups
Talk Shows
Blogs
Clothing

GBIC >> Perl >> Information Center Tutorials >> Date/Time

Perl Information Center Tutorials - Date/Time
These tutorials were written to help you get a quick, but thorough, understanding of Perl - the scope of the language as well as it's specific capabilities.

Beginners Built-In Functions     Advanced CGI Applications

Date/Time Functions Summary
Perl has relatively few date/time related functions. They can be categorized as follows:

    • Current Time
     gmtime, localtime
    • Elapsed Time   
     time, times

Date/Time Function Reference
Here's a quick reference of the available date/time functions, in alphabetical order.

  • gmtime - returns ($sec, $min, $hour, $mon, $year $wday, $yday, $isdate) for Greenwich time zone
        gmtime (expression)  # like localtime, but Greenwich time zone 
        
  • localtime - returns ($sec, $min, $hour, $mon, $year $wday, $yday, $isdate)
        localtime(expression)
       ($s, $m, $h, $mon, $yr $wdy, $ydy, $isdate) = localtime(time);
       $year += 1900;                         # gets true year
       $year = sprintf("%02d", $year % 100);  # gets last 2 digits of year
       $now = localtime;     # scalar context gets "Thu Oct 13 04:54:34 1994"
        
    Note: all outputs are numeric and zero-based. $year is number of years since 1900.

  • time - returns number of seconds since Jan 1, 1970
        $result = time       # result can be used by gmtime and localtime
        
  • times - returns process/children elapsed time
        ($user, $system, $cuser, $csystem) = times
        $user = times        # scalar context returns only $user
        

High Resolution Timing
For improved resolution timing needs, use the Time::HiRes Module.

If you have any suggestions for additions to these tutorials, please let me know.