Class: Data::LocalDateTime

Inherits:
LocalTime
  • Object
show all
Defined in:
lib/barometer/data/local_datetime.rb

Overview

A simple DateTime class

A time class that represents the local date_time … it has no concept of time zone

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(y, mon, d, h = 0, m = 0, s = 0) ⇒ LocalDateTime

Returns a new instance of LocalDateTime.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/barometer/data/local_datetime.rb', line 13

def initialize(y,mon,d,h=0,m=0,s=0)
  raise(ArgumentError, "invalid date") unless y && mon && d && Date.civil(y,mon,d)
  @year = y
  @month = mon
  @day = d
  super(h,m,s)
  self
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



11
12
13
# File 'lib/barometer/data/local_datetime.rb', line 11

def day
  @day
end

#monthObject

Returns the value of attribute month.



11
12
13
# File 'lib/barometer/data/local_datetime.rb', line 11

def month
  @month
end

#yearObject

Returns the value of attribute year.



11
12
13
# File 'lib/barometer/data/local_datetime.rb', line 11

def year
  @year
end

Class Method Details

.parse(string) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/barometer/data/local_datetime.rb', line 55

def self.parse(string)
  return nil unless string
  return string if string.is_a?(Data::LocalDateTime)
  
  year = nil; month = nil; day = nil;
  hour = nil; min = nil; sec = nil;
  if string.is_a?(Time) || string.is_a?(DateTime)
    year = string.year
    month = string.mon
    day = string.day
    hour = string.hour
    min = string.min
    sec = string.sec
  elsif string.is_a?(Date)
    year = string.year
    month = string.mon
    day = string.day
  elsif string.is_a?(String)
    begin
      datetime = Time.parse(string)
      year = datetime.year
      month = datetime.mon
      day = datetime.day
      hour = datetime.hour
      min = datetime.min
      sec = datetime.sec
    rescue ArgumentError
      return nil
    end
  end
  Data::LocalDateTime.new(year, month, day, hour, min, sec)
end

Instance Method Details

#<=>(other) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/barometer/data/local_datetime.rb', line 100

def <=>(other)
  if other.is_a?(String) || other.is_a?(Time) || other.is_a?(DateTime) || other.is_a?(Date)
    the_other = Data::LocalDateTime.parse(other)
  else
    the_other = other
  end
  raise ArgumentError unless the_other.is_a?(Data::LocalDateTime) || the_other.is_a?(Data::LocalTime)
  
  if ((other.is_a?(String) || other.is_a?(Time) || other.is_a?(DateTime)) &&
    the_other.is_a?(Data::LocalDateTime)) || other.is_a?(Data::LocalDateTime)
    # we are counting days + seconds
    if (_total_days <=> the_other._total_days) == 0
      return total_seconds <=> the_other.total_seconds
    else
      return _total_days <=> the_other._total_days
    end
  elsif other.is_a?(Date) && the_other.is_a?(Data::LocalDateTime)
    # we are counting days
    return _total_days <=> the_other._total_days
  elsif the_other.is_a?(Data::LocalTime)
    # we are counting seconds
    return total_seconds <=> the_other.total_seconds
  end
end

#_total_daysObject

this assumes all years have 366 days (which only is true for leap years) but since this is just for comparisons, this will be accurate



136
137
138
# File 'lib/barometer/data/local_datetime.rb', line 136

def _total_days
  self.to_d.yday + (@year * 366)
end

#nil?Boolean

Returns:

  • (Boolean)


131
# File 'lib/barometer/data/local_datetime.rb', line 131

def nil?; @year == 0 && @month == 0 && @day == 0 && super; end

#parse(string) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/barometer/data/local_datetime.rb', line 43

def parse(string)
  return unless string
  new_date = Data::LocalDateTime.parse(string)
  @year = new_date.year
  @month = new_date.month
  @day = new_date.day
  @hour = new_date.hour
  @min = new_date.min
  @sec = new_date.sec
  self
end

#to_dObject

convert to a Date class



90
91
92
# File 'lib/barometer/data/local_datetime.rb', line 90

def to_d
  Date.civil(@year, @month, @day)
end

#to_dtObject

convert to a DateTime class



96
97
98
# File 'lib/barometer/data/local_datetime.rb', line 96

def to_dt
  DateTime.new(@year, @month, @day, @hour, @min, @sec)
end

#to_s(time = false) ⇒ Object



125
126
127
128
129
# File 'lib/barometer/data/local_datetime.rb', line 125

def to_s(time=false)
  datetime = self.to_dt
  format = (time ? "%Y-%m-%d %I:%M:%S %p" : "%Y-%m-%d")
  datetime.strftime(format).downcase
end