Class: ZonelessTime::TimeWithoutZone

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

Constant Summary collapse

RFC2822_DAY_NAME =
Time::RFC2822_DAY_NAME
RFC2822_MONTH_NAME =
Time::RFC2822_MONTH_NAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, month, day, hour, min, sec, usec = 0) ⇒ TimeWithoutZone

Returns a new instance of TimeWithoutZone.



53
54
55
56
57
58
59
# File 'lib/zoneless_time/time_without_zone.rb', line 53

def initialize(year,month,day,hour,min,sec,usec=0)
  @date = Date.new(year,month,day)
  @hour = hour
  @min = min
  @sec = sec
  @usec = usec
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



3
4
5
# File 'lib/zoneless_time/time_without_zone.rb', line 3

def date
  @date
end

#hourObject

Returns the value of attribute hour.



3
4
5
# File 'lib/zoneless_time/time_without_zone.rb', line 3

def hour
  @hour
end

#minObject

Returns the value of attribute min.



3
4
5
# File 'lib/zoneless_time/time_without_zone.rb', line 3

def min
  @min
end

#secObject

Returns the value of attribute sec.



3
4
5
# File 'lib/zoneless_time/time_without_zone.rb', line 3

def sec
  @sec
end

#usecObject

Returns the value of attribute usec.



3
4
5
# File 'lib/zoneless_time/time_without_zone.rb', line 3

def usec
  @usec
end

Class Method Details

.at(time) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/zoneless_time/time_without_zone.rb', line 144

def at(time)
  if time.nil?
    nil
  elsif time.is_a? self
    time
  elsif time.acts_like? :time
    from_time time
  else
    from_time Time.at(time)
  end
end

Instance Method Details

#+(amount) ⇒ Object



32
33
34
# File 'lib/zoneless_time/time_without_zone.rb', line 32

def +(amount)
  (to_time + amount).without_zone
end

#-(amount) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/zoneless_time/time_without_zone.rb', line 36

def -(amount)
  result = if amount.is_a? Fixnum
    to_time - amount
  else
    to_time - amount.to_time
  end
  (result.is_a? Time) ? result.without_zone : result
end

#<(other) ⇒ Object



24
25
26
# File 'lib/zoneless_time/time_without_zone.rb', line 24

def <(other)
  to_i < other.to_time.to_i
end

#<=(other) ⇒ Object



28
29
30
# File 'lib/zoneless_time/time_without_zone.rb', line 28

def <=(other)
  to_i <= other.to_time.to_i
end

#<=>(other) ⇒ Object



12
13
14
# File 'lib/zoneless_time/time_without_zone.rb', line 12

def <=>(other)
  to_i <=> other.to_time.to_i
end

#==(other) ⇒ Object



126
127
128
# File 'lib/zoneless_time/time_without_zone.rb', line 126

def ==(other)
  matches? other
end

#>(other) ⇒ Object



16
17
18
# File 'lib/zoneless_time/time_without_zone.rb', line 16

def >(other)
  to_i > other.to_time.to_i
end

#>=(other) ⇒ Object



20
21
22
# File 'lib/zoneless_time/time_without_zone.rb', line 20

def >=(other)
  to_i >= other.to_time.to_i
end

#acts_like?(sym) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/zoneless_time/time_without_zone.rb', line 134

def acts_like?(sym)
  [:date, :time].include? sym
end

#dayObject



67
68
69
# File 'lib/zoneless_time/time_without_zone.rb', line 67

def day
  date.day
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/zoneless_time/time_without_zone.rb', line 130

def eql?(other)
  self == other
end

#hashObject



94
95
96
# File 'lib/zoneless_time/time_without_zone.rb', line 94

def hash
  self.class.hash ^ to_i
end

#in_time_zone(*args) ⇒ Object



102
103
104
# File 'lib/zoneless_time/time_without_zone.rb', line 102

def in_time_zone(*args)
  dup
end

#inspectObject



86
87
88
# File 'lib/zoneless_time/time_without_zone.rb', line 86

def inspect
  to_s
end

#localObject



106
107
108
# File 'lib/zoneless_time/time_without_zone.rb', line 106

def local
  dup
end

#matches?(other) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/zoneless_time/time_without_zone.rb', line 114

def matches?(other)
 [:year, :month, :day, :hour, :min, :sec].all? { |sym| other.send(sym) == self.send(sym) }
end

#monthObject



64
65
66
# File 'lib/zoneless_time/time_without_zone.rb', line 64

def month
  date.month
end

#seconds_since_midnightObject



74
75
76
# File 'lib/zoneless_time/time_without_zone.rb', line 74

def seconds_since_midnight
  ((60 * hour) + min) * 60 + sec
end

#since(amount) ⇒ Object



45
46
47
# File 'lib/zoneless_time/time_without_zone.rb', line 45

def since(amount)
  self + amount
end

#strftime(*args) ⇒ Object



110
111
112
# File 'lib/zoneless_time/time_without_zone.rb', line 110

def strftime(*args)
  to_time.strftime *args
end

#to_dateObject



122
123
124
# File 'lib/zoneless_time/time_without_zone.rb', line 122

def to_date
  Date.new(year, month, day)
end

#to_iObject



90
91
92
# File 'lib/zoneless_time/time_without_zone.rb', line 90

def to_i
  to_time.to_i
end

#to_s(type = nil) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/zoneless_time/time_without_zone.rb', line 78

def to_s(type=nil)
  if type == :db
    "#{year}-#{two_char month}-#{two_char day} #{two_char hour}:#{two_char min}:#{two_char sec}"
  else
    "#{RFC2822_DAY_NAME[wday]} #{RFC2822_MONTH_NAME[month-1]} #{day} #{two_char hour}:#{two_char min}:#{two_char sec} #{year}"
  end
end

#to_timeObject



118
119
120
# File 'lib/zoneless_time/time_without_zone.rb', line 118

def to_time
  Time.local(year, month, day, hour, min, sec, usec)
end

#until(amount) ⇒ Object



49
50
51
# File 'lib/zoneless_time/time_without_zone.rb', line 49

def until(amount)
  self - amount
end

#wdayObject



70
71
72
# File 'lib/zoneless_time/time_without_zone.rb', line 70

def wday
  date.wday
end

#without_zoneObject



98
99
100
# File 'lib/zoneless_time/time_without_zone.rb', line 98

def without_zone
  dup
end

#xmlschemaObject



8
9
10
# File 'lib/zoneless_time/time_without_zone.rb', line 8

def xmlschema
  to_time.xmlschema.gsub(/\+.*\Z/, '')
end

#yearObject



61
62
63
# File 'lib/zoneless_time/time_without_zone.rb', line 61

def year
  date.year
end