Class: Time

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_joda_time(gdatetime) ⇒ Object Also known as: from_gdata, from_date_time



14
15
16
17
18
19
20
21
22
23
# File 'lib/time_ext.rb', line 14

def from_joda_time(gdatetime)
  if defined? Time.zone
    Time.zone.parse(gdatetime.to_s)
  elsif defined? parse
    self.parse(gdatetime.to_s)
  else
    res = ParseDate.parsedate(gdatetime.to_s)
    Time.local(*res)
  end
end

Instance Method Details

#formatted_offset(colon = true, alternate_utc_string = nil) ⇒ Object



39
40
41
# File 'lib/time_ext.rb', line 39

def formatted_offset(colon = true, alternate_utc_string = nil)
  utc? && alternate_utc_string || to_utc_offset_s(utc_offset, colon)
end

#iso8601(fraction_digits = 0) ⇒ Object

The following are copied from Rails. If you are using Rails, everything is already defined in ActiveSupport



32
33
34
35
36
37
# File 'lib/time_ext.rb', line 32

def iso8601(fraction_digits = 0)
   fraction = if fraction_digits > 0
     ".%i" % self.usec.to_s[0, fraction_digits]
   end
   "#{self.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction}#{formatted_offset(true, 'Z')}"
end

#to_joda_timeObject Also known as: to_gdata, to_date_time



6
7
8
# File 'lib/time_ext.rb', line 6

def to_joda_time
  GDateTime.parseDateTime(self.iso8601)
end

#to_utc_offset_s(utc_offset, colon = true) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/time_ext.rb', line 43

def to_utc_offset_s(utc_offset, colon=true)
  seconds = utc_offset
  sign = (seconds < 0 ? -1 : 1)
  hours = seconds.abs / 3600
  minutes = (seconds.abs % 3600) / 60
  "%+03d%s%02d" % [ hours * sign, colon ? ":" : "", minutes ]
end