1. Perl
  2. builtin functions
  3. here

localtime function - get current date/time

You can use the localtime function to get the current date and time. You can get the local time according to each region.

# Seconds Minutes Hours Day Months Years of the week Beginning of the year or daylight saving time
# Apply et al.
# Elapsed days
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;

# Time string
my $localtime = localtime;

List context, date/time element information, Scalar context When used in, you can get the string that represents the date and time.

If you want to get Coordinated Universal Time, use gmtime function.

If you only need to know the elapsed time since the epoch, use time function.

Time::Piece is also recommended if you want to handle the date and time more conveniently.

For general information about dates and times, please refer to the following.

Local Time and Coordinated Universal Time

If you set the time like "The time when the sun is highest is noon (12:00)", the times will not match in each country. For example, when Japan is at noon (12:00), the other side of the globe is at midnight.

Such time is called local time. The OS clock is set to display the local time.

In addition, there is a time that coincides with the whole world called Coordinated Universal Time. This time is the same from anywhere in the world.

In the case of Japan, the local time is 9 hours ahead of Coordinated Universal Time.

Example localtime function

This is an example using the localtime function.

use strict;
use warnings;

print "(1) Get the current date and time(local time).\n";

# Seconds Minutes Hours Day Months Years of the week Beginning of the year or daylight saving time
# Apply et al.
# Elapsed days
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;

# The localtime function returns the year counted from 1900.
$year += 1900;

# Month starts from 0, so add 1 to display it.
$mon ++;

# Sunday will be 0.
my @day_of_week = qw/Sun Mon Tue Wed Thu Fri Sat/;

print "Currently $year year $mon month $mday day $hour hour $min minute $sec second.";
print "Day of the week". $Day_of_week[$wday]. "Day of the week.\n\n";
print "$yday days have passed counting in one year.\n\n";

if ($isdst) {
  print "Currently daylight saving time is applied.\n\n";
}
else {
  print "Currently daylight saving time does not apply.\n\n";
}

print "(2) Get the current date (local time) as a string.\n";
my $date_str = localtime;
print $date_str . "\n";

Get the current date and time(local time)

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

Use localtime function to get the current date and time. The localtime function is the reverse of the order when the Japanese wanted to get the time. The order is "seconds, minutes, hours, days, months, and years" from the beginning.

The year is an elapsed year from 1900, so you need to add 1900 to the $year you got.

$year += 1900;

The month starts at 0. For example, January is 0 and February is 1, so you need to add 1.

$mon ++;

The day of the week starts at 0 and 0 is Sunday. Monday is 1, Tuesday is 2, and so on. To display the day of the week, create an array that associates the subscripts of the array with the day of the week.

my @day_of_week = qw/Sun Mon Tue Wed Thu Fri Sat/;

$yday is the number of days since the beginning of the year. $isdst is whether or not daylight saving time is applied.

Get the current date (local time) as a string

my $date_str = localtime;

To get a string representation of the time, evaluate the return value of the localtime function in a scalar context.

Default argument of localtime function

If no argument is given to the localtime function, the value of time function (the number of seconds elapsed since the epoch) will be used. You can also explicitly give an argument for the number of seconds since the epoch.

Related Informatrion