Class: TimeCrisis::TimeWithZone

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/time_crisis/support/time_with_zone.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(utc_time, time_zone, local_time = nil, period = nil) ⇒ TimeWithZone

Returns a new instance of TimeWithZone.



14
15
16
17
# File 'lib/time_crisis/support/time_with_zone.rb', line 14

def initialize(utc_time, time_zone, local_time = nil, period = nil)
  @utc, @time_zone, @time = utc_time, time_zone, local_time
  @period = @utc ? period : get_period_and_ensure_valid_local_time
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



248
249
250
251
# File 'lib/time_crisis/support/time_with_zone.rb', line 248

def method_missing(sym, *args, &block)
  result = time.__send__(sym, *args, &block)
  result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result
end

Instance Attribute Details

#time_zoneObject (readonly)

Returns the value of attribute time_zone.



12
13
14
# File 'lib/time_crisis/support/time_with_zone.rb', line 12

def time_zone
  @time_zone
end

Class Method Details

.nameObject



7
8
9
# File 'lib/time_crisis/support/time_with_zone.rb', line 7

def self.name
  'Time'
end

Instance Method Details

#+(other) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/time_crisis/support/time_with_zone.rb', line 139

def +(other)
  if duration_of_variable_length?(other)
    method_missing(:+, other)
  else
    result = utc.acts_like?(:date) ? utc.since(other) : utc + other rescue utc.since(other)
    result.in_time_zone(time_zone)
  end
end

#-(other) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/time_crisis/support/time_with_zone.rb', line 148

def -(other)
  if other.acts_like?(:time)
    utc.to_f - other.to_f
  elsif duration_of_variable_length?(other)
    method_missing(:-, other)
  else
    result = utc.acts_like?(:date) ? utc.ago(other) : utc - other rescue utc.ago(other)
    result.in_time_zone(time_zone)
  end
end

#<=>(other) ⇒ Object



115
116
117
# File 'lib/time_crisis/support/time_with_zone.rb', line 115

def <=>(other)
  utc <=> other
end

#acts_like_time?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/time_crisis/support/time_with_zone.rb', line 221

def acts_like_time?
  true
end

#advance(options) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/time_crisis/support/time_with_zone.rb', line 171

def advance(options)
  if options.values_at(:years, :weeks, :months, :days).any?
    method_missing(:advance, options)
  else
    utc.advance(options).in_time_zone(time_zone)
  end
end

#ago(other) ⇒ Object



167
168
169
# File 'lib/time_crisis/support/time_with_zone.rb', line 167

def ago(other)
  since(-other)
end

#between?(min, max) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/time_crisis/support/time_with_zone.rb', line 119

def between?(min, max)
  utc.between?(min, max)
end

#dst?Boolean Also known as: isdst

Returns:

  • (Boolean)


45
46
47
# File 'lib/time_crisis/support/time_with_zone.rb', line 45

def dst?
  period.dst?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/time_crisis/support/time_with_zone.rb', line 135

def eql?(other)
  utc == other
end

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



61
62
63
# File 'lib/time_crisis/support/time_with_zone.rb', line 61

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

#freezeObject



230
231
232
233
# File 'lib/time_crisis/support/time_with_zone.rb', line 230

def freeze
  period; utc; time
  super
end

#future?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/time_crisis/support/time_with_zone.rb', line 131

def future?
  utc.future?
end

#httpdateObject



90
91
92
# File 'lib/time_crisis/support/time_with_zone.rb', line 90

def httpdate
  utc.httpdate
end

#in_time_zone(new_zone = ::TimeCrisis::Time.zone) ⇒ Object



35
36
37
38
# File 'lib/time_crisis/support/time_with_zone.rb', line 35

def in_time_zone(new_zone = ::TimeCrisis::Time.zone)
  return self if time_zone == new_zone
  utc.in_time_zone(new_zone)
end

#inspectObject



69
70
71
# File 'lib/time_crisis/support/time_with_zone.rb', line 69

def inspect
  "#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Returns:

  • (Boolean)


225
226
227
# File 'lib/time_crisis/support/time_with_zone.rb', line 225

def is_a?(klass)
  klass == ::Time || klass == ::TimeCrisis::Time || super
end

#localtimeObject Also known as: getlocal



40
41
42
# File 'lib/time_crisis/support/time_with_zone.rb', line 40

def localtime
  utc.getlocal
end

#marshal_dumpObject



235
236
237
# File 'lib/time_crisis/support/time_with_zone.rb', line 235

def marshal_dump
  [utc, time_zone.name, time]
end

#marshal_load(variables) ⇒ Object



239
240
241
# File 'lib/time_crisis/support/time_with_zone.rb', line 239

def marshal_load(variables)
  initialize(variables[0].utc, ::TimeCrisis::Time.__send__(:get_zone, variables[1]), variables[2].utc)
