Home arrow Perl Programming arrow Page 4 - File Tests in Perl

The localtime Function - Perl

In this article, you will learn how to find out useful information about files in Perl. It is excerpted from chapter 11 of the book Learning Perl, Fourth Edition, written by Randal L. Schwartz, Tom Phoenix and brian d foy (O'Reilly; ISBN: 0596101058). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. File Tests in Perl
  2. File Test Operators
  3. The stat and lstat Functions
  4. The localtime Function
  5. Bitwise Operators
  6. Using the Special Underscore Filehandle
By: O'Reilly Media
Rating: starstarstarstarstar / 22
May 10, 2007

print this article



When you have a timestamp number (such as the ones from stat), it will typically look something like 1180630098. That won’t help you, unless you need to compare two timestamps by subtracting. You may need to convert it to something human-readable, such as a string like “Thu May 31 09:48:18 2007”. Perl can do that with the localtime function in a scalar context:

  my $timestamp = 1180630098;
my $date = localtime $timestamp;

In a list context,localtimereturns a list of numbers, several of which may not be what you’d expect:

  my($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst)
    = localtime $timestamp;

The$monis a month number, ranging from0to11, which is handy as an index into an array of month names. The$yearis the number of years since 1900, oddly enough, so add1900to get the real year number. The$wdayranges from0(for Sunday) through6(for Saturday), and the$ydayis the day-of-the-year (ranging from 0 for January 1, through 364 or 365 for December 31).

Two related functions are also useful. Thegmtimefunction is the same aslocaltime, except that it returns the time in Universal Time (what we once called Greenwich Mean Time). If you need the current timestamp number from the system clock, use thetimefunction. Bothlocaltimeandgmtime default to using the currenttimevalue if you don’t supply a parameter:

  my $now = gmtime; # Get the current universal timestamp as a string

For more information on manipulating date and time information, see the information about some useful modules in Appendix R.

>>> More Perl Programming Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: