?? 05 - counting the days since an arbitrary date.rb
字號:
def last_modified(file) t1 = File.stat(file).ctime t2 = Time.now elapsed = (t2-t1)/(60*60*24) puts "#{file} was last modified #{elapsed} days ago."endlast_modified("/etc/passwd")# /etc/passwd was last modified 125.873605469919 days ago.last_modified("/home/leonardr/")# /home/leonardr/ was last modified 0.113293513796296 days ago.#---require 'date'def advent_calendar(date=DateTime.now) christmas = DateTime.new(date.year, 12, 25) christmas = DateTime.new(date.year+1, 12, 25) if date > christmas difference = (christmas-date).to_i if difference == 0 puts "Today is Christmas." else puts "Only #{difference} day#{"s" unless difference==1} until Christmas." endendadvent_calendar(DateTime.new(2006, 12, 24))# Only 1 day until Christmas.advent_calendar(DateTime.new(2006, 12, 25))# Today is Christmas.advent_calendar(DateTime.new(2006, 12, 26))# Only 364 days until Christmas.#---sent = DateTime.new(2006, 10, 4, 3, 15)received = DateTime.new(2006, 10, 5, 16, 33)elapsed = (received-sent) * 24puts "You responded to my email #{elapsed.to_f} hours after I sent it."# You responded to my email 37.3 hours after I sent it.#---require 'date'def remaining(date, event) intervals = [["day", 1], ["hour", 24], ["minute", 60], ["second", 60]] elapsed = DateTime.now - date tense = elapsed > 0 ? "since" : "until" interval = 1.0 parts = intervals.collect do |name, new_interval| interval /= new_interval number, elapsed = elapsed.abs.divmod(interval) "#{number.to_i} #{name}#{"s" unless number == 1}" end puts "#{parts.join(", ")} #{tense} #{event}."endremaining(DateTime.new(2006, 4, 15, 0, 0, 0, DateTime.now.offset), "the book deadline")# 27 days, 4 hours, 16 minutes, 9 seconds until the book deadline.remaining(DateTime.new(1999, 4, 23, 8, 0, 0, DateTime.now.offset), "the Math 114A final")# 2521 days, 11 hours, 43 minutes, 50 seconds since the Math 114A final.#---
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -