using System;
using NUnit.Framework;
namespace VariantX.Utilities
{
[TestFixture]
public class Dates
{
public Dates(){}
[Test]
public void DateParseTest()
{
// Tests every combo with the slash as a seperator.
Assert.AreEqual(1, ParseStringToDate("1/5/04").Month);
Assert.AreEqual(5, ParseStringToDate("1/5/04").Day);
Assert.AreEqual(2004, ParseStringToDate("1/5/04").Year);
Assert.AreEqual(1, ParseStringToDate("1/5/2004").Month);
Assert.AreEqual(5, ParseStringToDate("1/5/2004").Day);
Assert.AreEqual(2004, ParseStringToDate("1/5/2004").Year);
Assert.AreEqual(1, ParseStringToDate("01/5/04").Month);
Assert.AreEqual(5, ParseStringToDate("01/5/04").Day);
Assert.AreEqual(2004, ParseStringToDate("01/5/04").Year);
Assert.AreEqual(1, ParseStringToDate("01/5/2004").Month);
Assert.AreEqual(5, ParseStringToDate("01/5/2004").Day);
Assert.AreEqual(2004, ParseStringToDate("01/5/2004").Year);
Assert.AreEqual(1, ParseStringToDate("1/05/04").Month);
Assert.AreEqual(5, ParseStringToDate("1/05/04").Day);
Assert.AreEqual(2004, ParseStringToDate("1/05/04").Year);
Assert.AreEqual(1, ParseStringToDate("1/05/2004").Month);
Assert.AreEqual(5, ParseStringToDate("1/05/2004").Day);
Assert.AreEqual(2004, ParseStringToDate("1/05/2004").Year);
Assert.AreEqual(1, ParseStringToDate("01/05/04").Month);
Assert.AreEqual(5, ParseStringToDate("01/05/04").Day);
Assert.AreEqual(2004, ParseStringToDate("01/05/04").Year);
Assert.AreEqual(1, ParseStringToDate("01/05/2004").Month);
Assert.AreEqual(5, ParseStringToDate("01/05/2004").Day);
Assert.AreEqual(2004, ParseStringToDate("01/05/2004").Year);
// Tests every combo with the dash as a seperator.
Assert.AreEqual(1, ParseStringToDate("1-5-04").Month);
Assert.AreEqual(5, ParseStringToDate("1-5-04").Day);
Assert.AreEqual(2004, ParseStringToDate("1-5-04").Year);
Assert.AreEqual(1, ParseStringToDate("1-5-2004").Month);
Assert.AreEqual(5, ParseStringToDate("1-5-2004").Day);
Assert.AreEqual(2004, ParseStringToDate("1-5-2004").Year);
Assert.AreEqual(1, ParseStringToDate("01-5-04").Month);
Assert.AreEqual(5, ParseStringToDate("01-5-04").Day);
Assert.AreEqual(2004, ParseStringToDate("01-5-04").Year);
Assert.AreEqual(1, ParseStringToDate("01-5-2004").Month);
Assert.AreEqual(5, ParseStringToDate("01-5-2004").Day);
Assert.AreEqual(2004, ParseStringToDate("01-5-2004").Year);
Assert.AreEqual(1, ParseStringToDate("1-05-04").Month);
Assert.AreEqual(5, ParseStringToDate("1-05-04").Day);
Assert.AreEqual(2004, ParseStringToDate("1-05-04").Year);
Assert.AreEqual(1, ParseStringToDate("1-05-2004").Month);
Assert.AreEqual(5, ParseStringToDate("1-05-2004").Day);
Assert.AreEqual(2004, ParseStringToDate("1-05-2004").Year);
Assert.AreEqual(1, ParseStringToDate("01-05-04").Month);
Assert.AreEqual(5, ParseStringToDate("01-05-04").Day);
Assert.AreEqual(2004, ParseStringToDate("01-05-04").Year);
Assert.AreEqual(1, ParseStringToDate("01-05-2004").Month);
Assert.AreEqual(5, ParseStringToDate("01-05-2004").Day);
Assert.AreEqual(2004, ParseStringToDate("01-05-2004").Year);
// Tests every combo with the period as a seperator.
Assert.AreEqual(1, ParseStringToDate("1.5.04").Month);
Assert.AreEqual(5, ParseStringToDate("1.5.04").Day);
Assert.AreEqual(2004, ParseStringToDate("1.5.04").Year);
Assert.AreEqual(1, ParseStringToDate("1.5.2004").Month);
Assert.AreEqual(5, ParseStringToDate("1.5.2004").Day);
Assert.AreEqual(2004, ParseStringToDate("1.5.2004").Year);
Assert.AreEqual(1, ParseStringToDate("01.5.04").Month);
Assert.AreEqual(5, ParseStringToDate("01.5.04").Day);
Assert.AreEqual(2004, ParseStringToDate("01.5.04").Year);
Assert.AreEqual(1, ParseStringToDate("01.5.2004").Month);
Assert.AreEqual(5, ParseStringToDate("01.5.2004").Day);
Assert.AreEqual(2004, ParseStringToDate("01.5.2004").Year);
Assert.AreEqual(1, ParseStringToDate("1.05.04").Month);
Assert.AreEqual(5, ParseStringToDate("1.05.04").Day);
Assert.AreEqual(2004, ParseStringToDate("1.05.04").Year);
Assert.AreEqual(1, ParseStringToDate("1.05.2004").Month);
Assert.AreEqual(5, ParseStringToDate("1.05.2004").Day);
Assert.AreEqual(2004, ParseStringToDate("1.05.2004").Year);
Assert.AreEqual(1, ParseStringToDate("01.05.04").Month);
Assert.AreEqual(5, ParseStringToDate("01.05.04").Day);
Assert.AreEqual(2004, ParseStringToDate("01.05.04").Year);
Assert.AreEqual(1, ParseStringToDate("01.05.2004").Month);
Assert.AreEqual(5, ParseStringToDate("01.05.2004").Day);
Assert.AreEqual(2004, ParseStringToDate("01.05.2004").Year);
// Test with spaces as the seperator
Assert.AreEqual(1, ParseStringToDate("1 5 04").Month);
Assert.AreEqual(5, ParseStringToDate("1 5 04").Day);
Assert.AreEqual(2004, ParseStringToDate("1 5 04").Year);
Assert.AreEqual(1, ParseStringToDate("1 5 2004").Month);
Assert.AreEqual(5, ParseStringToDate("1 5 2004").Day);
Assert.AreEqual(2004, ParseStringToDate("1 5 2004").Year);
Assert.AreEqual(1, ParseStringToDate("01 5 04").Month);
Assert.AreEqual(5, ParseStringToDate("01 5 04").Day);
Assert.AreEqual(2004, ParseStringToDate("01 5 04").Year);
Assert.AreEqual(1, ParseStringToDate("01 5 2004").Month);
Assert.AreEqual(5, ParseStringToDate("01 5 2004").Day);
Assert.AreEqual(2004, ParseStringToDate("01 5 2004").Year);
Assert.AreEqual(1, ParseStringToDate("1 05 04").Month);
Assert.AreEqual(5, ParseStringToDate("1 05 04").Day);
Assert.AreEqual(2004, ParseStringToDate("1 05 04").Year);
Assert.AreEqual(1, ParseStringToDate("1 05 2004").Month);
Assert.AreEqual(5, ParseStringToDate("1 05 2004").Day);
Assert.AreEqual(2004, ParseStringToDate("1 05 2004").Year);
Assert.AreEqual(1, ParseStringToDate("01 05 04").Month);
Assert.AreEqual(5, ParseStringToDate("01 05 04").Day);
Assert.AreEqual(2004, ParseStringToDate("01 05 04").Year);
Assert.AreEqual(1, ParseStringToDate("01 05 2004").Month);
Assert.AreEqual(5, ParseStringToDate("01 05 2004").Day);
Assert.AreEqual(2004, ParseStringToDate("01 05 2004").Year);
// Test with only numbers. Note, that only 6 and 8 digits are
// supported. Any other, and the date could be ambiguous.
Assert.AreEqual(1, ParseStringToDate("010504").Month);
Assert.AreEqual(5, ParseStringToDate("010504").Day);
Assert.AreEqual(2004, ParseStringToDate("010504").Year);
Assert.AreEqual(1, ParseStringToDate("01052004").Month);
Assert.AreEqual(5, ParseStringToDate("01052004").Day);
Assert.AreEqual(2004, ParseStringToDate("01052004").Year);
}
public DateTime ParseStringToDate(string DateString)
{
DateTime dtReturn = new DateTime();
bool bSeperators = false;
int iTemp;
int iMonth;
int iDay;
int iYear;
//First check that it is at least 6 characters or more.
if(!(DateString.Length > 5))
throw new Exception("Date string not in correct format.");
// Next, see if the framework can parse it.
try
{
dtReturn = DateTime.Parse(DateString);
return dtReturn;
}
catch
{}
//Check to see if it has any seperators. If not, it should parse to a int.
try
{
iTemp = int.Parse(DateString);
bSeperators = false;
}
catch
{
bSeperators = true;
}
if(!bSeperators)
{
if(DateString.Length == 6)
{
iMonth = int.Parse(DateString.Substring(0,2));
iDay = int.Parse(DateString.Substring(2,2));
iYear = int.Parse(DateString.Substring(4,2));
iTemp = DateTime.Now.Year;
iTemp = iTemp / 100;
iTemp = iTemp * 100;
iYear += iTemp;
try
{
return new DateTime(iYear, iMonth, iDay);
}
catch
{
throw new Exception("Date string not in correct format.");
}
}
else
{
if(DateString.Length == 8)
{
iMonth = int.Parse(DateString.Substring(0,2));
iDay = int.Parse(DateString.Substring(2,2));
iYear = int.Parse(DateString.Substring(4,4));
try
{
return new DateTime(iYear, iMonth, iDay);
}
catch
{
throw new Exception("Date string not in correct format.");
}
}
else
{
// Not a 6 or 8 digit number that was passed in. Any other
// combination would have ambiguity, therefor it is an error.
throw new Exception("Date string not in correct format.");
}
}
}
else
{
// Looks like it is seperated by characters the framework doesn't support.
// Next version will take this and parse it, but for now it is an error.
throw new Exception("Date string not in correct format.");
}
}
}
}
Saturday, May 30, 2009
Get the Unix Epoch time in C#
// Get the Unix Epoch time in C#
int epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
int epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
Set Date & Time
// Set Date & Time
public struct SystemTime
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Millisecond;
};
[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
public extern static void Win32GetSystemTime(ref SystemTime st);
[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
public extern static bool Win32SetSystemTime(ref SystemTime st);
// ....
public static void Test()
{
SystemTime newTime = new SystemTime();
newTime.Year = (ushort)2005;
newTime.Month = (ushort)12;
newTime.Day = (ushort)2;
newTime.Hour = (ushort)12; //UTC time (if you are in UTC+2 zone then you'll put here: time - 2h)
newTime.Minute = (ushort)42;
newTime.Second = (ushort)11;
Win32SetSystemTime(ref newTime);
}
public struct SystemTime
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Millisecond;
};
[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
public extern static void Win32GetSystemTime(ref SystemTime st);
[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
public extern static bool Win32SetSystemTime(ref SystemTime st);
// ....
public static void Test()
{
SystemTime newTime = new SystemTime();
newTime.Year = (ushort)2005;
newTime.Month = (ushort)12;
newTime.Day = (ushort)2;
newTime.Hour = (ushort)12; //UTC time (if you are in UTC+2 zone then you'll put here: time - 2h)
newTime.Minute = (ushort)42;
newTime.Second = (ushort)11;
Win32SetSystemTime(ref newTime);
}
Display DateTime up to milliseconds
// Display DateTime up to milliseconds
string myTime = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff");
Console.WriteLine(myTime);
string myTime = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff");
Console.WriteLine(myTime);
DateTime to double and vice vers
// DateTime to double and vice versa
public static double ToDouble(DateTime what)
{
return BitConverter.ToDouble(BitConverter.GetBytes(what.Ticks), 0);
}
public static DateTime ToDateTime(double what)
{
return new DateTime(BitConverter.ToInt64(BitConverter.GetBytes(what), 0));
}
public static double ToDouble(DateTime what)
{
return BitConverter.ToDouble(BitConverter.GetBytes(what.Ticks), 0);
}
public static DateTime ToDateTime(double what)
{
return new DateTime(BitConverter.ToInt64(BitConverter.GetBytes(what), 0));
}
Subscribe to:
Posts (Atom)