mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-20 21:08:51 +00:00
Fixed TimeSpan and DateTime.
Added tests.
This commit is contained in:
parent
ccb8fae271
commit
92bc85c9bc
6 changed files with 164 additions and 68 deletions
|
|
@ -46,6 +46,8 @@ namespace Cosmos.Compiler.Tests.Bcl
|
|||
DoubleTest.Execute();
|
||||
MathTest.Execute();
|
||||
ConvertTests.Execute();
|
||||
DateTimeTests.Execute();
|
||||
TimeSpanTests.Execute();
|
||||
|
||||
//mDebugger.Send("Thread test start of 500 ms");
|
||||
//ThreadTest.Execute();
|
||||
|
|
|
|||
34
Tests/Cosmos.Compiler.Tests.Bcl/System/DateTimeTests.cs
Normal file
34
Tests/Cosmos.Compiler.Tests.Bcl/System/DateTimeTests.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
|
||||
using Cosmos.TestRunner;
|
||||
|
||||
namespace Cosmos.Compiler.Tests.Bcl.System
|
||||
{
|
||||
internal static class DateTimeTests
|
||||
{
|
||||
public static void Execute()
|
||||
{
|
||||
var someDateTime = new DateTime(2017, 4, 7, 16, 47, 32, 462);
|
||||
|
||||
Assert.IsTrue(someDateTime.Year == 2017, "DateTime.Year is not working");
|
||||
Assert.IsTrue(someDateTime.Month == 4, "DateTime.Month is not working");
|
||||
Assert.IsTrue(someDateTime.Day == 7, "DateTime.Day is not working");
|
||||
Assert.IsTrue(someDateTime.Hour == 16, "DateTime.Hour is not working");
|
||||
Assert.IsTrue(someDateTime.Minute == 47, "DateTime.Minute is not working");
|
||||
Assert.IsTrue(someDateTime.Second == 32, "DateTime.Second is not working");
|
||||
Assert.IsTrue(someDateTime.Millisecond == 462, "DateTime.Millisecond is not working");
|
||||
|
||||
Assert.IsTrue(DateTime.Now.Year >= 2018, "DateTime.Now is returning an year lower than 2018");
|
||||
|
||||
Assert.IsTrue(someDateTime.ToString() == "2017-04-07 16:47:32", "DateTime.ToString() is not working");
|
||||
|
||||
TimeSpan twoDaysTimeSpan = TimeSpan.FromDays(2);
|
||||
|
||||
someDateTime = someDateTime.Add(twoDaysTimeSpan);
|
||||
Assert.IsTrue(someDateTime.Day == 9, "DateTime.Add() is not working");
|
||||
|
||||
someDateTime = someDateTime.Subtract(twoDaysTimeSpan);
|
||||
Assert.IsTrue(someDateTime.Day == 7, "DateTime.Subtract() is not working");
|
||||
}
|
||||
}
|
||||
}
|
||||
55
Tests/Cosmos.Compiler.Tests.Bcl/System/TimeSpanTests.cs
Normal file
55
Tests/Cosmos.Compiler.Tests.Bcl/System/TimeSpanTests.cs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
using System;
|
||||
|
||||
using Cosmos.TestRunner;
|
||||
|
||||
namespace Cosmos.Compiler.Tests.Bcl.System
|
||||
{
|
||||
internal static class TimeSpanTests
|
||||
{
|
||||
public static void Execute()
|
||||
{
|
||||
TimeSpan twoDaysTimeSpan = TimeSpan.FromDays(2);
|
||||
|
||||
Assert.IsTrue(twoDaysTimeSpan.Ticks == 2.0 * 24.0 * 60.0 * 60.0 * 1000.0 * 10000.0, "TimeSpan.FromDays() is not working");
|
||||
|
||||
Assert.IsTrue(twoDaysTimeSpan.TotalDays == 2.0, "TimeSpan.TotalDays is not working");
|
||||
Assert.IsTrue(twoDaysTimeSpan.TotalHours == 2.0 * 24.0, "TimeSpan.TotalHours is not working");
|
||||
Assert.IsTrue(twoDaysTimeSpan.TotalMinutes == 2.0 * 24.0 * 60.0, "TimeSpan.TotalMinutes is not working");
|
||||
Assert.IsTrue(twoDaysTimeSpan.TotalSeconds == 2.0 * 24.0 * 60.0 * 60.0, "TimeSpan.TotalSeconds is not working");
|
||||
Assert.IsTrue(twoDaysTimeSpan.TotalMilliseconds == 2.0 * 24.0 * 60.0 * 60.0 * 1000.0, "TimeSpan.TotalMilliseconds is not working");
|
||||
|
||||
Assert.IsTrue(twoDaysTimeSpan.ToString() == "2.00:00:00", "TimeSpan.ToString() is not working");
|
||||
|
||||
Assert.IsTrue(TimeSpan.FromHours(523).Ticks == 523.0 * 60.0 * 60.0 * 1000.0 * 10000.0, "TimeSpan.FromHours() is not working");
|
||||
Assert.IsTrue(TimeSpan.FromMinutes(5638).Ticks == 5638.0 * 60.0 * 1000.0 * 10000.0, "TimeSpan.FromMinutes() is not working");
|
||||
Assert.IsTrue(TimeSpan.FromSeconds(36452).Ticks == 36452 * 1000.0 * 10000.0, "TimeSpan.FromSeconds() is not working");
|
||||
Assert.IsTrue(TimeSpan.FromMilliseconds(50394039).Ticks == 50394039.0 * 10000.0, "TimeSpan.FromMilliseconds() is not working");
|
||||
|
||||
TimeSpan someTimeSpan = new TimeSpan(5, 3, 47, 32, 543);
|
||||
|
||||
Assert.IsTrue(someTimeSpan.Days == 5, "TimeSpan.Days is not working");
|
||||
Assert.IsTrue(someTimeSpan.Hours == 3, "TimeSpan.Hours is not working");
|
||||
Assert.IsTrue(someTimeSpan.Minutes == 47, "TimeSpan.Minutes is not working");
|
||||
Assert.IsTrue(someTimeSpan.Seconds == 32, "TimeSpan.Seconds is not working");
|
||||
Assert.IsTrue(someTimeSpan.Milliseconds == 543, "TimeSpan.Milliseconds is not working");
|
||||
|
||||
Assert.IsTrue(someTimeSpan.ToString() == "5.03:47:32.5430000", "TimeSpan.ToString() is not working");
|
||||
|
||||
someTimeSpan = someTimeSpan.Add(twoDaysTimeSpan);
|
||||
Assert.IsTrue(someTimeSpan.Days == 7, "TimeSpan.Add() is not working");
|
||||
|
||||
someTimeSpan = someTimeSpan.Subtract(twoDaysTimeSpan);
|
||||
Assert.IsTrue(someTimeSpan.Days == 5, "TimeSpan.Subtract() is not working");
|
||||
|
||||
someTimeSpan = someTimeSpan.Negate();
|
||||
Assert.IsTrue(someTimeSpan.Days == -5, "TimeSpan.Negate() is not working");
|
||||
|
||||
someTimeSpan = someTimeSpan.Duration();
|
||||
Assert.IsTrue(someTimeSpan.Days == 5, "TimeSpan.Duration() is not working");
|
||||
|
||||
// there was a bug in Newobj, ctor args size needed to be lower or equal to the struct fields size
|
||||
// the test seems to be equal to the test above, but the test above uses Call and this uses Newobj
|
||||
Assert.IsTrue(new TimeSpan(5, 3, 47, 32, 543).ToString() == "5.03:47:32.5430000", "TimeSpan.ToString() is not working");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
using System;
|
||||
using Cosmos.Core;
|
||||
using IL2CPU.API;
|
||||
using IL2CPU.API.Attribs;
|
||||
|
||||
namespace Cosmos.Core_Plugs.System
|
||||
{
|
||||
[Plug(Target = typeof(DateTime))]
|
||||
public class DateTimeImpl
|
||||
{
|
||||
|
||||
public static DateTime get_Now()
|
||||
{
|
||||
int[] raw = GetRawDate();
|
||||
|
||||
return new DateTime(
|
||||
100 * BCDtoBIN(raw[10]) + BCDtoBIN(raw[9]), //YEAR
|
||||
BCDtoBIN(raw[8]), //MONTH
|
||||
BCDtoBIN(raw[7]), //DAY
|
||||
BCDtoBIN(raw[4]), //HOUR
|
||||
BCDtoBIN(raw[2]), //MINUTE
|
||||
BCDtoBIN(raw[0]) //SECOND
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: get timezone
|
||||
public static DateTime get_UtcNow() => get_Now();
|
||||
|
||||
private static int[] GetRawDate()
|
||||
{
|
||||
IOPort p70 = new IOPort(0x70);
|
||||
IOPort p71 = new IOPort(0x71);
|
||||
int[] raw = new int[0x0b];
|
||||
|
||||
p70.Byte = 0x0b;
|
||||
p71.Byte = 0x02; //24h format + BCD encoding
|
||||
|
||||
do
|
||||
p70.Byte = 0x0a;
|
||||
while ((p71.Byte & 0x80) == 0);
|
||||
|
||||
for (byte i = 0; i < 0x0a; i++)
|
||||
{
|
||||
p70.Byte = i;
|
||||
|
||||
raw[i] = p71.Word;
|
||||
}
|
||||
|
||||
p70.Byte = 0x32;
|
||||
|
||||
raw[0x0a] = p71.Word;
|
||||
|
||||
return raw;
|
||||
}
|
||||
|
||||
public static int ToString(ref decimal aThis)
|
||||
{
|
||||
throw new NotImplementedException("DateTime.ToString()");
|
||||
}
|
||||
|
||||
public static long GetSystemTimeAsFileTime() => get_Now().Ticks;
|
||||
|
||||
/// <summary>
|
||||
/// Converts BCD-encoded numbers to `normal` (binary-encoded) numbers
|
||||
/// </summary>
|
||||
private static int BCDtoBIN(int bcd) => ((bcd & 0xf0) >> 1) + ((bcd & 0xf0) >> 3) + (bcd & 0xf);
|
||||
}
|
||||
}
|
||||
37
source/Cosmos.System2_Plugs/System/DateTimeImpl.cs
Normal file
37
source/Cosmos.System2_Plugs/System/DateTimeImpl.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
|
||||
using IL2CPU.API.Attribs;
|
||||
|
||||
using Cosmos.HAL;
|
||||
|
||||
namespace Cosmos.System_Plugs.System
|
||||
{
|
||||
[Plug(typeof(DateTime))]
|
||||
public static class DateTimeImpl
|
||||
{
|
||||
public static DateTime Now =>
|
||||
new DateTime(
|
||||
RTC.Century * 100 + RTC.Year,
|
||||
RTC.Month,
|
||||
RTC.DayOfTheMonth,
|
||||
RTC.Hour,
|
||||
RTC.Minute,
|
||||
RTC.Second);
|
||||
|
||||
// TODO: get timezone
|
||||
public static DateTime UtcNow => Now;
|
||||
|
||||
public static long GetSystemTimeAsFileTime() => Now.Ticks;
|
||||
|
||||
public static string ToString(ref DateTime aThis)
|
||||
{
|
||||
// TODO: use current culture for string representation of DateTime
|
||||
return aThis.Year.ToString().PadLeft(4, '0') + "-" +
|
||||
aThis.Month.ToString().PadLeft(2, '0') + "-" +
|
||||
aThis.Day.ToString().PadLeft(2, '0') + " " +
|
||||
aThis.Hour.ToString().PadLeft(2, '0') + ":" +
|
||||
aThis.Minute.ToString().PadLeft(2, '0') + ":" +
|
||||
aThis.Second.ToString().PadLeft(2, '0');
|
||||
}
|
||||
}
|
||||
}
|
||||
36
source/Cosmos.System2_Plugs/System/TimeSpanImpl.cs
Normal file
36
source/Cosmos.System2_Plugs/System/TimeSpanImpl.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System;
|
||||
|
||||
using IL2CPU.API.Attribs;
|
||||
|
||||
namespace Cosmos.System_Plugs.System
|
||||
{
|
||||
[Plug(typeof(TimeSpan))]
|
||||
public static class TimeSpanImpl
|
||||
{
|
||||
public static string ToString(ref TimeSpan aThis)
|
||||
{
|
||||
string time = null;
|
||||
|
||||
if (aThis.Ticks < 0)
|
||||
{
|
||||
time += '-';
|
||||
}
|
||||
|
||||
if (aThis.Days != 0)
|
||||
{
|
||||
time += aThis.Days.ToString() + ".";
|
||||
}
|
||||
|
||||
time += aThis.Hours.ToString().PadLeft(2, '0') + ":" +
|
||||
aThis.Minutes.ToString().PadLeft(2, '0') + ":" +
|
||||
aThis.Seconds.ToString().PadLeft(2, '0');
|
||||
|
||||
if ((aThis.Ticks - (long)aThis.TotalSeconds * 1000 * 10000) != 0)
|
||||
{
|
||||
time += "." + (aThis.Ticks - (long)aThis.TotalSeconds * 1000 * 10000).ToString().PadLeft(7, '0');
|
||||
}
|
||||
|
||||
return time;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue