5.31. Perl time date

发布时间 : 2025-10-25 13:33:06 UTC      

Page Views: 18 views

In this chapter, we introduce to you Perl treatment of time and date by language.

There are several functions for processing time in Perl:

  • time() Function: returns the number of seconds accumulated since January 1, 1970

  • localtime() Function: get local time zone time

  • gmtime() Function: get Greenwich mean time

5.31.1. Current time and date #

Next, let’s take a look. localtime() function that returns the current time and date without arguments.

The following nine symbols represent different time and date parameters:

Sec, # seconds, 0 to 61 Min, # minutes, 0 to 59 Hour, # hours, 0 to 24 Mday, # days, 1 to 31 Mon, # month, 0 to 11 Year, # year, starting from 1900 Wday, # day of the week, 0-6,0 represents Sunday Yday, # day of the year, 0-364365 Isdst # True if daylight saving time is valid 

The example is shown as follows:

Example #

#/ Usr/bin/ perl@months =Qw (January, February, March, April, May, June, July, August, September) October, November, December)@ Days=qw (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday) Saturday; ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)=localtime(); Print "$mday $months [$mon] $days [$wday] n"; 

The output of the above example is as follows:

June Sunday, December 

If you call the localtime() which returns the time when the system setsthe time zone for the current period. An example is as follows:

Example #

#!/usr/bin/perl$datestring=localtime();print"Time date is:$datestring\\n"; 

The output of the above example is as follows:

Time date is:Sun Jun 12 11:27:31 2016 

5.31.2. Greenwich mean time (GMT) #

Function gmtime() vs. localtime() similar, but it returns standardGreenwich mean time.

Example #

#/ Usr/bin/perl $local_ Datestring=localtime(); Print "The local time and date are: $local_datestring n"$ Gmt_ Datestring=gmtime(); Print "GMT Time and date: $gmt_ Datestring n "; 

The output of the above example is as follows:

Local time and date: Sun Jun 12 11:32:14 2016 GMT time and date: Sun Jun 12 03:32:14 2016 

We can see from the example that there is a difference of 8 hours between Chinese time and Greenwich mean time.

5.31.3. Format date and time #

We can use it. localtime() function to output the formatting time that needs to be established. Formatted output using printf() function:

Example #

#!/usr/bin/perl($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)= localtime();printf("Format Time:HH:MM:SS\\n");printf("%02d:%02d:%02d",$hour,$min,$sec); 

The output of the above example is as follows:

Format Time:HH:MM:SS 11:35:23 

5.31.4. New era time (Epoch Time) #

We can use it. time() function to get the time of the new era, which returns the number of seconds accumulated since January 1, 1970. Examples are as follows:

Example #

#!/usr/bin/perl$epoc=time();print" The cumulative number of seconds since January 1, 1970 is:$epoc\\n"; 

The output of the above example is as follows:

The cumulative number of seconds since January 1, 1970 is:1465702883 

We can output a time format that we want:

Example #

#/ Usr/bin/perl ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)=localtime(); Print " Current time and date:"; Printf ("% d -% d -% d %d: % d:% d ", $year+1900, $mon+1, $mday, $hour, $min, $sec); print" n "; $epoc=time(); $epoc=$epoc-24 * 60 * 60# One day's time seconds ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)=localtime ($epoc); Print "Yesterday time and date:"; Printf ("% d -% d -% d %d: % d:% d ", $year+1900, $mon+1, $mday, $hour, $min, $sec); print" n "; 

The output of the above example is as follows:

Current time and date: March 15, 2017 12:47:54 Yesterday's time and date: March 14, 2017 12:47:54 

5.31.5. POSIX function strftime() #

Function strftime() we can format the time into the format we want.

The following table lists some formatted symbols * number indicates that you want to rely on local time:

Symbol

Description

Example

%a

Abbreviation of day of the week (Sun..Sat) *

Thu

%A

The full name of the week (Sunday..Saturday) *

Thursday

%b

Abbreviation for month (Jan..Dec) *

Aug

%B

Full name of the month (January..December) *

August

%c

Date and time *

Thu Aug 23 14:55:02 2001

%C

The year is divided by 100 and rounded (00-99)

20

%d

The day of a month (01-31)

23

%D

Date, MM/DD/YY? Equivalent to% m/%d/%y

08/23/01

%e

On the day of the month, fill in single digits with spaces (?1-31)

23

%F

YYYY-MM-DD? The abbreviation is similar to?% Y-%m-%d

37126

%g

The last two digits of the year (00-99)

1

%g

Year

2001

%h

Month abbreviation * (same as% b option)

Aug

%H

24-hour system (00-23)

14

%I

12-hour system (01-12)

2

%j

The day of the year (001-366)

235

%m

Month (01-12)

8

% M

Minutes (00-59)

55

%n

New line (’n’)

%p

Show AM or PM

PM

%r

Time (hh:mm:ss AM or PM), 12 hours *

0.621550926

%R

24 hours? HH:MM? Time format, which is equal to?% HRV% M

0.621527778

%S

Seconds (00-61)

2

%t

Horizontal tab (’t’)

%T

Time (24-hour system) (hh:mm:ss), equivalent to% H:%M:%S

0.621527778

%u

What day format is ISO 8601, Monday is 1? (1-7)

4

%U

The week ordinal of the year, Sunday is the first day (00-53)

33

%V

Week of ISO 8601 (00-53)

34

%w

The day of the week (0 represents Sunday) (0-6)

4

%W

The week ordinal of the year, Monday is the first day (00-53)

34

%x

Display date format (mm/dd/yy) *

08/23/01

%X

Display time format *

0.621550926

%y

Year, double digits (00-99)

1

%Y

Year

2001

%z

Time zone offset between ISO 8601 and UTC (1 minute=1, 1 hour=100)

100

%Z

Name of the current time zone, such as “China Standard time” *

CDT

%%

%? Symbol

%

Example #

#/ Usr/bin/perluse POSIXqw (strftime)$ Datestring=strftime "% Y -% m -% d %H: % M:% S ", localtime; printf (" time date - $datestring n "); # GMT Format time date $datestring=strftime "% Y -% m -% d %H: % M:% S ", gmtime; printf (" time date - $datestring n "); 

The output of the above example is as follows:

Time and date - June 12, 2016 12:15:13 Time and date - June 12, 2016 04:15:13 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。