Jelajahi Sumber

Use static methods for JS date functions instead of prototype patches.

From aszlig on github, part of issue #572.
Bruno Garcia 13 tahun lalu
induk
melakukan
b6195c1b02
4 mengubah file dengan 95 tambahan dan 4 penghapusan
  1. 1 4
      std/Date.hx
  2. 55 0
      std/js/_std/Date.hx
  3. 37 0
      std/js/_std/HxOverrides.hx
  4. 2 0
      std/js/_std/Std.hx

+ 1 - 4
std/Date.hx

@@ -103,7 +103,7 @@ extern class Date
 	static function fromString( s : String ) : Date;
 
 
-#if (js || flash)
+#if flash
 	private static function __init__() : Void untyped {
 		var d #if !swf_mark : Dynamic #end = Date;
 		d.now = function() {
@@ -165,9 +165,6 @@ extern class Date
 		#elseif flash
 		d.prototype[__unprotect__("__class__")] = d;
 		d[__unprotect__("__name__")] = ["Date"];
-		#elseif js
-		d.prototype.__class__ = __feature__('Type.resolveClass',$hxClasses['Date'] = d,d);
-		d.__name__ = ["Date"];
 		#end
 	}
 #end

+ 55 - 0
std/js/_std/Date.hx

@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2012, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api extern class Date {
+
+	function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void;
+	function getTime() : Float;
+	function getHours() : Int;
+	function getMinutes() : Int;
+	function getSeconds() : Int;
+	function getFullYear() : Int;
+	function getMonth() : Int;
+	function getDate() : Int;
+	function getDay() : Int;
+
+	inline function toString() : String {
+		return HxOverrides.Date_toString(this);
+	}
+
+	static inline function now() : Date {
+		return untyped __new__(Date);
+	}
+
+	static inline function fromTime( t : Float ) : Date {
+		var d : Date = untyped __new__(Date);
+		untyped d["setTime"]( t );
+		return d;
+	}
+
+	static inline function fromString( s : String ) : Date {
+		return HxOverrides.Date_fromString(s);
+	}
+}

+ 37 - 0
std/js/_std/HxOverrides.hx

@@ -29,6 +29,43 @@
  */
 class HxOverrides {
 
+	public static function Date_toString( date :Date ) : String {
+		var m = date.getMonth() + 1;
+		var d = date.getDate();
+		var h = date.getHours();
+		var mi = date.getMinutes();
+		var s = date.getSeconds();
+		return date.getFullYear()
+			+"-"+(if( m < 10 ) "0"+m else ""+m)
+			+"-"+(if( d < 10 ) "0"+d else ""+d)
+			+" "+(if( h < 10 ) "0"+h else ""+h)
+			+":"+(if( mi < 10 ) "0"+mi else ""+mi)
+			+":"+(if( s < 10 ) "0"+s else ""+s);
+	}
+
+	public static function Date_fromString( s : String ) : Date {
+		switch( s.length ) {
+		case 8: // hh:mm:ss
+			var k = s.split(":");
+			var d : Date = untyped __new__(Date);
+			untyped d["setTime"](0);
+			untyped d["setUTCHours"](k[0]);
+			untyped d["setUTCMinutes"](k[1]);
+			untyped d["setUTCSeconds"](k[2]);
+			return d;
+		case 10: // YYYY-MM-DD
+			var k = s.split("-");
+			return new Date(cast k[0],cast untyped k[1] - 1,cast k[2],0,0,0);
+		case 19: // YYYY-MM-DD hh:mm:ss
+			var k = s.split(" ");
+			var y = k[0].split("-");
+			var t = k[1].split(":");
+			return new Date(cast y[0],cast untyped y[1] - 1,cast y[2],cast t[0],cast t[1],cast t[2]);
+		default:
+			throw "Invalid date format : " + s;
+		}
+	}
+
 	public static function String_charCodeAt( s : String, index : Int ) : Null<Int> {
 		var x = (untyped s).charCodeAt(index);
 		if( x != x ) // fast isNaN

+ 2 - 0
std/js/_std/Std.hx

@@ -65,6 +65,8 @@ import js.Boot;
 		String.__name__ = ["String"];
 		Array.prototype.__class__ = __feature__("Type.resolveClass",$hxClasses["Array"] = Array,Array);
 		Array.__name__ = ["Array"];
+		Date.prototype.__class__ = __feature__("Type.resolveClass",$hxClasses["Date"] = Date,Date);
+		Date.__name__ = ["Date"];
 		var Int = __feature__("Type.resolveClass", $hxClasses["Int"] = { __name__ : ["Int"] }, { __name__ : ["Int"] });
 		var Dynamic = __feature__("Type.resolveClass", $hxClasses["Dynamic"] = { __name__ : ["Dynamic"] }, { __name__ : ["Dynamic"] });
 		var Float = __feature__("Type.resolveClass", $hxClasses["Float"] = __js__("Number"), __js__("Number"));