Class: Chronic::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/chronic/date.rb

Constant Summary collapse

YEAR_MONTHS =
12
MONTH_DAYS =
[nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
MONTH_DAYS_LEAP =
[nil, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
YEAR_SECONDS =

365 * 24 * 60 * 60

31_536_000
SEASON_SECONDS =

91 * 24 * 60 * 60

7_862_400
MONTH_SECONDS =

30 * 24 * 60 * 60

2_592_000
FORTNIGHT_SECONDS =

14 * 24 * 60 * 60

1_209_600
WEEK_SECONDS =

7 * 24 * 60 * 60

604_800
WEEKEND_SECONDS =

2 * 24 * 60 * 60

172_800
DAY_SECONDS =

24 * 60 * 60

86_400
MONTHS =
{
  :january => 1,
  :february => 2,
  :march => 3,
  :april => 4,
  :may => 5,
  :june => 6,
  :july => 7,
  :august => 8,
  :september => 9,
  :october => 10,
  :november => 11,
  :december => 12
}
DAYS =
{
  :sunday => 0,
  :monday => 1,
  :tuesday => 2,
  :wednesday => 3,
  :thursday => 4,
  :friday => 5,
  :saturday => 6
}

Class Method Summary collapse

Class Method Details

.could_be_day?(day) ⇒ Boolean

Checks if given number could be day

Returns:

  • (Boolean)

38
39
40
# File 'lib/chronic/date.rb', line 38

def self.could_be_day?(day)
  day >= 1 && day <= 31
end

.could_be_month?(month) ⇒ Boolean

Checks if given number could be month

Returns:

  • (Boolean)

43
44
45
# File 'lib/chronic/date.rb', line 43

def self.could_be_month?(month)
  month >= 1 && month <= 12
end

.could_be_year?(year) ⇒ Boolean

Checks if given number could be year

Returns:

  • (Boolean)

48
49
50
# File 'lib/chronic/date.rb', line 48

def self.could_be_year?(year)
  year >= 1 && year <= 9999
end

.make_year(year, bias) ⇒ Object

Build a year from a 2 digit suffix.

year - The two digit Integer year to build from. bias - The Integer amount of future years to bias.

Examples:

make_year(96, 50) #=> 1996
make_year(79, 20) #=> 2079
make_year(00, 50) #=> 2000

Returns The Integer 4 digit year.


64
65
66
67
68
69
70
71
# File 'lib/chronic/date.rb', line 64

def self.make_year(year, bias)
  return year if year.to_s.size > 2
  start_year = Chronic.time_class.now.year - bias
  century = (start_year / 100) * 100
  full_year = century + year
  full_year += 100 if full_year < start_year
  full_year
end

.month_overflow?(year, month, day) ⇒ Boolean

Returns:

  • (Boolean)

73
74
75
76
77
78
79
# File 'lib/chronic/date.rb', line 73

def self.month_overflow?(year, month, day)
  if ::Date.leap?(year)
    day > Date::MONTH_DAYS_LEAP[month]
  else
    day > Date::MONTH_DAYS[month]
  end
end