Class: CalendarDays

Inherits:
DateTime
  • Object
show all
Includes:
NetCache
Defined in:
lib/calendar_days/calendar_days.rb,
lib/calendar_days/version.rb,
lib/calendar_days/calendar_days.rb,
lib/calendar_days/calendar_days.rb,
lib/calendar_days/calendar_days.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NetCache

#create_repo_dir, #download_repo, #prepare_repo, #prepare_repo!, #repo_dir, #repo_dir_exist?, #repo_exist?, #repo_file, #repo_file_fullpath, #repo_uri

Constructor Details

#initialize(in_year, until_month = nil) ⇒ CalendarDays

Returns a new instance of CalendarDays.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/calendar_days/calendar_days.rb', line 38

def initialize( in_year, until_month = nil )

  until_month ||= 12
  @until_month = until_month
  @until_day   = DateTime.new(in_year, until_month, -1).day

  prepare_repo

  #
  ics_file    = File.open(repo_file_fullpath)
  @ics_events = Icalendar::Event.parse(ics_file).sort_by{|ev| ev.dtstart.to_datetime}
  ics_file.close

  self
end

Instance Attribute Details

#ics_eventsObject (readonly)

Returns the value of attribute ics_events.



54
55
56
# File 'lib/calendar_days/calendar_days.rb', line 54

def ics_events
  @ics_events
end

#until_dayObject

Returns the value of attribute until_day.



53
54
55
# File 'lib/calendar_days/calendar_days.rb', line 53

def until_day
  @until_day
end

#until_monthObject

Returns the value of attribute until_month.



53
54
55
# File 'lib/calendar_days/calendar_days.rb', line 53

def until_month
  @until_month
end

Class Method Details

.new(in_year = DateTime.now.year, since_month = nil) ⇒ Object



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

def self.new( in_year = DateTime.now.year, since_month = nil )
  since_month_tmp = since_month || 1
  tmp = super(in_year, since_month_tmp, 1)
  tmp.instance_eval{ initialize(in_year, since_month) }
end

Instance Method Details

#__weekday_listObject

指定した年(もしくは月)の平日 Weekday (土日祝日を抜いた日) を得る



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/calendar_days/calendar_days.rb', line 81

def __weekday_list
  ret = []

  dt_since = DateTime.new(in_year, since_month, since_day)
  dt_until = DateTime.new(in_year, until_month, until_day)

  dt_tmp = dt_since
  begin
    # if is_weekend?(dt_tmp) or is_holiday?(dt_tmp)
    if yield(dt_tmp)
      ret.push dt_tmp
    end

    dt_tmp += 1
  end while dt_tmp <= dt_until

  ret
end

#date_to_datetime(date) ⇒ Object



73
74
75
76
# File 'lib/calendar_days/calendar_days.rb', line 73

def date_to_datetime( date )
  date = DateTime.parse(date) if date.is_a? ::String
  date
end

#holiday_date(name) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/calendar_days/calendar_days.rb', line 196

def holiday_date( name )
  ret = unless block_given?
          holiday_list.select{|e| e.last =~ /#{name}/i }.map{|e| e.first }
        else
          holiday_list.select{|e| yield(e) }.map{|e| e.first }
        end
  ret = ret.first if ret.size == 1
  ret
end

#holiday_list(events: self.ics_events) ⇒ Object Also known as: holidays

holidays in since..until



149
150
151
152
153
154
# File 'lib/calendar_days/calendar_days.rb', line 149

def holiday_list( events: self.ics_events )
  idx_since = ics_holiday_list.bsearch_index{|al| self.since <= al.first }
  idx_until = ics_holiday_list.bsearch_index{|al| self.until <= al.first } - 1
  # $stderr.puts "idx_since: #{idx_since}, idx_until: #{idx_until}"
  ics_holiday_list[idx_since..idx_until]
end

#holiday_name(date) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/calendar_days/calendar_days.rb', line 182

def holiday_name( date )
  if date.is_a? ::String
    date = DateTime.parse(date)
  end
  __dt = date
  dt = __dt.to_s.gsub(/T.+$/, '')

  if is_holiday?(date)
    holiday_list.select{|e| e.first.to_s =~ /^#{dt}/}.first.last
  else
    nil
  end
end

#ics_endObject



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

def ics_end
  ics_events.last.dtstart.to_datetime
end

#ics_holiday_list(events: self.ics_events) ⇒ Object Also known as: ics_holidays

all holidays defined in ics file.



141
142
143
# File 'lib/calendar_days/calendar_days.rb', line 141

def ics_holiday_list( events: self.ics_events )
  events.map{|ev| [ev.dtstart.to_datetime, ev.summary] }
end

#ics_sinceObject



62
# File 'lib/calendar_days/calendar_days.rb', line 62

def ics_since; DateTime.new(ics_start.year, ics_start.month, ics_start.day); end

#ics_startObject



56
57
58
# File 'lib/calendar_days/calendar_days.rb', line 56

def ics_start
  ics_events.first.dtstart.to_datetime
end

#ics_untilObject



63
# File 'lib/calendar_days/calendar_days.rb', line 63

def ics_until; DateTime.new(ics_end.year,   ics_end.month,   ics_end.day  ); end

#is_holiday?(date) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/calendar_days/calendar_days.rb', line 157

def is_holiday?( date )
  if date.is_a? ::String
    date = DateTime.parse(date)
  end
  __dt = date

  #
  dt_first = ics_events().first.dtstart.to_datetime
  dt_last  = ics_events().last.dtstart.to_datetime
  if __dt.year < dt_first.year
    # return nil
    raise ArgumentError,
      "year #{__dt.year} in #{date} is too old;" \
      " specify after #{dt_first.year}."
  elsif __dt.year > dt_last.year
    # return nil
    raise ArgumentError,
      "year #{__dt.year} in #{date} is too new;" \
      " specify before #{dt_last.year}."
  end

  dt = __dt.to_s.gsub(/T.+$/, '')
  holiday_list.map{|e| e.first.to_s }.grep( /^#{dt}/ ).size > 0
end

#is_saturday?(date) ⇒ Boolean Also known as: is_sat?

Returns:

  • (Boolean)


115
116
117
# File 'lib/calendar_days/calendar_days.rb', line 115

def is_saturday?( date )
  date_to_datetime(date).saturday?
end

#is_sunday?(date) ⇒ Boolean Also known as: is_sun?

Returns:

  • (Boolean)


121
122
123
# File 'lib/calendar_days/calendar_days.rb', line 121

def is_sunday?( date )
  date_to_datetime(date).sunday?
end

#is_weekend?(date) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/calendar_days/calendar_days.rb', line 126

def is_weekend?(date)
  is_saturday?(date) or is_sunday?(date)
end

#sinceObject



36
# File 'lib/calendar_days/calendar_days.rb', line 36

def since; DateTime.new(in_year, since_month, since_day); end

#untilObject



55
# File 'lib/calendar_days/calendar_days.rb', line 55

def until; DateTime.new(in_year, until_month, until_day); end

#weekday_listObject Also known as: weekdays, workingdays



99
100
101
102
# File 'lib/calendar_days/calendar_days.rb', line 99

def weekday_list
  ret = __weekday_list{|dt| not(is_weekend?(dt)) and not(is_holiday?(dt)) }
  ret
end

#weekend_listObject Also known as: weekends

Attention

there exists such days which are both weekend and holiday.



107
108
109
110
# File 'lib/calendar_days/calendar_days.rb', line 107

def weekend_list
  ret = __weekday_list{|dt| is_weekend?(dt) }
  ret
end