6.24. Ruby date & time

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

Page Views: 9 views

The Time class is used in Ruby to represent the date and time. It is based on the system date and time provided by the operating system. This class maynot represent dates before 1970 or after 2038.

This tutorial will familiarize you with all the important concepts of date and time.

6.24.1. Create the current date and time #

Here is a simple example of getting the current date and time:

Example #

#/ Usr/bin/ruby - w # - * - coding: UTF-8- * - time1=Time. newputs : "+time1. inspect # Time. now has the same function as time2=Time. nowputs" Current time +time2. inspect 

The output of the above instance is as follows:

current time : 2015-09-17 15:23:14 +0800 current time: 2015-09-17 15:23:14 +0800 

6.24.2. Get Date & Time components #

We can use Time object to get components for various dates and times. Take a look at the following example:

Example #

#/ Usr/bin/ruby w # - * - coding: UTF-8- * - time=Time. new # Time The component puts "current time:"+time. inspectputtime. year #=> Year of date puttime. month #=>Month of date (1 to 12) puttime. day #=> The day of the month (1 to 31) puttime. wday #=>The day of the week (0 It's Sunday) puttime. yday #=>365: The day of the year puttime. hour #=>23:24 Hourly Puttime. min #=>59puttime. sec #=>59puttime. usec #=> 999999: microsecond puttime. zone #=>"UTC": time zone name 

The output of the above instance is as follows:

current time : 2015-09-17 15:24:44 +0800 2015 9 17 4 260 15 24 44 921519 CST 

6.24.3. Time.utc Time.gm and Time.local function #

These functions can be used to format dates in standard format, as follows:

#July 82008Time. local (2008,7,8) # July 82008, 09:10am, local time Time. local (2008,7,8,9,10) # July 82008, 09:10 UTCTime. utc (2008,7,8,9,10) # July 82008, 09:10:11 GMT (vs. UTC Same) Time.gm (2008,7,8,9,10,11) 

The following example gets all the components in the array:

[sec,min,hour,day,month,year,wday,yday,isdst,zone] 

Try the following example:

Example #

#!/usr/bin/ruby -wtime=Time.newvalues=time.to_apvalues 

The output of the above instance is as follows:

[39, 25, 15, 17, 9, 2015, 4, 260, false, "CST"] 

The array can be passed to the Time.utc or Time.local function to get the different formats of the date, as follows

Example #

#!/usr/bin/ruby -wtime=Time.newvalues=time.to_aputsTime.utc(*values) 

The output of the above instance is as follows:

2015-09-17 15:26:09 UTC 

Here is how to get the time, the number of seconds since the Epoch (platformdependent):

#Returns the number of seconds since the era time=Time. now. to_ I # Convert seconds to Time Object Time. at (time) # Returns the number of seconds since the era, including subtle time=Time. now. to_ F 

6.24.4. Time zone and daylight saving time #

You can use the Time object to get all the information about the time zone and daylight saving time, as follows:

Time=Time. new # Here is the explanation for time. zone #=>"UTC": returns the time zone time. utc_ Offset #=> 0: UTC is a 0 second offset from UTC time. zone #=> PST (or other time zone) time. isdst #=>false: If UTC does not have DST (Daylight Saving Time) time. utc? #=> True: If in UTC Time zone time. localtime # Convert to local time zone time. gmtime # Convert back UTC time. getlocal # Returns a new Time object in the local area time. getutc # Returns UTC A new Time object in 

6.24.5. Format time and date #

There are several ways to format dates and times. The following example demonstrates some of them:

Example #

#!/usr/bin/ruby -wtime=Time.newputstime.to_sputstime.ctimeputstime.localtimeputstime.strftime("%Y-%m-%d %H:%M:%S") 

The output of the above instance is as follows:

2015-09-17 15:26:42 +0800 Thu Sep 17 15:26:42 2015 2015-09-17 15:26:42 +0800 2015-09-17 15:26:42 

6.24.6. Time formatting instruction #

The instructions and methods listed in the following table Time.strftime use it together.

Instruction

Description

% a

An acronym for the day of the week (such as Sun).

% A

The full name of the day of the week (such as Sunday).

% b

An abbreviation for the name of the month (for example, Jan).

% B

The full name of the month (for example, January).

% c

Preferred local date and time representation.

% d

The day ordinal of a month (01 to 31).

% H

The hour ordinal of the day, 24-hour system (00 to 23).

% I

The hour ordinal of the day, the 12-hour system (01 to 12).

% j

The day of the year (001 to 366).

% m

The month of the year (01 to 12).

% M

The minute (00 to 59) of the hour.

% p

Meridian indication (AM or PM).

% S

The second (00 or 60) ordinal of a minute.

% U

The number of weeks in the current year, starting on the first Sunday (as the first day of the first week) (00 to 53).

% W

The number of weeks in the current year, starting on the first Monday (as the first day of the first week) (00 to 53).

% w

The day of the week (Sunday is 0 to 6).

% x

There is only a priority representation of the date and no time.

% X

There is only a priority representation of time and no date.

% y

A year without a century is represented by (00 to 99).

% Y

A year with a century.

% Z

Time zone name.

%%

% characters.

6.24.7. Time algorithm #

You can use your time to do some simple arithmetic, as follows:

Now=Time. now # The current time putsnowpast=now-10 # 10 seconds ago. Time - number=> Timeputpassfrequency=now+10 # 10 seconds from now. Time+number=> Timeputsfurturediff=future now #=>10 Time - Time=>Number of seconds putsdiff 

The output of the above instance is as follows:

2015-09-17 15:27:08 +0800 2015-09-17 15:26:58 +0800 2015-09-17 15:27:18 +0800 10.0 
《地理信息系统原理、技术与方法》  97

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