opal/corelib/time.rb in opal-1.3.2 vs opal/corelib/time.rb in opal-1.4.0.alpha1

- old
+ new

@@ -1,11 +1,11 @@ # helpers: slice require 'corelib/comparable' -class Time < `Date` - include Comparable +class ::Time < `Date` + include ::Comparable %x{ var days_of_week = #{%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday]}, short_days = #{%w[Sun Mon Tue Wed Thu Fri Sat]}, short_months = #{%w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec]}, @@ -14,29 +14,29 @@ def self.at(seconds, frac = undefined) %x{ var result; - if (#{Time === seconds}) { + if (#{::Time === seconds}) { if (frac !== undefined) { - #{raise TypeError, "can't convert Time into an exact number"} + #{::Kernel.raise ::TypeError, "can't convert Time into an exact number"} } result = new Date(seconds.getTime()); result.is_utc = seconds.is_utc; return result; } if (!seconds.$$is_number) { - seconds = #{Opal.coerce_to!(seconds, Integer, :to_int)}; + seconds = #{::Opal.coerce_to!(seconds, ::Integer, :to_int)}; } if (frac === undefined) { return new Date(seconds * 1000); } if (!frac.$$is_number) { - frac = #{Opal.coerce_to!(frac, Integer, :to_int)}; + frac = #{::Opal.coerce_to!(frac, ::Integer, :to_int)}; } return new Date(seconds * 1000 + (frac / 1000)); } end @@ -44,11 +44,11 @@ %x{ function time_params(year, month, day, hour, min, sec) { if (year.$$is_string) { year = parseInt(year, 10); } else { - year = #{Opal.coerce_to!(`year`, Integer, :to_int)}; + year = #{::Opal.coerce_to!(`year`, ::Integer, :to_int)}; } if (month === nil) { month = 1; } else if (!month.$$is_number) { @@ -68,67 +68,67 @@ case 'nov': month = 11; break; case 'dec': month = 12; break; default: month = #{`month`.to_i}; } } else { - month = #{Opal.coerce_to!(`month`, Integer, :to_int)}; + month = #{::Opal.coerce_to!(`month`, ::Integer, :to_int)}; } } if (month < 1 || month > 12) { - #{raise ArgumentError, "month out of range: #{`month`}"} + #{::Kernel.raise ::ArgumentError, "month out of range: #{`month`}"} } month = month - 1; if (day === nil) { day = 1; } else if (day.$$is_string) { day = parseInt(day, 10); } else { - day = #{Opal.coerce_to!(`day`, Integer, :to_int)}; + day = #{::Opal.coerce_to!(`day`, ::Integer, :to_int)}; } if (day < 1 || day > 31) { - #{raise ArgumentError, "day out of range: #{`day`}"} + #{::Kernel.raise ::ArgumentError, "day out of range: #{`day`}"} } if (hour === nil) { hour = 0; } else if (hour.$$is_string) { hour = parseInt(hour, 10); } else { - hour = #{Opal.coerce_to!(`hour`, Integer, :to_int)}; + hour = #{::Opal.coerce_to!(`hour`, ::Integer, :to_int)}; } if (hour < 0 || hour > 24) { - #{raise ArgumentError, "hour out of range: #{`hour`}"} + #{::Kernel.raise ::ArgumentError, "hour out of range: #{`hour`}"} } if (min === nil) { min = 0; } else if (min.$$is_string) { min = parseInt(min, 10); } else { - min = #{Opal.coerce_to!(`min`, Integer, :to_int)}; + min = #{::Opal.coerce_to!(`min`, ::Integer, :to_int)}; } if (min < 0 || min > 59) { - #{raise ArgumentError, "min out of range: #{`min`}"} + #{::Kernel.raise ::ArgumentError, "min out of range: #{`min`}"} } if (sec === nil) { sec = 0; } else if (!sec.$$is_number) { if (sec.$$is_string) { sec = parseInt(sec, 10); } else { - sec = #{Opal.coerce_to!(`sec`, Integer, :to_int)}; + sec = #{::Opal.coerce_to!(`sec`, ::Integer, :to_int)}; } } if (sec < 0 || sec > 60) { - #{raise ArgumentError, "sec out of range: #{`sec`}"} + #{::Kernel.raise ::ArgumentError, "sec out of range: #{`sec`}"} } return [year, month, day, hour, min, sec]; } } @@ -140,11 +140,11 @@ if (year === undefined) { return new Date(); } if (utc_offset !== nil) { - #{raise ArgumentError, 'Opal does not support explicitly specifying UTC offset for Time'} + #{::Kernel.raise ::ArgumentError, 'Opal does not support explicitly specifying UTC offset for Time'} } args = time_params(year, month, day, hour, min, sec); year = args[0]; month = args[1]; @@ -222,51 +222,46 @@ result.is_utc = true; return result; } end - class << self - alias mktime local - alias utc gm - end - def self.now new end def +(other) - if Time === other - raise TypeError, 'time + time?' + if ::Time === other + ::Kernel.raise ::TypeError, 'time + time?' end %x{ if (!other.$$is_number) { - other = #{Opal.coerce_to!(other, Integer, :to_int)}; + other = #{::Opal.coerce_to!(other, ::Integer, :to_int)}; } var result = new Date(self.getTime() + (other * 1000)); result.is_utc = self.is_utc; return result; } end def -(other) - if Time === other + if ::Time === other return `(self.getTime() - other.getTime()) / 1000` end %x{ if (!other.$$is_number) { - other = #{Opal.coerce_to!(other, Integer, :to_int)}; + other = #{::Opal.coerce_to!(other, ::Integer, :to_int)}; } var result = new Date(self.getTime() - (other * 1000)); result.is_utc = self.is_utc; return result; } end def <=>(other) - if Time === other + if ::Time === other to_f <=> other.to_f else r = other <=> self if r.nil? nil @@ -279,19 +274,17 @@ end end end def ==(other) - Time === other && `#{to_f} === #{other.to_f}` + ::Time === other && `#{to_f} === #{other.to_f}` end def asctime strftime '%a %b %e %H:%M:%S %Y' end - alias ctime asctime - def day `self.is_utc ? self.getUTCDate() : self.getDate()` end def yday @@ -311,23 +304,21 @@ jul = new Date(self.getFullYear(), 6, 1); return self.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()); } end - alias dst? isdst - def dup copy = `new Date(self.getTime())` copy.copy_instance_variables(self) copy.initialize_dup(self) copy end def eql?(other) - other.is_a?(Time) && (self <=> other).zero? + other.is_a?(::Time) && (self <=> other).zero? end def friday? `#{wday} == 5` end @@ -346,12 +337,10 @@ else strftime '%Y-%m-%d %H:%M:%S %z' end end - alias mday day - def min `self.is_utc ? self.getUTCMinutes() : self.getMinutes()` end def mon @@ -360,12 +349,10 @@ def monday? `#{wday} == 1` end - alias month mon - def saturday? `#{wday} == 6` end def sec @@ -411,21 +398,17 @@ result.is_utc = true; return result; } end - alias getutc getgm - def gmtime %x{ self.is_utc = true; return self; } end - alias utc gmtime - def gmt? `self.is_utc === true` end def gmt_offset @@ -690,25 +673,14 @@ def to_i `parseInt(self.getTime() / 1000, 10)` end - alias to_s inspect - def tuesday? `#{wday} == 2` end - alias tv_sec to_i - - alias tv_usec usec - - alias utc? gmt? - - alias gmtoff gmt_offset - alias utc_offset gmt_offset - def wday `self.is_utc ? self.getUTCDay() : self.getDay()` end def wednesday? @@ -718,11 +690,11 @@ def year `self.is_utc ? self.getUTCFullYear() : self.getFullYear()` end def cweek_cyear - jan01 = Time.new(year, 1, 1) + jan01 = ::Time.new(year, 1, 1) jan01_wday = jan01.wday first_monday = 0 year = self.year if jan01_wday <= 4 && jan01_wday != 0 # Jan 01 is in the first week of the year @@ -735,19 +707,37 @@ week = ((yday + offset) / 7.00).ceil if week <= 0 # Get the last week of the previous year - return Time.new(self.year - 1, 12, 31).cweek_cyear + return ::Time.new(self.year - 1, 12, 31).cweek_cyear elsif week == 53 # Find out whether this is actually week 53 or already week 01 of the following year - dec31 = Time.new(self.year, 12, 31) + dec31 = ::Time.new(self.year, 12, 31) dec31_wday = dec31.wday if dec31_wday <= 3 && dec31_wday != 0 week = 1 year += 1 end end [week, year] end + + class << self + alias mktime local + alias utc gm + end + + alias ctime asctime + alias dst? isdst + alias getutc getgm + alias gmtoff gmt_offset + alias mday day + alias month mon + alias to_s inspect + alias tv_sec to_i + alias tv_usec usec + alias utc gmtime + alias utc? gmt? + alias utc_offset gmt_offset end