Class: Hijri::Date
- Inherits:
-
Object
- Object
- Hijri::Date
- Includes:
- Comparable
- Defined in:
- lib/hijri/date.rb,
lib/hijri/format.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Format
Constant Summary collapse
- MONTHNAMES =
[nil] + %w(Muharram Safar Rabia-Awwal Rabia-Thani Jumaada-Awal Jumaada-Thani Rajab Sha'ban Ramadan Shawwal Dhul-Qi'dah Dhul-Hijjah)
- DAYNAMES =
%w(as-Sabt al-Ahad al-Ithnayn ath-Thalaathaa al-Arba'aa' al-Khamis al-Jumu'ah)
- ABBR_MONTHNAMES =
[nil] + ["Muharram", "Safar", "Rabia I", "Rabia II", "Jumaada I", "Jumaada II", "Rajab", "Sha'ban", "Ramadan", "Shawwal", "Dhul-Qi'dah", "Dhul-Hijjah"]
- ABBR_DAYNAMES =
%w(Sabt Ahad Ithnayn Thalaathaa Arba'aa' Khamis Jumu'ah)
- HALF_DAYS_IN_DAY =
:nodoc:
Rational(1, 2)
- HOURS_IN_DAY =
:nodoc:
Rational(1, 24)
- MINUTES_IN_DAY =
:nodoc:
Rational(1, 1440)
- SECONDS_IN_DAY =
:nodoc:
Rational(1, 86400)
- MILLISECONDS_IN_DAY =
:nodoc:
Rational(1, 86400*10**3)
- NANOSECONDS_IN_DAY =
:nodoc:
Rational(1, 86400*10**9)
- MILLISECONDS_IN_SECOND =
:nodoc:
Rational(1, 10**3)
- NANOSECONDS_IN_SECOND =
:nodoc:
Rational(1, 10**9)
Instance Attribute Summary collapse
-
#day ⇒ Object
(also: #mday)
readonly
Returns the value of attribute day.
-
#month ⇒ Object
(also: #mon)
readonly
Returns the value of attribute month.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
-
._httpdate(str) ⇒ Object
:nodoc:.
-
._iso8601(str) ⇒ Object
:nodoc:.
-
._jisx0301(str) ⇒ Object
:nodoc:.
- ._parse(str, comp = true) ⇒ Object
-
._rfc2822(str) ⇒ Object
(also: _rfc822)
:nodoc:.
-
._rfc3339(str) ⇒ Object
:nodoc:.
- ._strptime(str, fmt = '%F') ⇒ Object
-
._xmlschema(str) ⇒ Object
:nodoc:.
- .today ⇒ Object
Instance Method Summary collapse
- #<=>(date) ⇒ Object
-
#asctime ⇒ Object
(also: #ctime)
alias_method :format, :strftime.
- #change(kargs) ⇒ Object
-
#httpdate ⇒ Object
:nodoc:.
-
#initialize(year = 1, month = 1, day = 1) ⇒ Date
constructor
A new instance of Date.
- #iso8601 ⇒ Object
- #jisx0301 ⇒ Object
- #rfc2822 ⇒ Object (also: #rfc822)
- #rfc3339 ⇒ Object
- #strftime(fmt = '%F') ⇒ Object
- #to_abs ⇒ Object (also: #abs)
- #to_greo(adjust = 0) ⇒ Object
-
#to_hijri ⇒ Object
Just to have a consistent Interface.
- #to_s ⇒ Object
- #valid_date?(year, month, day) ⇒ Boolean
- #wday ⇒ Object
- #weeknum(week_start = 0) ⇒ Object (also: #cweek)
-
#xmlschema ⇒ Object
:nodoc:.
- #yday ⇒ Object
Constructor Details
#initialize(year = 1, month = 1, day = 1) ⇒ Date
Returns a new instance of Date.
35 36 37 38 39 40 41 |
# File 'lib/hijri/date.rb', line 35 def initialize(year=1, month=1, day=1) if valid_date?(year, month, day) @year, @month, @day = year, month, day else raise ArgumentError, "Invalid Date" end end |
Instance Attribute Details
#day ⇒ Object (readonly) Also known as: mday
Returns the value of attribute day.
5 6 7 |
# File 'lib/hijri/date.rb', line 5 def day @day end |
#month ⇒ Object (readonly) Also known as: mon
Returns the value of attribute month.
5 6 7 |
# File 'lib/hijri/date.rb', line 5 def month @month end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
5 6 7 |
# File 'lib/hijri/date.rb', line 5 def year @year end |
Class Method Details
._httpdate(str) ⇒ Object
:nodoc:
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 |
# File 'lib/hijri/format.rb', line 1191 def self._httpdate(str) # :nodoc: if /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+ \d{2}\s+ (#{Format::ABBR_MONTHS.keys.join('|')})\s+ -?\d{4}\s+ # allow minus, anyway \d{2}:\d{2}:\d{2}\s+ gmt\s*\z/iox =~ str _rfc2822(str) elsif /\A\s*(#{Format::DAYS.keys.join('|')})\s*,\s+ \d{2}\s*-\s* (#{Format::ABBR_MONTHS.keys.join('|')})\s*-\s* \d{2}\s+ \d{2}:\d{2}:\d{2}\s+ gmt\s*\z/iox =~ str _parse(str) elsif /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s+ (#{Format::ABBR_MONTHS.keys.join('|')})\s+ \d{1,2}\s+ \d{2}:\d{2}:\d{2}\s+ \d{4}\s*\z/iox =~ str _parse(str) end end |
._iso8601(str) ⇒ Object
:nodoc:
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 |
# File 'lib/hijri/format.rb', line 1092 def self._iso8601(str) # :nodoc: if /\A\s*(([-+]?\d{2,}|-)-\d{2}-\d{2}| ([-+]?\d{2,})?-\d{3}| (\d{2}|\d{4})?-w\d{2}-\d| -w-\d) (t \d{2}:\d{2}(:\d{2}([,.]\d+)?)? (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str _parse(str) elsif /\A\s*(([-+]?(\d{2}|\d{4})|--)\d{2}\d{2}| ([-+]?(\d{2}|\d{4}))?\d{3}|-\d{3}| (\d{2}|\d{4})?w\d{2}\d) (t? \d{2}\d{2}(\d{2}([,.]\d+)?)? (z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str _parse(str) elsif /\A\s*(\d{2}:\d{2}(:\d{2}([,.]\d+)?)? (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str _parse(str) elsif /\A\s*(\d{2}\d{2}(\d{2}([,.]\d+)?)? (z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str _parse(str) end end |
._jisx0301(str) ⇒ Object
:nodoc:
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 |
# File 'lib/hijri/format.rb', line 1215 def self._jisx0301(str) # :nodoc: if /\A\s*[mtsh]?\d{2}\.\d{2}\.\d{2} (t (\d{2}:\d{2}(:\d{2}([,.]\d*)?)? (z|[-+]\d{2}(:?\d{2})?)?)?)?\s*\z/ix =~ str if /\A\s*\d/ =~ str _parse(str.sub(/\A\s*(\d)/, 'h\1')) else _parse(str) end else _iso8601(str) end end |
._parse(str, comp = true) ⇒ Object
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 |
# File 'lib/hijri/format.rb', line 1026 def self._parse(str, comp=true) str = str.dup e = Format::Bag.new e._comp = comp str.gsub!(/[^-+',.\/:@[:alnum:]\[\]]+/, ' ') _parse_time(str, e) # || _parse_beat(str, e) _parse_day(str, e) _parse_eu(str, e) || _parse_us(str, e) || _parse_iso(str, e) || _parse_jis(str, e) || _parse_vms(str, e) || _parse_sla(str, e) || _parse_dot(str, e) || _parse_iso2(str, e) || _parse_year(str, e) || _parse_mon(str, e) || _parse_mday(str, e) || _parse_ddd(str, e) if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/i, ' ') if e.year e.year = -e.year + 1 end end if str.sub!(/\A\s*(\d{1,2})\s*\z/, ' ') if e.hour && !e.mday v = $1.to_i if (1..31) === v e.mday = v end end if e.mday && !e.hour v = $1.to_i if (0..24) === v e.hour = v end end end if e._comp if e.cwyear if e.cwyear >= 0 && e.cwyear <= 99 e.cwyear += if e.cwyear >= 69 then 1900 else 2000 end end end if e.year if e.year >= 0 && e.year <= 99 e.year += if e.year >= 69 then 1900 else 2000 end end end end e.offset ||= zone_to_diff(e.zone) if e.zone e.to_hash end |
._rfc2822(str) ⇒ Object Also known as: _rfc822
:nodoc:
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 |
# File 'lib/hijri/format.rb', line 1170 def self._rfc2822(str) # :nodoc: if /\A\s*(?:(?:#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+)? \d{1,2}\s+ (?:#{Format::ABBR_MONTHS.keys.join('|')})\s+ -?(\d{2,})\s+ # allow minus, anyway \d{2}:\d{2}(:\d{2})?\s* (?:[-+]\d{4}|ut|gmt|e[sd]t|c[sd]t|m[sd]t|p[sd]t|[a-ik-z])\s*\z/iox =~ str e = _parse(str, false) if $1.size < 4 if e[:year] < 50 e[:year] += 2000 elsif e[:year] < 1000 e[:year] += 1900 end end e end end |
._rfc3339(str) ⇒ Object
:nodoc:
1117 1118 1119 1120 1121 1122 1123 1124 |
# File 'lib/hijri/format.rb', line 1117 def self._rfc3339(str) # :nodoc: if /\A\s*-?\d{4}-\d{2}-\d{2} # allow minus, anyway (t|\s) \d{2}:\d{2}:\d{2}(\.\d+)? (z|[-+]\d{2}:\d{2})\s*\z/ix =~ str _parse(str) end end |
._strptime(str, fmt = '%F') ⇒ Object
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
# File 'lib/hijri/format.rb', line 574 def self._strptime(str, fmt='%F') str = str.dup e = Format::Bag.new return unless _strptime_i(str, fmt, e) if e._cent if e.cwyear e.cwyear += e._cent * 100 end if e.year e. year += e._cent * 100 end end if e._merid if e.hour e.hour %= 12 e.hour += e._merid end end unless str.empty? e.leftover = str end e.to_hash end |
._xmlschema(str) ⇒ Object
:nodoc:
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 |
# File 'lib/hijri/format.rb', line 1126 def self._xmlschema(str) # :nodoc: if /\A\s*(-?\d{4,})(?:-(\d{2})(?:-(\d{2}))?)? (?:t (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?)? (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str e = Format::Bag.new e.year = $1.to_i e.mon = $2.to_i if $2 e.mday = $3.to_i if $3 e.hour = $4.to_i if $4 e.min = $5.to_i if $5 e.sec = $6.to_i if $6 e.sec_fraction = Rational($7.to_i, 10**$7.size) if $7 if $8 e.zone = $8 e.offset = zone_to_diff($8) end e.to_hash elsif /\A\s*(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))? (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str e = Format::Bag.new e.hour = $1.to_i if $1 e.min = $2.to_i if $2 e.sec = $3.to_i if $3 e.sec_fraction = Rational($4.to_i, 10**$4.size) if $4 if $5 e.zone = $5 e.offset = zone_to_diff($5) end e.to_hash elsif /\A\s*(?:--(\d{2})(?:-(\d{2}))?|---(\d{2})) (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str e = Format::Bag.new e.mon = $1.to_i if $1 e.mday = $2.to_i if $2 e.mday = $3.to_i if $3 if $4 e.zone = $4 e.offset = zone_to_diff($4) end e.to_hash end end |
.today ⇒ Object
29 30 31 32 |
# File 'lib/hijri/date.rb', line 29 def today date = ::Date.today date.to_hijri end |
Instance Method Details
#<=>(date) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/hijri/date.rb', line 55 def <=>(date) # Make sure the date is a Hijri::Date instance date = date.to_hijri if self.to_s == date.to_s return 0 elsif @year > date.year || (@year == date.year && @month > date.month) || (@year == date.year && @month == date.month && @day > date.day) return 1 else return -1 end end |
#asctime ⇒ Object Also known as: ctime
alias_method :format, :strftime
343 |
# File 'lib/hijri/format.rb', line 343 def asctime() strftime('%c') end |
#change(kargs) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/hijri/date.rb', line 43 def change(kargs) # Remove nil values kargs.reject!{|k,v| v.nil?} @year = kargs.fetch :year , year @month = kargs.fetch :month, month @day = kargs.fetch :day , day end |
#httpdate ⇒ Object
:nodoc:
357 |
# File 'lib/hijri/format.rb', line 357 def httpdate() new_offset(0).strftime('%a, %d %b %Y %T GMT') end |
#iso8601 ⇒ Object
347 |
# File 'lib/hijri/format.rb', line 347 def iso8601() strftime('%F') end |
#jisx0301 ⇒ Object
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/hijri/format.rb', line 359 def jisx0301 if jd < 2405160 iso8601 else case jd when 2405160...2419614 g = 'M%02d' % (year - 1867) when 2419614...2424875 g = 'T%02d' % (year - 1911) when 2424875...2447535 g = 'S%02d' % (year - 1925) else g = 'H%02d' % (year - 1988) end g + strftime('.%m.%d') end end |
#rfc2822 ⇒ Object Also known as: rfc822
353 |
# File 'lib/hijri/format.rb', line 353 def rfc2822() strftime('%a, %-d %b %Y %T %z') end |
#rfc3339 ⇒ Object
349 |
# File 'lib/hijri/format.rb', line 349 def rfc3339() iso8601 end |
#strftime(fmt = '%F') ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/hijri/format.rb', line 217 def strftime(fmt='%F') fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do f = {} m = $& s, w, c = $1, $2, $3 if s s.scan(/./) do |k| case k when '-'; f[:p] = '-' when '_'; f[:p] = "\s" when '0'; f[:p] = '0' when '^'; f[:u] = true when '#'; f[:x] = true end end end if w f[:w] = w.to_i end case c when 'A'; emit_ad(DAYNAMES[wday], 0, f) when 'a'; emit_ad(ABBR_DAYNAMES[wday], 0, f) when 'B'; emit_ad(MONTHNAMES[mon], 0, f) when 'b'; emit_ad(ABBR_MONTHNAMES[mon], 0, f) when 'C', 'EC'; emit_sn((year / 100).floor, 2, f) when 'c', 'Ec'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f) when 'D'; emit_a(strftime('%m/%d/%y'), 0, f) when 'd', 'Od'; emit_n(mday, 2, f) when 'e', 'Oe'; emit_a(mday, 2, f) when 'F' if m == '%F' format('%.4d-%02d-%02d', year, mon, mday) # 4p else emit_a(strftime('%Y-%m-%d'), 0, f) end when 'G'; emit_sn(cwyear, 4, f) when 'g'; emit_n(cwyear % 100, 2, f) when 'H', 'OH'; emit_n(hour, 2, f) when 'h'; emit_ad(strftime('%b'), 0, f) when 'I', 'OI'; emit_n((hour % 12).nonzero? || 12, 2, f) when 'j'; emit_n(yday, 3, f) when 'k'; emit_a(hour, 2, f) when 'L' f[:p] = nil w = f[:w] || 3 u = 10**w emit_n((sec_fraction * u).floor, w, f) when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f) when 'M', 'OM'; emit_n(min, 2, f) when 'm', 'Om'; emit_n(mon, 2, f) when 'N' f[:p] = nil w = f[:w] || 9 u = 10**w emit_n((sec_fraction * u).floor, w, f) when 'n'; emit_a("\n", 0, f) when 'P'; emit_ad(strftime('%p').downcase, 0, f) when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f) when 'Q' s = ((ajd - UNIX_EPOCH_IN_AJD) / MILLISECONDS_IN_DAY).round emit_sn(s, 1, f) when 'R'; emit_a(strftime('%H:%M'), 0, f) when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f) when 'S', 'OS'; emit_n(sec, 2, f) when 's' s = ((ajd - UNIX_EPOCH_IN_AJD) / SECONDS_IN_DAY).round emit_sn(s, 1, f) when 'T' if m == '%T' format('%02d:%02d:%02d', hour, min, sec) # 4p else emit_a(strftime('%H:%M:%S'), 0, f) end when 't'; emit_a("\t", 0, f) when 'U', 'W', 'OU', 'OW' emit_n(if c[-1,1] == 'U' then wnum0 else wnum1 end, 2, f) when 'u', 'Ou'; emit_n(cwday, 1, f) when 'V', 'OV'; emit_n(cweek, 2, f) when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f) when 'w', 'Ow'; emit_n(wday, 1, f) when 'X', 'EX'; emit_a(strftime('%H:%M:%S'), 0, f) when 'x', 'Ex'; emit_a(strftime('%m/%d/%y'), 0, f) when 'Y', 'EY'; emit_sn(year, 4, f) when 'y', 'Ey', 'Oy'; emit_n(year % 100, 2, f) when 'Z'; emit_au(strftime('%:z'), 0, f) when /\A(:{0,3})z/ t = $1.size sign = if offset < 0 then -1 else +1 end fr = offset.abs ss = fr.div(SECONDS_IN_DAY) # 4p hh, ss = ss.divmod(3600) mm, ss = ss.divmod(60) if t == 3 if ss.nonzero? then t = 2 elsif mm.nonzero? then t = 1 else t = -1 end end case t when -1 tail = [] sep = '' when 0 f[:w] -= 2 if f[:w] tail = ['%02d' % mm] sep = '' when 1 f[:w] -= 3 if f[:w] tail = ['%02d' % mm] sep = ':' when 2 f[:w] -= 6 if f[:w] tail = ['%02d' % mm, '%02d' % ss] sep = ':' end ([emit_z(sign * hh, 2, f)] + tail).join(sep) when '%'; emit_a('%', 0, f) when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f) else m end end end |
#to_abs ⇒ Object Also known as: abs
69 70 71 72 73 74 75 76 |
# File 'lib/hijri/date.rb', line 69 def to_abs month_days = 29 * (month - 1) # days on this year nonleap_year_days = 354 * (year - 1) leap_year_days = (3 + (11 * year)) / 30.0 this_year = (month / 2.0).to_i return (day + month_days + this_year + nonleap_year_days + leap_year_days + Hijri::ISLAMIC_EPOCH).to_i end |
#to_greo(adjust = 0) ⇒ Object
79 80 81 |
# File 'lib/hijri/date.rb', line 79 def to_greo(adjust=0) ::Date.new(*Converter.hijri_to_greo(self)) + adjust end |
#to_hijri ⇒ Object
Just to have a consistent Interface.
84 85 86 |
# File 'lib/hijri/date.rb', line 84 def to_hijri self end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/hijri/date.rb', line 51 def to_s format('%.4d-%02d-%02d', year, month, day) end |
#valid_date?(year, month, day) ⇒ Boolean
88 89 90 91 92 93 |
# File 'lib/hijri/date.rb', line 88 def valid_date?(year, month, day) return false unless (1..INFINITY).cover?(year) return false unless (1..12).cover?(month) return false unless (1..30).cover?(day) return true end |
#wday ⇒ Object
99 100 101 |
# File 'lib/hijri/date.rb', line 99 def wday (((year * AVERAGE_YEARS_DAYS) + yday) % 7).floor end |
#weeknum(week_start = 0) ⇒ Object Also known as: cweek
103 104 105 |
# File 'lib/hijri/date.rb', line 103 def weeknum(week_start=0) yday / 7 end |
#xmlschema ⇒ Object
:nodoc:
351 |
# File 'lib/hijri/format.rb', line 351 def xmlschema() iso8601 end |
#yday ⇒ Object
95 96 97 |
# File 'lib/hijri/date.rb', line 95 def yday (((month - 1) * AVERAGE_MONTH_DAYS) + day).floor end |