Perl has a convenient built-in function for finding the current date and time in a script. However, when we talk about finding the time, we are talking about the time currently set on the machine running the script. For example, if you are running a Perl script on your local machine, localtime will return the current time you set, and possibly set to your current time zone.
When running the same script on the web server, you may find that the local time on your desktop system is off local time. The server may be in a different time zone, or may have incorrect settings. Each machine may have a completely different idea of the local time, and it may require some tweaking within the script or on the server itself to make it match what you expect.
The localtime function returns a list containing the current time data, some of which needs to be adjusted. Run the program below and you will see each element in the list printed on a line and separated by spaces.
#!/usr/local/bin/perl @timeData = localtime(time); print join(' ', @timeData);
You should see something similar, although the numbers may be significantly different.
20 36 8 27 11 105 2 360 0
These elements of the current time are in order:
-One minute passed
-A few minutes passed
-Midnight hour
- day of month
- number of months since the beginning of the week
- number of years since 1900
- number of days since the beginning of the week (Sunday)
- Number of days since the beginning of the year
- Whether daylight saving time is in effect
Make Perl local time readable
in the array returned by localtime Some elements of it read a little awkwardly. Who would think of this year in terms of years after 1900? Let's look at an example to make our dates and times clearer.
When you run this program, you will see a more readable date and time, like this:
9:14:42, Wed Dec 28, 2018
So what did we do to create this more readable What about the version? First, we prepare two arrays containing the names of the months and days of the week.
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
Since the localtime function returns these elements with values between 0-11 and 0-6, they are the best candidates for the array. The value returned by localtime can be used as a numeric address to access the correct element in the array.
$months[$month] $weekDays[$dayOfWeek]
The next step is to get all the values from the localtime function. In this example, we use a Perl shortcut to automatically put each element in the localtime array into its own variable. We choose names to make it easier to remember which element is which.
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
We also need to adjust the value for the year. Remember, localtime returns the number of years since 1900, so to find the current year we need to add 1900 to the given value.
$year = 1900 + $yearOffset;
How to express current universal time in Perl
Suppose you want to avoid all possible time zone confusion and control your own time difference. Getting the current time in localtime will always return a value based on the machine's time zone setting - a server in the US will return a time, while a server in Australia will return a time that is almost entirely different throughout the day due to time zone differences.
Perl also has a second convenient time indication function that works exactly like localtime, but instead of returning a fixed time in the machine's time zone, it returns Coordinated Universal Time (UTC, also known as Greenwich Mean Time or GMT). Quite simply, the function is called gmtime
#!/usr/local/bin/perl @timeData = gmtime(time); print join(' ', @timeData);
There is no difference between the gmtime and localtime functions except that the time returned is the same on every machine as in GMT. All data and transformations are done the same way.
#!/usr/local/bin/perl @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun); ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = gmtime(); $year = 1900 + $yearOffset; $theGMTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year"; print $theGMTime;
1.localtime will return the current local time on the machine running the script.
2.gmtime will return Universal Greenwich Mean Time, or GMT (or UTC).
3. The return value may not be what you expect, so make sure to convert if necessary.
Related recommendations: "Perl Tutorial"//m.sbmmt.com/course/list/39.html
This article This article is about how to use Perl to represent the current time. I hope it will be helpful to friends in need!
The above is the detailed content of How to represent the current time in Perl. For more information, please follow other related articles on the PHP Chinese website!