Module: AWSCronParser::Helpers::Calendar
- Included in:
- CronExpression
- Defined in:
- lib/aws_cron_parser/helpers/calendar.rb
Constant Summary collapse
- AWS_DAY_NAMES =
{ 'SUN' => 1, 'SUNDAY' => 1, 'MON' => 2, 'MONDAY' => 2, 'TUE' => 3, 'TUESDAY' => 3, 'WED' => 4, 'WEDNESDAY' => 4, 'THU' => 5, 'THURSDAY' => 5, 'FRI' => 6, 'FRIDAY' => 6, 'SAT' => 7, 'SATURDAY' => 7 }.freeze
- DAY_NAMES =
{ 'SUN' => 0, 'SUNDAY' => 0, 'MON' => 1, 'MONDAY' => 1, 'TUE' => 2, 'TUESDAY' => 2, 'WED' => 3, 'WEDNESDAY' => 3, 'THU' => 4, 'THURSDAY' => 4, 'FRI' => 5, 'FRIDAY' => 5, 'SAT' => 6, 'SATURDAY' => 6 }.freeze
- WEEKDAY_NAMES =
Weekday names (0-6: SUN-SAT, where 0=Sunday, 6=Saturday)
%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].freeze
- AWS_WEEKDAY_NAMES =
AWS weekday names (1-7: SUN-SAT, where 1=Sunday, 7=Saturday). Must stay aligned with AWS_DAY_NAMES above and with the parser, which matches candidates on Date#wday + 1.
[nil, 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'].freeze
- MONTH_NAMES =
Month names (1-based indexing for CRON)
[nil, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'].freeze
- MONTH_ABBREVIATIONS =
Month abbreviations (for parsing JAN, FEB, etc.) Using upcase in lookup to avoid case duplication
{ 'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6, 'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12 }.freeze
- MONTH_FULL_NAMES =
Month full names (for parsing JANUARY, FEBRUARY, etc.) Using upcase in lookup to avoid case duplication
{ 'JANUARY' => 1, 'FEBRUARY' => 2, 'MARCH' => 3, 'APRIL' => 4, 'MAY' => 5, 'JUNE' => 6, 'JULY' => 7, 'AUGUST' => 8, 'SEPTEMBER' => 9, 'OCTOBER' => 10, 'NOVEMBER' => 11, 'DECEMBER' => 12 }.freeze
Instance Method Summary collapse
-
#day_name(num_or_str, is_aws: false) ⇒ Object
Convert day name from number or string.
-
#month_abbr_to_num(abbr) ⇒ Object
Convert month abbreviation to number.
-
#month_name(num) ⇒ Object
Map month name from number.
-
#weekday_abbr_to_num(abbr, is_aws: false) ⇒ Object
Convert weekday abbreviation to number.
-
#weekday_name_from_abbr(abbr) ⇒ Object
Convert weekday name from abbreviation.
Instance Method Details
#day_name(num_or_str, is_aws: false) ⇒ Object
Convert day name from number or string
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/aws_cron_parser/helpers/calendar.rb', line 53 def day_name(num_or_str, is_aws: false) if num_or_str.is_a?(String) && num_or_str.match?(/[A-Z]/) case num_or_str.upcase when 'MON' then 'Monday' when 'TUE' then 'Tuesday' when 'WED' then 'Wednesday' when 'THU' then 'Thursday' when 'FRI' then 'Friday' when 'SAT' then 'Saturday' when 'SUN' then 'Sunday' else num_or_str end else weekday_names = is_aws ? AWS_WEEKDAY_NAMES : WEEKDAY_NAMES num = num_or_str.to_i # Standard CRON spells Sunday either 0 or 7; AWS only uses 1-7. num = 0 if !is_aws && num == 7 weekday_names[num] || "day #{num_or_str}" end end |
#month_abbr_to_num(abbr) ⇒ Object
Convert month abbreviation to number
80 81 82 83 84 85 86 87 88 |
# File 'lib/aws_cron_parser/helpers/calendar.rb', line 80 def month_abbr_to_num(abbr) raise ArgumentError, "Invalid month abbreviation: #{abbr.inspect}" unless abbr.is_a?(String) && !abbr.empty? return abbr.to_i if /^\d+$/.match?(abbr) upcase_abbr = abbr.upcase MONTH_FULL_NAMES[upcase_abbr] || MONTH_ABBREVIATIONS[upcase_abbr] || (raise ArgumentError, "Invalid month abbreviation: #{abbr}") end |
#month_name(num) ⇒ Object
Map month name from number
75 76 77 |
# File 'lib/aws_cron_parser/helpers/calendar.rb', line 75 def month_name(num) MONTH_NAMES[num.to_i] || "month #{num}" end |
#weekday_abbr_to_num(abbr, is_aws: false) ⇒ Object
Convert weekday abbreviation to number
91 92 93 94 95 96 97 98 |
# File 'lib/aws_cron_parser/helpers/calendar.rb', line 91 def weekday_abbr_to_num(abbr, is_aws: false) raise ArgumentError, "Invalid weekday abbreviation: #{abbr.inspect}" unless abbr.is_a?(String) && !abbr.empty? return abbr.to_i if /^\d+$/.match?(abbr) day_names = is_aws ? AWS_DAY_NAMES : DAY_NAMES day_names[abbr.upcase] || (raise ArgumentError, "Invalid weekday abbreviation: #{abbr}") end |
#weekday_name_from_abbr(abbr) ⇒ Object
Convert weekday name from abbreviation
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/aws_cron_parser/helpers/calendar.rb', line 101 def weekday_name_from_abbr(abbr) case abbr.upcase when 'MON' then 'Monday' when 'TUE' then 'Tuesday' when 'WED' then 'Wednesday' when 'THU' then 'Thursday' when 'FRI' then 'Friday' when 'SAT' then 'Saturday' when 'SUN' then 'Sunday' else abbr end end |