-
Notifications
You must be signed in to change notification settings - Fork 21.6k
/
conversions.rb
58 lines (52 loc) · 1.99 KB
/
conversions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'date'
require 'active_support/core_ext/time/calculations'
class String
# Converts a string to a Time value.
# The +form+ can be either :utc or :local (default :utc).
#
# The time is parsed using Date._parse method.
# If +form+ is :local, then time is formatted using Time.zone
#
# "3-2-2012".to_time # => 2012-02-03 00:00:00 UTC
# "12:20".to_time # => ArgumentError: invalid date
# "2012-12-13 06:12".to_time # => 2012-12-13 06:12:00 UTC
# "2012-12-13T06:12".to_time # => 2012-12-13 06:12:00 UTC
# "2012-12-13T06:12".to_time(:local) # => 2012-12-13 06:12:00 +0100
def to_time(form = :utc)
unless blank?
date_values = ::Date._parse(self, false).
values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset).
map! { |arg| arg || 0 }
date_values[6] *= 1000000
offset = date_values.pop
::Time.send("#{form}_time", *date_values) - offset
end
end
# Converts a string to a Date value.
#
# "1-1-2012".to_date #=> Sun, 01 Jan 2012
# "01/01/2012".to_date #=> Sun, 01 Jan 2012
# "2012-12-13".to_date #=> Thu, 13 Dec 2012
# "12/13/2012".to_date #=> ArgumentError: invalid date
def to_date
unless blank?
date_values = ::Date._parse(self, false).values_at(:year, :mon, :mday)
::Date.new(*date_values)
end
end
# Converts a string to a DateTime value.
#
# "1-1-2012".to_datetime #=> Sun, 01 Jan 2012 00:00:00 +0000
# "01/01/2012 23:59:59".to_datetime #=> Sun, 01 Jan 2012 23:59:59 +0000
# "2012-12-13 12:50".to_datetime #=> Thu, 13 Dec 2012 12:50:00 +0000
# "12/13/2012".to_datetime #=> ArgumentError: invalid date
def to_datetime
unless blank?
date_values = ::Date._parse(self, false).
values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :sec_fraction).
map! { |arg| arg || 0 }
date_values[5] += date_values.pop
::DateTime.civil(*date_values)
end
end
end