- Perl Basics
- Perl - Home
- Perl - Introduction
- Perl - Environment
- Perl - Syntax Overview
- Perl - Data Types
- Perl - Variables
- Perl - Scalars
- Perl - Arrays
- Perl - Hashes
- Perl - IF...ELSE
- Perl - Loops
- Perl - Operators
- Perl - Date & Time
- Perl - Subroutines
- Perl - References
- Perl - Formats
- Perl - File I/O
- Perl - Directories
- Perl - Error Handling
- Perl - Special Variables
- Perl - Coding Standard
- Perl - Regular Expressions
- Perl - Sending Email
- Perl Advanced
- Perl - Socket Programming
- Perl - Object Oriented
- Perl - Database Access
- Perl - CGI Programming
- Perl - Packages & Modules
- Perl - Process Management
- Perl - Embedded Documentation
- Perl - Functions References
- Perl Useful Resources
- Perl - Questions and Answers
- Perl - Quick Guide
- Perl - Useful Resources
- Perl - Discussion
Perl localtime Function
Description
This function converts the time specified by EXPR in a list context, returning a nine-element array with the time analyzed for the current local time zone. The elements of the array are −
# 0 1 2 3 4 5 6 7 8 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
If EXPR is omitted, uses the value returned by time.
$mday is the day of the month, and $mon is the month itself, in the range 0..11 with 0 indicating January and 11 indicating December.
$year is the number of years since 1900, not just the last two digits of the year. That is, $year is 123 in year 2023. The proper way to get a complete 4-digit year is simply: $year += 1900;
Syntax
Following is the simple syntax for this function −
localtime EXPR
Return Value
This function returns a string of the form: Thu Sep 21 14:52:52 2000 in scalar context and the individual time component values (seconds, minutes, hours, day of month, month, year, day of week, day of year, daylight savings time) in list context.
Example
Following is the example code showing its basic usage −
#!/usr/bin/perl -w use POSIX; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; print "$sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst\n"; $now_string = localtime; print "$now_string\n"; $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; print "$now_string\n";
When above code is executed, it produces the following result −
19, 58, 14, 1, 8, 2013, 0, 243, 0 Sun Sep 1 14:58:19 2013 Sun Sep 1 14:58:19 2013