class CalendarDays

class CalendarDays < ::DateTime

Constants

VERSION

Attributes

ics_events[R]
since_day[R]
since_month[R]
since_year[R]
until_day[R]
until_month[R]
until_year[R]

Public Class Methods

new( _y = nil, _m = nil ) click to toggle source

Args

_y

year

_m

month

# File lib/calendar_days/calendar_days.rb, line 49
def initialize( _y = nil, _m = nil )

  #
  self.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

  @since_year  = (_y || self.ics_start.year).to_i
  @since_month = (_m ||  1).to_i
  @since_day   = 1

  @until_year  = (_y || self.ics_end.year).to_i

  #ng. @until_month = _m || (_y.nil?)? self.ics_end.month : 12
  @until_month = (_m || ((_y.nil?)? self.ics_end.month : 12)).to_i
  @until_day   = DateTime.new(@until_year, @until_month, -1).day


  raise ArgumentError, "You specified #{[_y, _m].compact.join(' and ')}."+
    " Specify year (and month) in the date range #{to_s_date(ics_start)}"+
    " - #{to_s_date(ics_end)}." unless valid_date?

  self
end

Public Instance Methods

__ics_end() click to toggle source
# File lib/calendar_days/calendar_days.rb, line 85
def __ics_end
  ics_events.last.dtstart.to_datetime
end
__ics_start() click to toggle source
# File lib/calendar_days/calendar_days.rb, line 82
def __ics_start
  ics_events.first.dtstart.to_datetime
end
__weekday_list( dt_since = self.since, dt_until = self.until ) { |d| ... } click to toggle source

get the weekdays from the specified date range except saturday, sunday, and holiday. 指定した年(もしくは月)の平日 Weekday (土日祝日を抜いた日) を得る

# File lib/calendar_days/calendar_days.rb, line 111
def __weekday_list( dt_since = self.since, dt_until = self.until )
  (dt_since..dt_until).select{|d| yield(d) }
end
buisiness_days()
Alias for: weekday_list
buisinessdays()
Alias for: weekday_list
date_to_datetime( date ) click to toggle source
# File lib/calendar_days/calendar_days.rb, line 31
def date_to_datetime( date )
  date = DateTime.parse(date) if date.is_a? ::String
  date
end
holiday?( date )
Alias for: is_holiday?
holiday_date( name ) { |e| ... } click to toggle source

get the date of holiday from name.

Args

name

name of holiday.

Return

Name or [ Name, … ] (in case two or more dates are matched)

# File lib/calendar_days/calendar_days.rb, line 253
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 ) click to toggle source

holidays in since..until

# File lib/calendar_days/calendar_days.rb, line 200
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
Also aliased as: holidays
holiday_name( date ) click to toggle source
# File lib/calendar_days/calendar_days.rb, line 234
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
holidays( events: self.ics_events )
Alias for: holiday_list
ics_business_days()
Alias for: ics_weekday_list
ics_businessdays()
Alias for: ics_weekday_list
ics_end() click to toggle source
# File lib/calendar_days/calendar_days.rb, line 92
def ics_end
  tmp = __ics_end
  DateTime.new(tmp.year, tmp.month, -1)
end
Also aliased as: ics_until
ics_holiday_list( events: self.ics_events ) click to toggle source

all holidays defined in ics file.

# File lib/calendar_days/calendar_days.rb, line 192
def ics_holiday_list( events: self.ics_events )
  events.map{|ev| [ev.dtstart.to_datetime, ev.summary] }
end
Also aliased as: ics_holidays
ics_holidays( events: self.ics_events )
Alias for: ics_holiday_list
ics_since()
Alias for: ics_start
ics_start() click to toggle source
# File lib/calendar_days/calendar_days.rb, line 88
def ics_start
  tmp = __ics_start
  DateTime.new(tmp.year, tmp.month, 1)
end
Also aliased as: ics_since
ics_until()
Alias for: ics_end
ics_week_days()
Alias for: ics_weekday_list
ics_weekday_list() click to toggle source

get the weekdays defined in the ics file.

# File lib/calendar_days/calendar_days.rb, line 127
def ics_weekday_list
  __weekday_list(ics_start, ics_end){|dt|
    not(is_weekend?(dt)) and not(is_holiday?(dt))
  }
end
ics_weekdays()
Alias for: ics_weekday_list
ics_weekend_list() click to toggle source
# File lib/calendar_days/calendar_days.rb, line 155
def ics_weekend_list
  __weekday_list(ics_start, ics_end){|dt| is_weekend?(dt) }
end
Also aliased as: ics_weekends
ics_weekends()
Alias for: ics_weekend_list
ics_working_days()
Alias for: ics_weekday_list
ics_workingdays()
Alias for: ics_weekday_list
is_holiday?( date ) click to toggle source
# File lib/calendar_days/calendar_days.rb, line 208
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
Also aliased as: holiday?
is_sat?( date )
Alias for: is_saturday?
is_saturday?( date ) click to toggle source
# File lib/calendar_days/calendar_days.rb, line 164
def is_saturday?( date )
  date_to_datetime(date).saturday?
end
Also aliased as: is_sat?, saturday?
is_sun?( date )
Alias for: is_sunday?
is_sunday?( date ) click to toggle source
# File lib/calendar_days/calendar_days.rb, line 170
def is_sunday?( date )
  date_to_datetime(date).sunday?
end
Also aliased as: is_sun?, sunday?
is_weekend?(date) click to toggle source
# File lib/calendar_days/calendar_days.rb, line 176
def is_weekend?(date)
  is_saturday?(date) or is_sunday?(date)
end
Also aliased as: weekend?
saturday?( date )
Alias for: is_saturday?
since() click to toggle source
# File lib/calendar_days/calendar_days.rb, line 79
def since; DateTime.new(since_year, since_month, since_day); end
sunday?( date )
Alias for: is_sunday?
to_s_date( date ) click to toggle source
# File lib/calendar_days/calendar_days.rb, line 27
def to_s_date( date )
  date.to_s.gsub(/T.+$/, '')
end
until() click to toggle source
# File lib/calendar_days/calendar_days.rb, line 80
def until; DateTime.new(until_year, until_month, until_day); end
valid_date?(date_since = self.since, date_until = self.until) click to toggle source
# File lib/calendar_days/calendar_days.rb, line 36
def valid_date?(date_since = self.since, date_until = self.until)
  if ics_start <= date_since and date_until <= ics_end
    true
  else
    false
  end
end
week_days()
Alias for: weekday_list
weekday_list() click to toggle source

get the weekdays from the user-specified date range.

# File lib/calendar_days/calendar_days.rb, line 118
def weekday_list
  __weekday_list{|dt|
    not(is_weekend?(dt)) and not(is_holiday?(dt))
  }
end
weekdays()
Alias for: weekday_list
weekend?(date)
Alias for: is_weekend?
weekend_list() click to toggle source

get saturdays and sundays.

Attention

there exists such days which are both weekend and holiday.

# File lib/calendar_days/calendar_days.rb, line 152
def weekend_list
  __weekday_list{|dt| is_weekend?(dt) }
end
Also aliased as: weekends
weekends()
Alias for: weekend_list
working_days()
Alias for: weekday_list
workingdays()
Alias for: weekday_list