Class: TimeStep::Calendar
- Inherits:
-
Object
- Object
- TimeStep::Calendar
- 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
-
#calendar ⇒ Object
readonly
Returns the value of attribute calendar.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#tz ⇒ Object
readonly
Returns the value of attribute tz.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #date2jday(year, month, day) ⇒ Object
-
#initialize(calendar = "standard", tz: nil) ⇒ Calendar
constructor
Construct the object.
- #inspect ⇒ Object
- #jday2date(jday) ⇒ Object
-
#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.
- #time_new(year, month, day, hour = 0, min = 0, sec = 0, offset = 0) ⇒ Object
- #valid_datetime_type?(time) ⇒ Boolean
Constructor Details
#initialize(calendar = "standard", tz: nil) ⇒ Calendar
Construct the object
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 58 59 |
# File 'lib/timesteps/timestep_calendar.rb', line 28 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
#calendar ⇒ Object (readonly)
Returns the value of attribute calendar.
61 62 63 |
# File 'lib/timesteps/timestep_calendar.rb', line 61 def calendar @calendar end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
61 62 63 |
# File 'lib/timesteps/timestep_calendar.rb', line 61 def name @name end |
#tz ⇒ Object (readonly)
Returns the value of attribute tz.
61 62 63 |
# File 'lib/timesteps/timestep_calendar.rb', line 61 def tz @tz end |
Instance Method Details
#==(other) ⇒ Object
67 68 69 |
# File 'lib/timesteps/timestep_calendar.rb', line 67 def == (other) return @calendar == other.calendar end |
#date2jday(year, month, day) ⇒ Object
135 136 137 |
# File 'lib/timesteps/timestep_calendar.rb', line 135 def date2jday (year, month, day) return time_new(year, month, day).jd end |
#inspect ⇒ Object
63 64 65 |
# File 'lib/timesteps/timestep_calendar.rb', line 63 def inspect "#<TimeStep::Calendar calendar='#{@calendar.to_s}'>" end |
#jday2date(jday) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/timesteps/timestep_calendar.rb', line 114 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.
89 90 91 |
# File 'lib/timesteps/timestep_calendar.rb', line 89 def parse (timespec, format: nil, offset: nil) return DateTime.(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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/timesteps/timestep_calendar.rb', line 93 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
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/timesteps/timestep_calendar.rb', line 71 def valid_datetime_type? (time) 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 |