Class: CarbonDate::StandardFormatter
- Defined in:
- lib/carbon_date/standard_formatter.rb
Overview
The default formatter for CarbonDate::Date
Constant Summary collapse
- BCE_SUFFIX =
Suffix to use for Before Common Era dates (quite often BCE or BC)
'BCE'- MONTHS =
Collection of strings denoting month names for this Formatter
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
Instance Method Summary collapse
-
#billion_years(date) ⇒ Object
Formats a CarbonDate::Date with billion_years precision Returns: A human-readable string representing the Date.
-
#century(date) ⇒ Object
Formats a CarbonDate::Date with century precision Returns: A human-readable string representing the Date.
-
#coarse_precision(date_year, interval) ⇒ Object
A helper function used to format dates that have less than millenium precision Params:
date_yearThe year component of the CarbonDate::Date being formattedintervalThe precision in years Returns: A human-readable string representing the Date. -
#day(date) ⇒ Object
Formats a CarbonDate::Date with day precision as a string Returns: A human-readable string representing the Date.
-
#decade(date) ⇒ Object
Formats a CarbonDate::Date with decade precision Returns: A human-readable string representing the Date.
-
#hour(date) ⇒ Object
Formats a CarbonDate::Date with hour precision as a string Returns: A human-readable string representing the Date.
-
#hundred_million_years(date) ⇒ Object
Formats a CarbonDate::Date with hundred_million_years precision Returns: A human-readable string representing the Date.
-
#hundred_thousand_years(date) ⇒ Object
Formats a CarbonDate::Date with hundred_thousand_years precision Returns: A human-readable string representing the Date.
-
#millennium(date) ⇒ Object
Formats a CarbonDate::Date with millennium precision Returns: A human-readable string representing the Date.
-
#million_years(date) ⇒ Object
Formats a CarbonDate::Date with million_years precision Returns: A human-readable string representing the Date.
-
#minute(date) ⇒ Object
Formats a CarbonDate::Date with minute precision as a string Returns: A human-readable string representing the Date.
-
#month(date) ⇒ Object
Formats a CarbonDate::Date with month precision as a string Returns: A human-readable string representing the Date.
-
#number_with_delimiter(n, delim = ',') ⇒ Object
Converts an integer number into a human-readable string with thousands delimiters.
-
#pad(s) ⇒ Object
Pad a string with ‘0’ to ensure it has two characters.
-
#second(date) ⇒ Object
Formats a CarbonDate::Date with second precision as a string Returns: A human-readable string representing the Date.
-
#ten_million_years(date) ⇒ Object
Formats a CarbonDate::Date with ten_million_years precision Returns: A human-readable string representing the Date.
-
#ten_thousand_years(date) ⇒ Object
Formats a CarbonDate::Date with ten_thousand_years precision Returns: A human-readable string representing the Date.
-
#year(date) ⇒ Object
Formats a CarbonDate::Date with year precision as a string Returns: A human-readable string representing the Date.
Methods inherited from Formatter
Instance Method Details
#billion_years(date) ⇒ Object
Formats a CarbonDate::Date with billion_years precision Returns: A human-readable string representing the Date
128 129 130 |
# File 'lib/carbon_date/standard_formatter.rb', line 128 def billion_years(date) coarse_precision(date.year, 1e9.to_i) end |
#century(date) ⇒ Object
Formats a CarbonDate::Date with century precision Returns: A human-readable string representing the Date
75 76 77 78 79 |
# File 'lib/carbon_date/standard_formatter.rb', line 75 def century(date) c = ((date.year.abs.to_i / 100) + 1).ordinalize + ' century' return [c, BCE_SUFFIX].join(' ') if (date.year <= -1) c end |
#coarse_precision(date_year, interval) ⇒ Object
A helper function used to format dates that have less than millenium precision Params: date_year The year component of the CarbonDate::Date being formatted interval The precision in years Returns: A human-readable string representing the Date
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/carbon_date/standard_formatter.rb', line 138 def coarse_precision(date_year, interval) date_year = date_year.to_i interval = interval.to_i year_diff = date_year - ::Date.today.year return "Within the last #{number_with_delimiter(interval)} years" if (-(interval - 1)..0).include? year_diff return "Within the next #{number_with_delimiter(interval)} years" if (1..(interval - 1)).include? year_diff rounded = (year_diff.to_f / interval.to_f).round * interval return "in #{number_with_delimiter(rounded.abs)} years" if rounded > 0 return "#{number_with_delimiter(rounded.abs)} years ago" if rounded < 0 nil end |
#day(date) ⇒ Object
Formats a CarbonDate::Date with day precision as a string Returns: A human-readable string representing the Date
34 35 36 |
# File 'lib/carbon_date/standard_formatter.rb', line 34 def day(date) [date.day.ordinalize.to_s, month(date)].join(' ') end |
#decade(date) ⇒ Object
Formats a CarbonDate::Date with decade precision Returns: A human-readable string representing the Date
66 67 68 69 70 |
# File 'lib/carbon_date/standard_formatter.rb', line 66 def decade(date) d = ((date.year.abs.to_i / 10) * 10).to_s + 's' return [d, BCE_SUFFIX].join(' ') if (date.year <= -1) d end |
#hour(date) ⇒ Object
Formats a CarbonDate::Date with hour precision as a string Returns: A human-readable string representing the Date
41 42 43 44 45 |
# File 'lib/carbon_date/standard_formatter.rb', line 41 def hour(date) h = date.minute >= 30 ? date.hour + 1 : date.hour time = [pad(h.to_s), '00'].join(':') [time, day(date)].join(' ') end |
#hundred_million_years(date) ⇒ Object
Formats a CarbonDate::Date with hundred_million_years precision Returns: A human-readable string representing the Date
121 122 123 |
# File 'lib/carbon_date/standard_formatter.rb', line 121 def hundred_million_years(date) coarse_precision(date.year, 100e6.to_i) end |
#hundred_thousand_years(date) ⇒ Object
Formats a CarbonDate::Date with hundred_thousand_years precision Returns: A human-readable string representing the Date
100 101 102 |
# File 'lib/carbon_date/standard_formatter.rb', line 100 def hundred_thousand_years(date) coarse_precision(date.year, 100e3.to_i) end |
#millennium(date) ⇒ Object
Formats a CarbonDate::Date with millennium precision Returns: A human-readable string representing the Date
84 85 86 87 88 |
# File 'lib/carbon_date/standard_formatter.rb', line 84 def millennium(date) m = ((date.year.abs.to_i / 1000) + 1).ordinalize + ' millennium' return [m, BCE_SUFFIX].join(' ') if (date.year <= -1) m end |
#million_years(date) ⇒ Object
Formats a CarbonDate::Date with million_years precision Returns: A human-readable string representing the Date
107 108 109 |
# File 'lib/carbon_date/standard_formatter.rb', line 107 def million_years(date) coarse_precision(date.year, 1e6.to_i) end |
#minute(date) ⇒ Object
Formats a CarbonDate::Date with minute precision as a string Returns: A human-readable string representing the Date
50 51 52 53 |
# File 'lib/carbon_date/standard_formatter.rb', line 50 def minute(date) time = [pad(date.hour.to_s), pad(date.minute.to_s)].join(':') [time, day(date)].join(' ') end |
#month(date) ⇒ Object
Formats a CarbonDate::Date with month precision as a string Returns: A human-readable string representing the Date
27 28 29 |
# File 'lib/carbon_date/standard_formatter.rb', line 27 def month(date) [MONTHS[date.month - 1], year(date)].join(', ') end |
#number_with_delimiter(n, delim = ',') ⇒ Object
Converts an integer number into a human-readable string with thousands delimiters. Example:
number_with_delimiter(1234567890, ',')
=> 1,234,567,890
Params: n the number to be delimited. Will be converted to an integer delim the string to be used as the thousands delimiter. Defaults to ‘,’
161 162 163 |
# File 'lib/carbon_date/standard_formatter.rb', line 161 def number_with_delimiter(n, delim = ',') n.to_i.to_s.reverse.chars.each_slice(3).map(&:join).join(delim).reverse end |
#pad(s) ⇒ Object
Pad a string with ‘0’ to ensure it has two characters. If a string of 2 or more characters is used as parameter, will not alter the string. Example:
pad('1')
=> "01"
Params: s The string to pad. Returns: A string of at least 2 characters
174 175 176 |
# File 'lib/carbon_date/standard_formatter.rb', line 174 def pad(s) s.rjust(2, '0') end |
#second(date) ⇒ Object
Formats a CarbonDate::Date with second precision as a string Returns: A human-readable string representing the Date
58 59 60 61 |
# File 'lib/carbon_date/standard_formatter.rb', line 58 def second(date) time = [pad(date.hour.to_s), pad(date.minute.to_s), pad(date.second.to_s)].join(':') [time, day(date)].join(' ') end |
#ten_million_years(date) ⇒ Object
Formats a CarbonDate::Date with ten_million_years precision Returns: A human-readable string representing the Date
114 115 116 |
# File 'lib/carbon_date/standard_formatter.rb', line 114 def ten_million_years(date) coarse_precision(date.year, 10e6.to_i) end |
#ten_thousand_years(date) ⇒ Object
Formats a CarbonDate::Date with ten_thousand_years precision Returns: A human-readable string representing the Date
93 94 95 |
# File 'lib/carbon_date/standard_formatter.rb', line 93 def ten_thousand_years(date) coarse_precision(date.year, 10e3.to_i) end |
#year(date) ⇒ Object
Formats a CarbonDate::Date with year precision as a string Returns: A human-readable string representing the Date
18 19 20 21 22 |
# File 'lib/carbon_date/standard_formatter.rb', line 18 def year(date) y = date.year.abs.to_s return [y, BCE_SUFFIX].join(' ') if (date.year <= -1) y end |