1. Perl
  2. builtin functions
  3. gmtime

gmtime function - gets the current date and time(Coordinated Universal Time)

Use the gmtime function to get the current date and time in Coordinated Universal Time. It's called gmtime, but it's now a function that gets Coordinated Universal Time, not a function that gets Greenwich Mean Time (GMT).

# 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) = gmtime;

# Time string
my $localtime = gmtime;

If you want to get the local time, use localtime 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.

gmtime function example

This is an example of the gmtime function.

use strict;
use warnings;

print "(1) Get the current date and time(Coordinated Universal 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) = gmtime;

# 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 (Coordinated Universal Time) as a string.\n";
my $date_str = gmtime;
print $date_str . "\n";

Related Informatrion