end

#past?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/time_crisis/support/time_with_zone.rb', line 123

def past?
  utc.past?
end

#periodObject



31
32
33
# File 'lib/time_crisis/support/time_with_zone.rb', line 31

def period
  @period ||= time_zone.period_for_utc(@utc)
end

#respond_to?(sym, include_priv = false) ⇒ Boolean

Returns:

  • (Boolean)


243
244
245
246
# File 'lib/time_crisis/support/time_with_zone.rb', line 243

def respond_to?(sym, include_priv = false)
  return false if sym.to_s == 'acts_like_date?'
  super || time.respond_to?(sym, include_priv)
end

#rfc2822Object Also known as: rfc822



94
95
96
# File 'lib/time_crisis/support/time_with_zone.rb', line 94

def rfc2822
  to_s(:rfc822)
end

#since(other) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/time_crisis/support/time_with_zone.rb', line 159

def since(other)
  if duration_of_variable_length?(other)
    method_missing(:since, other)
  else
    utc.since(other).in_time_zone(time_zone)
  end
end

#strftime(format) ⇒ Object



110
111
112
113
# File 'lib/time_crisis/support/time_with_zone.rb', line 110

def strftime(format)
  format = format.gsub('%Z', zone).gsub('%z', formatted_offset(false))
  time.strftime(format)
end

#timeObject



19
20
21
# File 'lib/time_crisis/support/time_with_zone.rb', line 19

def time
  @time ||= period.to_local(@utc)
end

#to_aObject



191
192
193
# File 'lib/time_crisis/support/time_with_zone.rb', line 191

def to_a
  [time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone]
end

#to_datetimeObject



213
214
215
# File 'lib/time_crisis/support/time_with_zone.rb', line 213

def to_datetime
  utc.to_datetime.new_offset(Rational(utc_offset, 86_400))
end

#to_fObject



195
196
197
# File 'lib/time_crisis/support/time_with_zone.rb', line 195

def to_f
  utc.to_f
end

#to_iObject Also known as: hash, tv_sec



199
200
201
# File 'lib/time_crisis/support/time_with_zone.rb', line 199

def to_i
  utc.to_i
end

#to_s(format = :default) ⇒ Object Also known as: to_formatted_s



99
100
101
102
103
104
105
106
107
# File 'lib/time_crisis/support/time_with_zone.rb', line 99

def to_s(format = :default)
  if format == :db
    utc.to_s(format)
  elsif formatter = ::TimeCrisis::Time::DATE_FORMATS[format]
    formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
  else
    "#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}"
  end
end

#to_tc_datetimeObject



217
218
219
# File 'lib/time_crisis/support/time_with_zone.rb', line 217

def to_tc_datetime
  utc.to_tc_datetime.new_offset(utc_offset)
end

#to_tc_timeObject



209
210
211
# File 'lib/time_crisis/support/time_with_zone.rb', line 209

def to_tc_time
  self
end

#to_timeObject



205
206
207
# File 'lib/time_crisis/support/time_with_zone.rb', line 205

def to_time
  self
end

#to_yaml(options = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/time_crisis/support/time_with_zone.rb', line 82

def to_yaml(options = {})
  if options.kind_of?(YAML::Emitter)
    utc.to_yaml(options)
  else
    time.to_yaml(options).gsub('Z', formatted_offset(true, 'Z'))
  end
end

#today?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/time_crisis/support/time_with_zone.rb', line 127

def today?
  time.today?
end

#usecObject



187
188
189
# File 'lib/time_crisis/support/time_with_zone.rb', line 187

def usec
  time.respond_to?(:usec) ? time.usec : 0
end

#utcObject Also known as: comparable_time, getgm, getutc, gmtime



23
24
25
# File 'lib/time_crisis/support/time_with_zone.rb', line 23

def utc
  @utc ||= period.to_utc(@time)
end

#utc?Boolean Also known as: gmt?

Returns:

  • (Boolean)


50
51
52
# File 'lib/time_crisis/support/time_with_zone.rb', line 50

def utc?
  time_zone.name == 'UTC'
end

#utc_offsetObject Also known as: gmt_offset, gmtoff



55
56
57
# File 'lib/time_crisis/support/time_with_zone.rb', line 55

def utc_offset
  period.utc_total_offset
end

#xmlschema(fraction_digits = 0) ⇒ Object Also known as: iso8601



73
74
75
76
77
78
79
# File 'lib/time_crisis/support/time_with_zone.rb', line 73

def xmlschema(fraction_digits = 0)
  fraction = if fraction_digits > 0
    ".%i" % time.usec.to_s[0, fraction_digits]
  end

  "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction}#{formatted_offset(true, 'Z')}"
end

#zoneObject



65
66
67
# File 'lib/time_crisis/support/time_with_zone.rb', line 65

def zone
  period.zone_identifier.to_s
end