Selaa lähdekoodia

let's try it like this

Simon Krajewski 11 vuotta sitten
vanhempi
commit
4799c377b2

+ 12 - 7
std/python/_std/Date.hx

@@ -25,8 +25,7 @@
 package;
 
 import python.lib.datetime.DateTime;
-
-
+import python.lib.datetime.TimeDelta;
 
 @:coreApi class Date
 {
@@ -39,13 +38,12 @@ import python.lib.datetime.DateTime;
 	{
 		if (year < DateTime.min.year) year = DateTime.min.year;
 		if (day == 0) day = 1;
-		date = new DateTime(year, month+1, day, hour, min, sec);
+		date = new DateTime(year, month+1, day, hour, min, sec, 0, python.lib.datetime.Timezone.utc);
 	}
 
 	public inline function getTime() : Float
 	{
-			
-		return date.timestamp()*1000.0;
+		return datetimeTimestamp(date);
 	}
 
 	public inline function getHours() : Int
@@ -110,13 +108,20 @@ import python.lib.datetime.DateTime;
 	static public function fromTime( t : Float ) : Date
 	{
 		var d = new Date(1970, 0, 1, 0, 0, 0);
-		d.date = DateTime.fromtimestamp(t/1000.0);
+		d.date = DateTime.fromtimestamp(t/1000.0, python.lib.datetime.Timezone.utc);
 		return d;
 	}
 
 	static function UTC( year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Float
 	{
-		return new DateTime(year, month+1, day, hour, min, sec, 0, python.lib.datetime.Timezone.utc).timestamp()*1000.0;
+		var dt = new DateTime(year, month+1, day, hour, min, sec, 0, python.lib.datetime.Timezone.utc);
+		return datetimeTimestamp(dt);
+	}
+
+	static function datetimeTimestamp(dt:DateTime):Int {
+		var dt2 = new DateTime(1970, 1, 1, 0, 0, 0, 0, python.lib.datetime.Timezone.utc);
+		var timedelta = new TimeDelta(0, 1);
+		return untyped __python__("(dt - dt2) * 1000 / timedelta");
 	}
 
 	static public function fromString( s : String ) : Date

+ 3 - 3
std/python/lib/datetime/DateTime.hx

@@ -19,12 +19,12 @@ extern class DateTime {
 	public var microsecond : Int;
 	public var tzinfo : TzInfo;
 
-	
+
 
 	public static function today ():DateTime;
 	public static function now (?tzinfo:TzInfo):DateTime;
 	public static function utcnow ():DateTime;
-	public static function fromtimestamp (timestamp:Float):DateTime;
+	public static function fromtimestamp (timestamp:Float, tzInfo:TzInfo=null):DateTime;
 	public static function utcfromtimestamp (timestamp:Int):DateTime;
 	public static function fromordinal (ordinal:Int):DateTime;
 
@@ -38,7 +38,7 @@ extern class DateTime {
 	// python 3.3
 	public function timestamp ():Float;
 
-	static function __init__ ():Void 
+	static function __init__ ():Void
 	{
 		python.Macros.importFromAs("datetime", "datetime", "python.lib.datetime.DateTime");
 	}

+ 5 - 4
std/python/lib/datetime/TimeDelta.hx

@@ -7,14 +7,15 @@ extern class TimeDelta {
 	public static var max : TimeDelta;
 	public static var resolution : TimeDelta;
 
-	
+
 	var days : Int;
-	
+
 	var seconds : Int;
 	var microseconds : Int;
-	
 
-	static function __init__ ():Void 
+	public function new(days:Int = 0, seconds:Int = 0, microseconds:Int = 0, milliseconds:Int = 0, minutes:Int = 0, hours:Int = 0, weeks:Int = 0):Void;
+
+	static function __init__ ():Void
 	{
 		python.Macros.importFromAs("datetime", "timedelta", "python.lib.datetime.TimeDelta");
 	}

+ 1 - 1
tests/unit/unitstd/DateTools.unit.hx.disabled → tests/unit/unitstd/DateTools.unit.hx

@@ -8,6 +8,6 @@ var d2 = DateTools.delta(d, diff);
 d2.toString() == "2012-02-17 01:03:02";
 
 //UTC based timestamp generation
-#if (js || flash || php || cpp)
+#if (js || flash || php || cpp || python)
 DateTools.makeUtc(1982, 10, 10, 14, 2, 20) == 405784940000.;
 #end