Ruby 简明教程 Part 7

1.简介

2.安装

3.基本语法

4高级进阶

 

------继续

3.基本语法

3.14 Date & Time

 

 Time类表示日期和时间。它是操做系统提供的系统日期和时间之上的。  这个类有可能在你的系统上不能表示1970以前或2038以后的日期。html

 

3.14.1Getting Current Date and Time 获取当前日期和时间

 

#!/usr/bin/ruby -w

time1 = Time.new
puts "Current Time : " + time1.inspect

# Time.now is a synonym:
time2 = Time.now
puts "Current Time : " + time2.inspect

结果:数组

zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_time.rb
Current Time : 2019-11-27 02:44:56 +0800
Current Time : 2019-11-27 02:44:56 +0800

3.14.2 Getting Components of a Date & Time 获取组件

 demo_timeComponents.rbruby

time = Time.new

# Components of a Time
puts "Current Time : " + time.inspect
puts "year=" + time.year.to_s    # => Year of the date 
puts "month=" + time.month.to_s   # => Month of the date (1 to 12)
puts "day=" + time.day.to_s     # => Day of the date (1 to 31 )
puts "wday=" + time.wday.to_s    # => 0: Day of week: 0 is Sunday
puts "yday=" + time.yday.to_s    # => 365: Day of year
puts "hour=" + time.hour.to_s    # => 23: 24-hour clock
puts "min=" + time.min.to_s     # => 59
puts "sec=" + time.sec.to_s     # => 59
puts "ms=" + time.usec.to_s    # => 999999: microseconds
puts "timezone=" + time.zone    # => "UTC": timezone name

结果:函数

zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_timeComponent.rb
Current Time : 2019-11-27 03:00:52 +0800
year=2019
month=11
day=27
wday=3
yday=331
hour=3
min=0
sec=52
ms=804903
timezone=CST

3.14.3 Time.utc, Time.gm and Time.local Functions

 utc,gm ,local 函数能够来格式化时间。以下:操作系统

# July 8, 2008
Time.local(2008, 7, 8)  
# July 8, 2008, 09:10am, local time
Time.local(2008, 7, 8, 9, 10)   
# July 8, 2008, 09:10 UTC
Time.utc(2008, 7, 8, 9, 10)  
# July 8, 2008, 09:10:11 GMT (same as UTC)
Time.gm(2008, 7, 8, 9, 10, 11)

能够获取如下格式的日期时间信息:code

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

 

time = Time.new
values = time.to_a
p values

结果:orm

[40, 8, 3, 27, 11, 2019, 3, 331, false, "CST"]
 htm

 

这个数组能够传给 Time.utc 或Time.local 来格式化:对象

 

time = Time.new
values = time.to_a
puts Time.utc(*values)

结果:索引

2019-11-27 03:10:41 UTC

如下是从epoch开始的秒数(取决于平台):
# Returns number of seconds since epoch
time = Time.now.to_i  

# Convert number of seconds into Time object.
Time.at(time)

# Returns second since epoch which includes microseconds
time = Time.now.to_f

3.14.4 Timezones and Daylight Savings Time

你能够用Time 对象获取与Timezones 和 Daylight savings 的全部信息。

time = Time.new

# Here is the interpretation
time.zone       # => "UTC": return the timezone
time.utc_offset # => 0: UTC is 0 seconds offset from UTC
time.zone       # => "PST" (or whatever your timezone is)
time.isdst      # => false: If UTC does not have DST.
time.utc?       # => true: if t is in UTC time zone
time.localtime  # Convert to local timezone.
time.gmtime     # Convert back to UTC.
time.getlocal   # Return a new Time object in local zone
time.getutc     # Return a new Time object in UTC

3.14.5 Formatting Times and Dates

有不一样方法能够格式化日期和时间。举例

time = Time.new
puts time.to_s
puts time.ctime
puts time.localtime
puts time.strftime("%Y-%m-%d %H:%M:%S")

结果

zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_timeformat.rb
2019-11-27 03:17:18 +0800
Wed Nov 27 03:17:18 2019
2019-11-27 03:17:18 +0800
2019-11-27 03:17:18

Time.strftime 指令

Sr.No. Directive & Description
1

%a

The abbreviated weekday name (Sun). 英文星期缩写

2

%A

The full weekday name (Sunday). 英文星期全名

3

%b

The abbreviated month name (Jan).  英文月份缩写

4

%B

The full month name (January).   英文月全名

5

%c

The preferred local date and time representation. 当地日期和时间表示

6

%d

Day of the month (01 to 31).  每个月的天,即几号。

7

%H

Hour of the day, 24-hour clock (00 to 23). 小时,24小时制(00-23)

8

%I

Hour of the day, 12-hour clock (01 to 12). 小时, 12小时制(01-12)

9

%j

Day of the year (001 to 366).   天(001-366)

10

%m

Month of the year (01 to 12).  月(01-12)

11

%M

Minute of the hour (00 to 59). 分(00-59)

12

%p

Meridian indicator (AM or PM).  上下午

13

%S

Second of the minute (00 to 60).  秒(00-60)

14

%U

Week number of the current year, starting with the first Sunday as the first day of the first week (00 to 53).

当年的第几周,以第一个星期天为第一周的第一天(00-53)

15

%W

Week number of the current year, starting with the first Monday as the first day of the first week (00 to 53).

当年周数,以第一个星期一做为第一周的第一天 (00-53)

16

%w

Day of the week (Sunday is 0, 0 to 6). 周几。 (0-6)

17

%x

Preferred representation for the date alone, no time. 日期,没有时间

18

%X

Preferred representation for the time alone, no date. 时间,没有日期。

19

%y

Year without a century (00 to 99). 最后俩位年

20

%Y

Year with century.   四位年

21

%Z

Time zone name.  时间带名字

22

%%

Literal % character.   %

3.14.6 Time Arithmetic 时间算术

 时间能够+,-。举例:

now = Time.now          # Current time
puts now

past = now - 10         # 10 seconds ago. Time - number => Time
puts past

future = now + 10  # 10 seconds from now Time + number => Time
puts future

diff = future - past     # => 10  Time - Time => number of seconds
puts diff

结果

zzl@zzl-VirtualBox:~/rubyprojects$ ruby  demo_timeArithmetic.rb
2019-11-27 03:21:18 +0800
2019-11-27 03:21:08 +0800
2019-11-27 03:21:28 +0800
20.0

3.15 Iterator 迭代器

迭代器依次返回集合中全部成员。 介绍一下each 和 collect.

3.15.1 Ruby each Iterator

each {|item| block} → ary

each → Enumerator

将每一个元素都传给block执行,返回本身。

若是没有block ,则返回枚举器。

a = [ "a", "b", "c" ]
b=a.each {|x| print x, " -- " }
puts "\n"
p  b

结果:
ruby demo_each.rb
a -- b -- c -- 
["a", "b", "c"]

each_index {|index| block} → ary

each_index → Enumerator

和each 相似,不过它传的是元素的索引,而不是元素。 

若是没有block ,则返回枚举器。

3.15.2 Ruby collect Iterator

collect {|item| block} → new_ary

collect → Enumerator

将每个元素都执行block,返回一个数组包含block返回的值。

若是没有block, 返回一个枚举器。

a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!"}           #=> ["a!", "b!", "c!", "d!"]
a.map.with_index {|x, i| x * i}   #=> ["", "b", "cc", "ddd"]
a                                 #=> ["a", "b", "c", "d"]

collect! {|item| block } → ary

collect! → Enumerator

将每个元素都执行block,替换原来的元素。

若是没有block, 返回一个枚举器。

a = [ "a", "b", "c", "d" ]
a.map! {|x| x + "!" }
a #=>  [ "a!", "b!", "c!", "d!" ]
a.collect!.with_index {|x, i| x[0...i] }
a #=>  ["", "b", "c!", "d!"]