Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/cotcube-helpers/swig/date.rb,
lib/cotcube-helpers/datetime_ext.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cw(week:, year: Date.today.year) ⇒ Object

creates a range of 2 dates, of the given calendar week, Monday to Sunday



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
# File 'lib/cotcube-helpers/datetime_ext.rb', line 28

def self.cw( week: , year: Date.today.year )
  form = '%Y %W %w'
  build_range = lambda {|w|
    begin
	( DateTime.strptime("#{year} #{w} 1", form).to_date..
DateTime.strptime("#{year} #{w} 0", form).to_date)
    rescue
	# beyond Dec 31st #strptime must be called with cw:0 to keep it working
	( DateTime.strptime("#{year} #{w} 1", form).to_date..
DateTime.strptime("#{year+1} 0  0", form).to_date)
    end
  }
  case week
  when :current
    build_range.call(Date.today.cweek)
  when :last
    wday = Date.today.wday
    build_range.call((Date.today - (wday.zero? ? 7 : wday)).cweek)
  when Integer
    raise ArgumentError, "'#{week}' is not supported as calendar week, choose from (1..53)" if week <= 0 or week > 53
    build_range.call(week)
  else
    raise ArgumentError, "'#{week}' is not a supported format for calendar week"
  end
end

Instance Method Details

#abbr_daynameObject



13
14
15
# File 'lib/cotcube-helpers/swig/date.rb', line 13

def abbr_dayname
  ABBR_DAYNAMES[self.wday]
end

#to_stringObject



17
18
19
# File 'lib/cotcube-helpers/swig/date.rb', line 17

def to_string
  self.strftime("%Y-%m-%d")
end

#to_timestringObject



21
22
23
# File 'lib/cotcube-helpers/swig/date.rb', line 21

def to_timestring
  self.strftime("%Y-%m-%d-%H-%M-%S")
end