Module: DateTimeGroup

Includes:
Format, Zones
Included in:
ActiveSupport::TimeWithZone, DateTime, Time
Defined in:
lib/dtg/date_time_group.rb

Constant Summary

Constants included from Format

Format::FORMAT

Constants included from Zones

Zones::UTC_ZONES

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(dtg, zone = :z) ⇒ Object

Change will convert a DTG into a different zone with respect to its zone code



79
80
81
82
# File 'lib/dtg/date_time_group.rb', line 79

def self.convert(dtg, zone = :z)
  old = from_dtg(dtg)
  old.to_dtg(zone)
end

.from_dtg(dtg) ⇒ Object

From will convert from DTG to a Time (not enough data to recreate AS or DateTime)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dtg/date_time_group.rb', line 47

def self.from_dtg(dtg)
  dtg_hash = parse(dtg)
  year = dtg_hash[:year].to_i
  if (Time.now.year % 100) < year
    year = Time.now.year
  else
    year_temp = Time.now.year
    year += year_temp - year_temp % 100
  end
  zone = UTC_ZONES[dtg_hash[:zone].downcase.to_sym].to_s
  if zone.nil? | zone.strip.empty?
    zone = '+00:00'
  elsif zone == 'UTC'
    zone = '+00:00'
  elsif zone.size == 2
    zone = zone[0] + '0' + zone[1] + ':00'
  else
    zone += ':00'
  end
  # Year, month, Day, Hour, Minute, Second, Offset
  Time.new(
    year,
    dtg_hash[:month],
    dtg_hash[:day].to_i,
    dtg_hash[:hour].to_i,
    dtg_hash[:minute].to_i,
    0,
    zone
  )
end

.parse(dtg) ⇒ Object

Parse will strip the DTG into its different fields



35
36
37
38
39
40
41
42
43
44
# File 'lib/dtg/date_time_group.rb', line 35

def self.parse(dtg)
  {
    day: dtg[FORMAT[:day_range]],
    hour: dtg[FORMAT[:hour_range]],
    minute: dtg[FORMAT[:minute_range]],
    zone: dtg[FORMAT[:zone_range]],
    month: dtg[FORMAT[:month_range]],
    year: dtg[FORMAT[:year_range]]
  }
end

Instance Method Details

#convert(zone = :z) ⇒ Object

Convert the object into the proper zone specified



28
29
30
31
32
# File 'lib/dtg/date_time_group.rb', line 28

def convert(zone = :z)
  key = zone.downcase.to_sym
  raise "Error: #{zone} is not a valid zone" unless UTC_ZONES.key?(key)
  key == :j ? self.dup : self.in_time_zone(UTC_ZONES[key])
end

#dtgObject

DTG Test to determine if was injected properly into class for native use



15
16
17
# File 'lib/dtg/date_time_group.rb', line 15

def dtg
  "DTG gem is natively integrated with this class: #{self.class}"
end

#format(zone = :z) ⇒ Object

Format the object into specified zone (as dtg)



20
21
22
23
24
25
# File 'lib/dtg/date_time_group.rb', line 20

def format(zone = :z)
  key = zone.downcase.to_sym
  raise "Error: #{zone} is not a valid zone" unless UTC_ZONES.key?(key)
  dtg = "%d%H%M#{key.upcase.to_s} %b %y"
  strftime(dtg)
end

#to_dtg(zone = :z) ⇒ Object

Convert the object to the proper zone and then format as dtg in zone



10
11
12
# File 'lib/dtg/date_time_group.rb', line 10

def to_dtg(zone = :z)
  convert(zone).format(zone)
end