diff --git a/BeefLibs/corlib/src/TimeSpan.bf b/BeefLibs/corlib/src/TimeSpan.bf index c1fe23b5..fb92c0c4 100644 --- a/BeefLibs/corlib/src/TimeSpan.bf +++ b/BeefLibs/corlib/src/TimeSpan.bf @@ -1,3 +1,4 @@ +using System.Diagnostics; using System.Globalization; namespace System @@ -51,15 +52,10 @@ namespace System this = TimeToTicks(hours, minutes, seconds).Get(); } - /*public this(int hours, int minutes, int seconds) { - _ticks = TimeToTicks(hours, minutes, seconds); - }*/ - - //TODO: This fails too - /*public this(int days, int hours, int minutes, int seconds) + public this(int32 days, int32 hours, int32 minutes, int32 seconds) : this(days,hours,minutes,seconds,0) { - }*/ + } public this(int32 days, int32 hours, int32 minutes, int32 seconds, int32 milliseconds) { @@ -143,6 +139,42 @@ namespace System return totalSeconds * TicksPerSecond; } + private static Result Interval(double value, int32 scale) + { + if (value.IsNaN) + return .Err; + double tmp = value * scale; + double millis = tmp + (value >= 0 ? 0.5 : -0.5); + if ((millis > Int64.MaxValue / TicksPerMillisecond) || (millis < Int64.MinValue / TicksPerMillisecond)) + return .Err; + return TimeSpan((int64)millis * TicksPerMillisecond); + } + + public static Result FromDays(double value) + { + return Interval(value, MillisPerDay); + } + + public static Result FromHours(double value) + { + return Interval(value, MillisPerHour); + } + + public static Result FromMilliseconds(double value) + { + return Interval(value, 1); + } + + public static Result FromMinutes(double value) + { + return Interval(value, MillisPerMinute); + } + + public static Result FromSeconds(double value) + { + return Interval(value, MillisPerSecond); + } + public Result Negate() { if (Ticks==TimeSpan.MinValue.Ticks)