Class: TZInfo::TimezonePeriod

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

Overview

A period of time in a timezone where the same offset from UTC applies.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(utc_start, utc_end, utc_offset, std_offset, zone_identifier) ⇒ TimezonePeriod

Initializes a new TimezonePeriod.



380
381
382
383
384
385
386
387
388
389
# File 'lib/tzinfo/timezone.rb', line 380

def initialize(utc_start, utc_end, utc_offset, std_offset, zone_identifier)
  @utc_start = utc_start
  @utc_end = utc_end
  @utc_offset = utc_offset
  @std_offset = std_offset
  @zone_identifier = zone_identifier            
  
  @local_start = utc_start.nil? ? nil : to_local(utc_start)
  @local_end = utc_end.nil? ? nil : to_local(utc_end)             
end

Instance Attribute Details

#local_endObject (readonly)

End time of the period (local time). May be nil if unbounded.



377
378
379
# File 'lib/tzinfo/timezone.rb', line 377

def local_end
  @local_end
end

#local_startObject (readonly)

Start time of the period (local time). May be nil if unbounded.



374
375
376
# File 'lib/tzinfo/timezone.rb', line 374

def local_start
  @local_start
end

#std_offsetObject (readonly)

Offset from the local time where daylight savings is in effect (seconds). E.g.: utc_offset could be -5 hours. Normally, std_offset would be 0. During daylight savings, std_offset would become +1 hours.



366
367
368
# File 'lib/tzinfo/timezone.rb', line 366

def std_offset
  @std_offset
end

#utc_endObject (readonly)

End time of the period (UTC). May be nil if unbounded.



358
359
360
# File 'lib/tzinfo/timezone.rb', line 358

def utc_end
  @utc_end
end

#utc_offsetObject (readonly)

Base offset of the timezone from UTC (seconds).



361
362
363
# File 'lib/tzinfo/timezone.rb', line 361

def utc_offset
  @utc_offset
end

#utc_startObject (readonly)

Start time of the period (UTC). May be nil if unbounded.



355
356
357
# File 'lib/tzinfo/timezone.rb', line 355

def utc_start
  @utc_start
end

#zone_identifierObject (readonly)

The identifier of this period, e.g. “GMT” (Greenwich Mean Time) or “BST” (British Summer Time) for “Europe/London”. The returned identifier is a symbol.



371
372
373
# File 'lib/tzinfo/timezone.rb', line 371

def zone_identifier
  @zone_identifier
end

Instance Method Details

#dst?Boolean

true if daylight savings is in effect for this period; otherwise false.

Returns:

  • (Boolean)


402
403
404
# File 'lib/tzinfo/timezone.rb', line 402

def dst?
  std_offset != 0
end

#local_after_start?(local) ⇒ Boolean

true if the given local DateTime is after the start of the period; otherwise false.

Returns:

  • (Boolean)


427
428
429
# File 'lib/tzinfo/timezone.rb', line 427

def local_after_start?(local)
  @local_start.nil? || @local_start <= local
end

#local_before_end?(local) ⇒ Boolean

true if the given local DateTime is before the end of the period; otherwise false.

Returns:

  • (Boolean)


432
433
434
# File 'lib/tzinfo/timezone.rb', line 432

def local_before_end?(local)
  @local_end.nil? || @local_end > local
end

#to_local(utc) ⇒ Object

Converts a UTC DateTime to local time based on the offset of this period.



437
438
439
# File 'lib/tzinfo/timezone.rb', line 437

def to_local(utc)
  utc + utc_total_offset_rational
end

#to_utc(local) ⇒ Object

Converts a local DateTime to UTC based on the offset of this period.



442
443
444
# File 'lib/tzinfo/timezone.rb', line 442

def to_utc(local)
  local - utc_total_offset_rational
end

#utc_after_start?(utc) ⇒ Boolean

true if the given utc DateTime is after the start of the period; otherwise false.

Returns:

  • (Boolean)


412
413
414
# File 'lib/tzinfo/timezone.rb', line 412

def utc_after_start?(utc)
  @utc_start.nil? || @utc_start <= utc
end

#utc_before_end?(utc) ⇒ Boolean

true if the given utc DateTime is before the end of the period; otherwise false.

Returns:

  • (Boolean)


417
418
419
# File 'lib/tzinfo/timezone.rb', line 417

def utc_before_end?(utc)
  @utc_end.nil? || @utc_end > utc
end

#utc_total_offsetObject

Total offset from UTC (seconds). Equal to utc_offset + std_offset.



392
393
394
# File 'lib/tzinfo/timezone.rb', line 392

def utc_total_offset
  utc_offset + std_offset
end

#utc_total_offset_rationalObject

Total offset from UTC (days). Result is a Rational.



397
398
399
# File 'lib/tzinfo/timezone.rb', line 397

def utc_total_offset_rational
  Rational(utc_total_offset, 86400)
end

#valid_for_local?(local) ⇒ Boolean

true if this period is valid for the given local DateTime; otherwise false.

Returns:

  • (Boolean)


422
423
424
# File 'lib/tzinfo/timezone.rb', line 422

def valid_for_local?(local)
  local_after_start?(local) && local_before_end?(local) 
end

#valid_for_utc?(utc) ⇒ Boolean

true if this period is valid for the given utc DateTime; otherwise false.

Returns:

  • (Boolean)


407
408
409
# File 'lib/tzinfo/timezone.rb', line 407

def valid_for_utc?(utc)
  utc_after_start?(utc) && utc_before_end?(utc) 
end