浏览代码

[cs] overload Date constructor to support constructing from native DateTime instead of replacing it after default construction, this makes code cleaner and prevents double allocation

Dan Korostelev 10 年之前
父节点
当前提交
5df9be8ae8
共有 1 个文件被更改,包括 12 次插入13 次删除
  1. 12 13
      std/cs/_std/Date.hx

+ 12 - 13
std/cs/_std/Date.hx

@@ -32,7 +32,12 @@ import haxe.Int64;
 	@:readOnly private static var epochTicks:Int64 = new DateTime(1970, 1, 1).Ticks;
 	private var date:DateTime;
 
-	public function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void
+	@:overload private function new(native:DateTime)
+	{
+		date = native;
+	}
+
+	@:overload public function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void
 	{
 		if (day <= 0) day = 1;
 		if (year <= 0) year = 1;
@@ -94,18 +99,14 @@ import haxe.Int64;
 			+":"+(if( s < 10 ) "0"+s else ""+s);
 	}
 
-	static public function now() : Date
+	static public inline function now() : Date
 	{
-		var d = new Date(0, 0, 0, 0, 0, 0);
-		d.date = DateTime.Now;
-		return d;
+		return new Date(DateTime.Now);
 	}
 
-	static public function fromTime( t : Float ) : Date
+	static public inline function fromTime( t : Float ) : Date
 	{
-		var d = new Date(0, 0, 0, 0, 0, 0);
-		d.date = new DateTime(cast(t * cast(TimeSpan.TicksPerMillisecond, Float), Int64) + epochTicks);
-		return d;
+		return new Date(new DateTime(cast(t * cast(TimeSpan.TicksPerMillisecond, Float), Int64) + epochTicks));
 	}
 
 	static public function fromString( s : String ) : Date
@@ -129,10 +130,8 @@ import haxe.Int64;
 		}
 	}
 
-	private static function fromNative( d : cs.system.DateTime ) : Date
+	private static inline function fromNative( d : cs.system.DateTime ) : Date
 	{
-		var date = new Date(0, 0, 0, 0, 0, 0);
-		date.date = d;
-		return date;
+		return new Date(d);
 	}
 }