Class: WTF::Date
- Inherits:
-
Object
- Object
- WTF::Date
- Defined in:
- lib/wtf.rb
Class Method Summary collapse
-
.convert(arg) ⇒ Object
convert is a convenience method to make it easier to convert between WTF times and standard times.
-
.now ⇒ Object
now is a synonym for WTF::Date.new.
Instance Method Summary collapse
-
#as_utc ⇒ Object
returns a Time object representing the WTF::Date’s date and time in UTC.
-
#as_wtf ⇒ Object
returns a string representing the date and time in World Time Format.
-
#date_part ⇒ Object
returns just the World Time Format date.
-
#initialize(time = nil) ⇒ Date
constructor
creates a new WTF::Date object.
-
#time_part ⇒ Object
returns just the World Time Format time.
Constructor Details
#initialize(time = nil) ⇒ Date
creates a new WTF::Date object. The argument can be a string representing the date and/or time in World Time Format, or it can be a Time object. If the argument is missing, Time.now is assumed.
Examples:
WTF::Date.new(":NB") #=> returns a new WTF::Date object for the WTF time :NB
WTF::Date.new(":RJA") #=> returns a new WTF::Date object for the WTF date :RJA
WTF::Date.new("MM:BRT") #=> returns a new WTF::Date object for the WTF datetime MM:BRT
WTF::Date.new #=> returns a new WTF::Date object corresponding to right now
WTF::Date.new(Time.utc(2010, 5, 6)) #=> returns a new WTF::Date object for the given Time
If the argument is in World Time Format, it must can contain a date part, a time part, and a colon to separate them. Here are some examples of valid formats:
-
“FJKRM:BROMQ” – This is a fully specified World Time Format date and time. The first part is the date part and the second part is the time part. The date part cannot be longer than five characters. The time part can be longer than five characters, but any characters after the fifth character are ignored when calculating.
-
“:BROMQ” – You can leave out the date part. Today is assumed.
-
“FJKRM:” – You can leave out the time part. The beginning of the Julian day is assumed (which is noon).
-
“BROMQ” – If you leave out the colon, it is assumed that you are giving the time, not the date.
-
“RM:BROMQ” – You can leave out some of the digits for the date. If you do, the remaining digits will be filled in according to today’s date. If today’s date is FMBAZ, then “RM:BROMQ” becomes “FMBRM:BROMQ”.
-
“:BR” – You don’t have to specify all five of the time digits.
-
“A:B” – This is a valid format.
Also note:
-
The date conversion does not work before the Gregorian calendar change that happened in October 1582.
-
Since the date part is limited to five characters, there is an upper bound for how far into the future you can do a date conversion.
-
The time part is limited to five characters and therefore the time precision is limited to about 10 milliseconds.
-
Leap seconds were not taken into account.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/wtf.rb', line 48 def initialize(time = nil) if time if time.is_a? String @time = convert_from_wtf(time) elsif time.is_a? Time @time = time else raise ArgumentError.new("Argument must be a String or a Time.") end else @time = ::Time.now end @wtf = convert_to_wtf(@time) end |
Class Method Details
.convert(arg) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/wtf.rb', line 78 def self.convert(arg) return nil if arg.nil? return self.new(arg).as_utc if arg.is_a?(String) return self.new(arg).as_wtf if arg.is_a?(Time) raise ArgumentError.new("Argument must be a String or a Time.") end |
.now ⇒ Object
now is a synonym for WTF::Date.new. It returns a WTF::Date object initialized with the current date and time.
66 67 68 |
# File 'lib/wtf.rb', line 66 def self.now self.new end |
Instance Method Details
#as_utc ⇒ Object
93 94 95 |
# File 'lib/wtf.rb', line 93 def as_utc @time end |
#as_wtf ⇒ Object
104 105 106 |
# File 'lib/wtf.rb', line 104 def as_wtf @wtf end |
#date_part ⇒ Object
115 116 117 118 |
# File 'lib/wtf.rb', line 115 def date_part @wtf =~ /^([A-Z]{0,5}):([A-Z]*)$/ $1 end |