Ruby/Date/DateTime
Материал из Wiki.crossplatform.ru
Add week to Date and Time
require "date" module Week def week (yday + 7 - wday) / 7 end end class Date include Week end class Time include Week end saturday = DateTime.new(2005, 1, 1) saturday.week # => 0 (saturday+1).week # => 1 #Sunday, January 2 (saturday-1).week # => 52 #Friday, December 31
Compare Time and DateTime
require "date" now_time = Time.new now_datetime = DateTime.now puts now_time.year puts now_datetime.year puts now_time.hour puts now_datetime.hour puts now_time.zone puts now_datetime.zone puts now_time.isdst puts now_time.usec # => 247930 # That is, 247930 microseconds puts now_datetime.sec_fraction # => Rational(62191, 21600000000) # That is, about 287921 microseconds
Converting Between Time and DateTime Objects
require "date" class Time def to_datetime seconds = sec + Rational(usec, 10**6) offset = Rational(utc_offset, 60 * 60 * 24) DateTime.new(year, month, day, hour, min, seconds, offset) end end puts time = Time.gm(2000, 6, 4, 10, 30, 22, 4010) puts time.to_datetime.to_s
Convert local (Eastern) time to Pacific time
require "date" eastern = DateTime.now eastern.to_s pacific_offset = Rational(-7, 24) pacific = eastern.new_offset(pacific_offset) pacific.to_s
Create DateTime object with new method
require "date" puts DateTime::new(1865, 4, 9).to_s puts DateTime::new(2100, 1, 1).to_s
DateTime.civil
require "date" puts DateTime.civil(1999, 12, 31, 23, 21, Rational(51044, 100000)).to_s puts DateTime.civil(1991, 10, 1).to_s puts DateTime.civil(2000).to_s
DateTime offset
require "date" now = DateTime.now now.to_s now.new_offset.to_s # The original object was not affected by the time zone conversion. puts now.to_s
DateTime.parse
require "date" puts DateTime.parse("02-09-2007 12:30:44 AM").to_s puts DateTime.parse("02-09-2007 12:30:44 PM EST").to_s
DateTime.strptime with format "%m-%d-%Y %H:%M:%S %Z"
date_and_time = "%m-%d-%Y %H:%M:%S %Z" require "date" puts DateTime.strptime("02-09-2007 12:30:44 EST", date_and_time).to_s puts DateTime.strptime("02-09-2007 12:30:44 PST", date_and_time).to_s puts DateTime.strptime("02-09-2007 12:30:44 GMT", date_and_time).to_s
DateTime.strptime with format "%m-%d-%Y %I:%M:%S %p"
require "date" twelve_hour_clock_time = "%m-%d-%Y %I:%M:%S %p" puts DateTime.strptime("02-09-2007 12:30:44 AM", twelve_hour_clock_time).to_s puts DateTime.strptime("02-09-2007 12:30:44 PM", twelve_hour_clock_time).to_s
Local offset
require "date" local = DateTime.now utc = local.new_offset puts local.offset # => Rational(-5, 24) puts local_from_utc = utc.new_offset(local.offset) puts local_from_utc.to_s # => "2006-03-18T20:15:58-0500" puts local == local_from_utc # => true
Offset a date
require "date" local = DateTime.now local.to_s # => "2006-03-18T20:15:58-0500" utc = local.new_offset(0) utc.to_s # => "2006-03-19T01:15:58Z"
require "date"
my_offset = DateTime.now.offset # => Rational(-5, 24) puts DateTime.civil(1999, 12, 31, 23, 21, Rational(51044, 100000), my_offset).to_s
The current date and time:
require "date" puts DateTime::now.to_s
Time zero for the date library:
require "date" puts DateTime::new.to_s
Try different formats
require "date" TRY_FORMATS = ["%d/%m/%y", "%Y %m %d"] def try_to_parse(s) parsed = nil TRY_FORMATS.each do |format| begin parsed = Date.strptime(s, format) break rescue ArgumentError end end return parsed end puts try_to_parse("1/10/07").to_s # => "2007-10-01" puts try_to_parse("2007 1 10").to_s # => "2007-01-10"