Module: UKAcademicCalendar::DateAndTimeInstanceMethods

Included in:
Date, Time
Defined in:
lib/uk_academic_calendar/mixins/date_and_time_instance_methods.rb

Overview

Mixin included in Date and Time classes providing academic year related instance methods

Instance Method Summary collapse

Instance Method Details

#academic_termUKAcademicCalendar::AutumnTerm, ...

Returns term object including self.

Examples:

Date.new(2024, 8, 31).academic_term #=> Summer 2023/2024
Time.new(2024, 11, 1, 12).academic_term #=> Autumn 2024/2025

Returns:



52
53
54
# File 'lib/uk_academic_calendar/mixins/date_and_time_instance_methods.rb', line 52

def academic_term
  UKAcademicCalendar.term_including self
end

#academic_yearInteger

Returns integer representing the calendar year within the academic year started in.

Examples:

Date.new(2024, 8, 31).academic_year #=> 2023
Time.new(2024, 9, 1, 12).academic_year #=> 2024

Returns:

  • (Integer)

    integer representing the calendar year within the academic year started in



10
11
12
13
14
# File 'lib/uk_academic_calendar/mixins/date_and_time_instance_methods.rb', line 10

def academic_year
  return year if yday >= start_yday

  year - 1
end

#academic_year_monthInteger

Returns integer representing the month of the academic year including self.

Examples:

Date.new(2024, 8, 31).academic_year_month #=> 12
Time.new(2024, 11, 1, 12).academic_year_month #=> 3

Returns:

  • (Integer)

    integer representing the month of the academic year including self



42
43
44
45
# File 'lib/uk_academic_calendar/mixins/date_and_time_instance_methods.rb', line 42

def academic_year_month
  ac_yr_start_month = YEAR_OFFSET_START_MONTH
  ((month - ac_yr_start_month) % 12) + 1
end

#beginning_of_academic_term#to_date

Returns instance of self.class representing the beginning of the academic term including self.

Examples:

Date.new(2024, 2, 25).beginning_of_academic_term #=> 2024-01-01
Time.new(2024, 9, 1, 12).beginning_of_academic_term #=> 2024-09-01 00:00:00 +0100

Returns:

  • (#to_date)

    instance of self.class representing the beginning of the academic term including self



60
61
62
63
64
65
66
# File 'lib/uk_academic_calendar/mixins/date_and_time_instance_methods.rb', line 60

def beginning_of_academic_term
  term_start = academic_term.start_date
  shifted = change(year: term_start.year, month: term_start.month, day: term_start.day)
  return shifted unless acts_like? :time

  shifted.beginning_of_day
end

#beginning_of_academic_year#to_date

Returns instance of self.class representing the beginning of the academic year.

Examples:

Date.new(2024, 8, 31).beginning_of_academic_year #=> 2023-09-01
Time.new(2024, 9, 1, 12).beginning_of_academic_year #=> 2024-09-01 00:00:00 +0100

Returns:

  • (#to_date)

    instance of self.class representing the beginning of the academic year



20
21
22
23
24
25
# File 'lib/uk_academic_calendar/mixins/date_and_time_instance_methods.rb', line 20

def beginning_of_academic_year
  shifted = change(year: academic_year, month: YEAR_OFFSET_START_MONTH, day: 1)
  return shifted unless acts_like? :time

  shifted.beginning_of_day
end

#end_of_academic_term#to_date

Returns instance of self.class representing the end of the academic term including self.

Examples:

Date.new(2024, 11, 25).end_of_academic_term #=> 2024-12-31
Time.new(2024, 7, 1, 12).end_of_academic_term #=> 2024-08-31 23:59:999999999 +0100

Returns:

  • (#to_date)

    instance of self.class representing the end of the academic term including self



72
73
74
75
76
77
78
# File 'lib/uk_academic_calendar/mixins/date_and_time_instance_methods.rb', line 72

def end_of_academic_term
  term_end = academic_term.end_date
  shifted = change(year: term_end.year, month: term_end.month, day: term_end.day)
  return shifted unless acts_like? :time

  shifted.end_of_day
end

#end_of_academic_year#to_date

Returns instance of self.class representing the end of the academic year.

Examples:

Date.new(2024, 8, 31).end_of_academic_year #=> 2024-08-31
Time.new(2024, 9, 1, 12).end_of_academic_year #=> 2025-08-31 23:59:59.999999999 +0100

Returns:

  • (#to_date)

    instance of self.class representing the end of the academic year



31
32
33
34
35
36
# File 'lib/uk_academic_calendar/mixins/date_and_time_instance_methods.rb', line 31

def end_of_academic_year
  shifted = beginning_of_academic_year.next_year.prev_day
  return shifted unless acts_like? :time

  shifted.end_of_day
end