Class: TimeStep::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/timesteps/timestep_calendar.rb

Constant Summary collapse

DateTimeType =
{
  :standard => DateTime,
  :proleptic_gregorian => DateTime,
  :proleptic_julian => DateTime,
  :noleap => DateTime::NoLeap,
  :allleap => DateTime::AllLeap,
  SYM_360_day => DateTime::Fixed360Day,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(calendar = "standard", tz: nil) ⇒ Calendar

Construct the object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/timesteps/timestep_calendar.rb', line 26

def initialize (calendar = "standard", tz: nil)
  unless calendar.is_a?(String)
    raise ArgumentError, "argument calendar '#{calendar}' should be string" 
  end
  @name = calendar
  case @name.downcase.intern
  when :standard, :gregorian
    @calendar = :standard
  when :proleptic_gregorian
    @calendar = :proleptic_gregorian
  when :proleptic_julian, :julian
    @calendar = :proleptic_julian
  when :noleap, SYM_365_day
    @calendar = :noleap
  when :allleap, SYM_366_day
    @calendar = :allleap
  when SYM_360_day
    @calendar = SYM_360_day
  end
  if tz 
    raise "'standard' calendar needed for tz" unless @calendar == :standard
    case tz
    when nil
    when String
      @tz = TZInfo::Timezone.get(tz)
    when TZInfo::Timezone
      @tz = tz
    else
      raise "invalid tz specification"
    end
  end
end

Instance Attribute Details

#calendarObject (readonly)

Returns the value of attribute calendar.



59
60
61
# File 'lib/timesteps/timestep_calendar.rb', line 59

def calendar
  @calendar
end

#nameObject (readonly)

Returns the value of attribute name.



59
60
61
# File 'lib/timesteps/timestep_calendar.rb', line 59

def name
  @name
end

#tzObject (readonly)

Returns the value of attribute tz.



59
60
61
# File 'lib/timesteps/timestep_calendar.rb', line 59

def tz
  @tz
end

Instance Method Details

#==(other) ⇒ Object



65
66
67
# File 'lib/timesteps/timestep_calendar.rb', line 65

def == (other)
  return @calendar == other.calendar 
end

#date2jday(year, month, day) ⇒ Object



136
137
138
# File 'lib/timesteps/timestep_calendar.rb', line 136

def date2jday (year, month, day)
  return time_new(year, month, day).jd
end

#inspectObject



61
62
63
# File 'lib/timesteps/timestep_calendar.rb', line 61

def inspect 
  "#<TimeStep::Calendar calendar='#{@calendar.to_s}'>"
end

#jday2date(jday) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/timesteps/timestep_calendar.rb', line 115

def jday2date (jday)
  case @calendar
  when :standard
    time = DateTime.jd(jday, 0, 0, 0, 0, Date::ITALY)
  when :proleptic_gregorian
    time = DateTime.jd(jday, 0, 0, 0, 0, Date::GREGORIAN)
  when :proleptic_julian
    time = DateTime.jd(jday, 0, 0, 0, 0, Date::JULIAN)
  when :noleap
    time = DateTime::NoLeap.jd(jday, 0, 0, 0, 0)
  when :allleap
    time = DateTime::AllLeap.jd(jday, 0, 0, 0, 0)
  when SYM_360_day
    time = DateTime::Fixed360Day.jd(jday, 0, 0, 0, 0)
  end
  if @tz
    time = @tz.to_local(time)
  end
  return time
end

#parse(timespec, format: nil, offset: nil) ⇒ DateTime object

Parses the given representation of date and time in the calendar, and creates an date time instance.

Returns:



90
91
92
# File 'lib/timesteps/timestep_calendar.rb', line 90

def parse (timespec, format: nil, offset: nil)
  return DateTime.parse_timestamp(timespec, calendar: @calendar.to_s, format: format, tz: @tz, offset: offset)
end

#time_new(year, month, day, hour = 0, min = 0, sec = 0, offset = 0) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/timesteps/timestep_calendar.rb', line 94

def time_new (year, month, day, hour=0, min=0, sec=0, offset=0)
  case @calendar
  when :standard
    time = DateTime.new(year, month, day, hour, min, sec, offset, Date::ITALY)
  when :proleptic_gregorian
    time = DateTime.new(year, month, day, hour, min, sec, offset, Date::GREGORIAN)
  when :proleptic_julian
    time = DateTime.new(year, month, day, hour, min, sec, offset, Date::JULIAN)
  when :noleap
    time = DateTime::NoLeap.new(year, month, day, hour, min, sec, offset)
  when :allleap
    time = DateTime::AllLeap.new(year, month, day, hour, min, sec, offset)
  when SYM_360_day
    time = DateTime::Fixed360Day.new(year, month, day, hour, min, sec, offset)
  end
  if @tz
    time = @tz.to_local(time)
  end
  return time
end

#valid_datetime_type?(time) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/timesteps/timestep_calendar.rb', line 69

def valid_datetime_type? (time)
  if @calendar == :standard and time.is_a?(TZInfo::DateTimeWithOffset)
    return true
  end
  return false unless time.is_a?(DateTimeType[@calendar]) 
  case @calendar
  when :standard
    return time.start == Date::ITALY
  when :proleptic_gregorian
    return time.start == Date::GREGORIAN
  when :proleptic_julian
    return time.start == Date::JULIAN
  else
    return true
  end 
end