Class: WTF::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/wtf.rb

Class Method Summary collapse

Instance Method Summary collapse

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

convert is a convenience method to make it easier to convert between WTF times and standard times.

Examples:

WTF::Date.convert("FIWIT:NAAAA")  #=> same as WTF::Date.new("FIWIT:NAAAA").as_utc
WTF::Date.convert(time)           #=> same as WTF::Date.new(time).as_wtf

Raises:

  • (ArgumentError)


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

.nowObject

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_utcObject

returns a Time object representing the WTF::Date’s date and time in UTC.

Examples:

standard_time = WTF::Date.new("FIWIT:NAAAA").as_utc
standard_time = WTF::Date.new(":BJR").as_utc
standard_time = WTF::Date.new("XQ:ARM").as_utc


93
94
95
# File 'lib/wtf.rb', line 93

def as_utc
  @time
end

#as_wtfObject

returns a string representing the date and time in World Time Format.

Example:

wtf_date = WTF::Date.new(Time.utc(2002, 7, 10, 13, 55, 1, 777000))
wtf_date.as_wtf  #=> "FJNXQ:CCAAA"


104
105
106
# File 'lib/wtf.rb', line 104

def as_wtf
  @wtf
end

#date_partObject

returns just the World Time Format date.

Example:

wtf_date = WTF::Date.new(Time.utc(2002, 7, 10, 13, 55, 1, 777000))
wtf_date.date_part  #=> "FJNXQ"


115
116
117
118
# File 'lib/wtf.rb', line 115

def date_part
  @wtf =~ /^([A-Z]{0,5}):([A-Z]*)$/
  $1
end

#time_partObject

returns just the World Time Format time.

Example:

wtf_date = WTF::Date.new(Time.utc(2002, 7, 10, 13, 55, 1, 777000))
wtf_date.time_part  #=> "CCAAA"


127
128
129
130
# File 'lib/wtf.rb', line 127

def time_part
  @wtf =~ /^([A-Z]{0,5}):([A-Z]*)$/
  $2
end