1.簡介
2.安裝
3.基本語法
4高級進階
------繼續
3.基本語法
3.14 Date & Time
Time類表示日期和時間。它是操作系統提供的系統日期和時間之上的。 這個類有可能在你的系統上不能表示1970之前或2038之後的日期。
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.rb
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)
可以獲取以下格式的日期時間信息:
[sec,min,hour,day,month,year,wday,yday,isdst,zone]
time = Time.new
values = time.to_a
p values
結果:
[40, 8, 3, 27, 11, 2019, 3, 331, false, "CST"]
這個數組可以傳給 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!"]