DateTime Comparison Precision - c#

I'm doing DateTime comparison but I don't want to do comparison at second, millisecond and ticks level. What's the most elegant way?
If I simply compare the DateTime, then they are seldom equal due to ticks differences.

What about using a timespan.
if (Math.Truncate((A - B).TotalMinutes) == 0)
{
//There is less than one minute between them
}
Probably not the most elegant way, but it allows for cases which are one second apart and yet have different days/hours/minutes parts such as going over midnight.
Edit: it occured to me that the truncate is unecessary...
if (Math.Abs((A - B).TotalMinutes) < 1)
{
//There is less than one minute between them
}
Personally I think this is more elegant...

One approach could be to create two new DateTimes from your values you want to compare, but ignore anything from the seconds on down and then compare those:
DateTime compare1 = new DateTime(year1, month1, day1, hour1, minute1, 0);
DateTime compare2 = new DateTime(year2, month2, day2, hour2, minute2, 0);
int result = DateTime.Compare(compare1, compare2);
I'd be the first to admit it's not elegant, but it solves the problem.

Using a TimeSpan you get all the granularity you want :
DateTime dt1, dt2;
double d = (dt2 - dt1).TotalDays;
double h = (dt2 - dt1).TotalHours;
double m = (dt2 - dt1).TotalMinutes;
double s = (dt2 - dt1).TotalSeconds;
double ms = (dt2 - dt1).TotalMilliseconds;
double ticks = (dt2 - dt1).Ticks;

public class DateTimeComparer : Comparer<DateTime>
{
private Prescision _Prescision;
public enum Prescision : sbyte
{
Millisecons,
Seconds,
Minutes,
Hour,
Day,
Month,
Year,
Ticks
}
Func<DateTime, DateTime>[] actions = new Func<DateTime, DateTime>[]
{
(x) => { return x.AddMilliseconds(-x.Millisecond);},
(x) => { return x.AddSeconds(-x.Second);},
(x) => { return x.AddMinutes(-x.Minute);},
(x) => { return x.AddHours(-x.Hour);},
(x) => { return x.AddDays(-x.Day);},
(x) => { return x.AddMonths(-x.Month);},
};
public DateTimeComparer(Prescision prescision = Prescision.Ticks)
{
_Prescision = prescision;
}
public override int Compare(DateTime x, DateTime y)
{
if (_Prescision == Prescision.Ticks)
{
return x.CompareTo(y);
}
for (sbyte i = (sbyte)(_Prescision - 1); i >= 0; i--)
{
x = actions[i](x);
y = actions[i](y);
}
return x.CompareTo(y);
}
}
Usage example:
new DateTimeComparer(DateTimeComparer.Prescision.Day).Compare(Date1, Date2)

How about this ComparerClass?
public class DateTimeComparer : Comparer<DateTime>
{
private string _Format;
public DateTimeComparer(string format)
{
_Format = format;
}
public override int Compare(DateTime x, DateTime y)
{
if(x.ToString(_Format) == y.ToString(_Format))
return 0;
return x.CompareTo(y);
}
}
This can be used by
List.Sort(new DateTimeComparer("hh:mm"));

You can convert them to String format and compare the string with each other.
This also gives freedom to choose your comparison parameters, like only the time without the date, etc.
if (String.Format("{0:ddMMyyyyHHmmss}", date1) == String.Format("{0:ddMMyyyyHHmmss}", date2))
{
// success
}

I've written this to help myself:
internal class ImpreciseCompareDate : IComparer<DateTime>
{
private readonly double _Tolerance;
public ImpreciseCompareDate(double MillisecondsTolerance)
{
_Tolerance = MillisecondsTolerance;
}
public int Compare(DateTime x, DateTime y)
{
return Math.Abs((x - y).TotalMilliseconds) < _Tolerance ? 0 : x.CompareTo(y);
}
}
Tolerance can be set to (10d/3d) to account for SQL servers 1/300th of a ms. If tolerance is exceeded, delegate to default comparer.

Another way is to convert first by processing on ticks level with a simple (non-rounding) calculation:
var now = DateTime.UtcNow;
// 636340541021531973, 2017-06-26T06:08:22.1531973Z
var millisecondsPrecision = new DateTime(now.Ticks / 10000 * 10000, now.Kind);
// 636340541021530000, 2017-06-26T06:08:22.1530000Z
var secondsPrecision = new DateTime(now.Ticks / 10000000 * 10000000, now.Kind);
// 636340541020000000, 2017-06-26T06:08:22.0000000Z
var minutePrecision = new DateTime(now.Ticks / (10000000*60) * (10000000*60), now.Kind);
// 636340541000000000, 2017-06-26T06:08:00.0000000Z

#ALZ's solution looks nice but it's too complicated and has a bug.
So I decided to combine it with #ChrisF's solution.
public class DateTimeComparer : Comparer<DateTime>
{
public enum Precision
{
Years = 0,
Months,
Days,
Hours,
Minutes,
Seconds,
Millisecons,
Ticks
}
private Precision _precision;
public DateTimeComparer(Precision precision = Precision.Ticks)
{
_precision = precision;
}
public override int Compare(DateTime x, DateTime y)
{
if (_precision == Precision.Ticks)
{
return x.CompareTo(y);
}
var xx = AssembleValue(x, _precision);
var yy = AssembleValue(y, _precision);
return xx.CompareTo(yy);
}
private static DateTime AssembleValue(DateTime input, Precision precision)
{
var p = (int)precision;
var i = 1;
return new DateTime(input.Year,
p >= i++ ? input.Month : 1,
p >= i++ ? input.Day : 1,
p >= i++ ? input.Hour : 0,
p >= i++ ? input.Minute : 0,
p >= i++ ? input.Second : 0,
p >= i++ ? input.Millisecond : 0);
}
}

Very simple solution from my own code:
TimeSpan timeDifference = presentLastSavedDate.Subtract(previousLastSavedDate);
if (timeDifference.Seconds > 0)
{
return Content(HttpStatusCode.Conflict, ALREADY_CHANGED_MSG);
}

I have create a very fast compare functions to compare DateTime with different Precission. All are arithmetical calculations and no new object are created.
public enum DateTimeComparePrecision : long
{
Millisecond = TimeSpan.TicksPerMillisecond,
Second = TimeSpan.TicksPerSecond,
Minute = TimeSpan.TicksPerMinute,
Hour = TimeSpan.TicksPerHour,
Day = TimeSpan.TicksPerDay,
}
public static bool DatesAreEqual(DateTime d1, DateTime d2, DateTimeComparePrecision Precision)
{
return (d1.Ticks - (d1.Ticks % (long)Precision)) == (d2.Ticks - (d2.Ticks % (long)Precision));
}
public static int DatesCompare(DateTime d1, DateTime d2, DateTimeComparePrecision Precision)
{
long Day1 = (d1.Ticks - (d1.Ticks % (long)Precision));
long Day2 = (d2.Ticks - (d2.Ticks % (long)Precision));
if (Day2 > Day1)
return 1;
if (Day2 < Day1)
return -1;
return 0;
}
How Ticks Transformed for the compare
DateTime NowIs = DateTime.UtcNow;
Console.WriteLine($"{NowIs:dd MM yyyy HH:mm:ss.fffffff}");
DateTime d1 = new DateTime((NowIs.Ticks - (NowIs.Ticks % TimeSpan.TicksPerMillisecond)));
Console.WriteLine($"{d1:dd MM yyyy HH:mm:ss.fffffff}");
d1 = new DateTime((NowIs.Ticks - (NowIs.Ticks % TimeSpan.TicksPerSecond)));
Console.WriteLine($"{d1:dd MM yyyy HH:mm:ss.fffffff}");
d1 = new DateTime((NowIs.Ticks - (NowIs.Ticks % TimeSpan.TicksPerMinute)));
Console.WriteLine($"{d1:dd MM yyyy HH:mm:ss.fffffff}");
d1 = new DateTime((NowIs.Ticks - (NowIs.Ticks % TimeSpan.TicksPerHour)));
Console.WriteLine($"{d1:dd MM yyyy HH:mm:ss.fffffff}");
d1 = new DateTime((NowIs.Ticks - (NowIs.Ticks % TimeSpan.TicksPerDay)));
Console.WriteLine($"{d1:dd MM yyyy HH:mm:ss.fffffff}");
output
01 03 2022 12:51:26.7237323
01 03 2022 12:51:26.7230000
01 03 2022 12:51:26.0000000
01 03 2022 12:51:00.0000000
01 03 2022 12:00:00.0000000
01 03 2022 00:00:00.0000000

Related

Calculate datetime difference in C#

Hi I was solving a problem to calculate some library fine based on difference in return date and due date in C#. Now there are some constraints to the problem like
if the return year is changed i.e. if the return year is greater than the due date calendar year then fine is 10000. e.g. due date "31/12/2015" and return date "01/01/2016" then also fine is 10000.
if the return month is changed then fine is 500 * number of months late.
if the return day is changed then fine is 15 * number of days late.
else fine is 0.
Now i wrote the function below:
static int CalcFine (int[] returnedOn, int[] dueOn) {
int returnD = returnedOn[0];
int returnM = returnedOn[1];
int returnY = returnedOn[2];
int dueD = dueOn[0];
int dueM = dueOn[1];
int dueY = dueOn[2];
if (returnY > dueY) {
return 10000;
} else if (returnY < dueY) {
return 0;
} else {
if (returnM > dueM) {
return (returnM - dueM) * 500;
} else if (returnM < dueM) {
return 0;
} else {
if (returnD > dueD) {
return (returnD - dueD) * 15;
} else {
return 0;
}
}
}
}
I read about the DateTime class in C# that has pretty neat functions that return the difference in two dates as total days, total minutes, etc. But given the constraint of Fine being different based on year, month and days, I am not sure if there is any other inbuilt function to solve the above problem. In short I am trying to find if there is another simple way to solve the above problem without using so many if-else's.
You can get the difference in days, hours or minutes.
DateTime fromdate = new DateTime(2012,1,1);
DateTime todate = DateTime.Now;
TimeSpan diff = todate - fromdate;
int differenceInDays = diff.Days;
If you want to try differently for your validations and business rules. Follow the below code
public double GetFineAmount(DateTime DueDate)
{
DateTime dt = DateTime.Now;
int yeardiff, monthdiff, daydiff;
yeardiff = dt.Year - DueDate.Year;
if (yeardiff > 0) return 10000;
monthdiff = dt.Month - DueDate.Month;
if (monthdiff > 0) return 500 * monthdiff;
daydiff = dt.Day - DueDate.Day;
if (daydiff > 0) return 15 * daydiff;
return 0;
}
Editted again.. changed string pattern. I guess I need some sleep...
static int CalcFine (string returnedOn, string dueOn)
{
DateTime returnedDate = DateTime.ParseExact(
returnedOn, "d M yyyy", CultureInfo.InvariantCulture);
DateTime dueDate = DateTime.ParseExact(
dueOn, "d M yyyy", CultureInfo.InvariantCulture);
if (returnedDate < dueDate)
return 0;
if (returnedDate.Year > dueDate.Year)
return 10000;
if (returnedDate.Month > dueDate.Month)
return 500 * (returnedDate.Month - dueDate.Month);
if (returnedDate.Day > dueDate.Day)
return 15 * (returnedDate.Day - dueDate.Day);
else
return 0;
}
DateTime is a powerful tool. But you don't want to over-complicate this.
If you just find the difference between the two dates in days, the equation becomes a lot easier to manage versus trying to subtract dates.
static int CalcFine(DateTime returnedOn, DateTime dueOn)
{
TimeSpan dateDiff = (returnedOn - dueOn);
int TotalDays = dateDiff.Days;
if (TotalDays >= 365)
{
return 10000;
}
else if(TotalDays < 365 && TotalDays > 30 && TotalDays % 30 > 1)
{
return (500 * (TotalDays % 30));
}
else if(TotalDays < 30 && TotalDays > 0)
{
return 15 * TotalDays;
}
else
{
return 0;
}
}

Split period into month parts

I have the following datetimes:
Start = 15/12/2012 13:00:00
End = 16/02/2013 14:00:00
How can I split that in 3 parts for each month?
- 15-12-2012 13:00:00 -> 01-01-2013 00:00:00
- 01-01-2013 00:00:00 -> 01-02-2013 00:00:00
- 01-02-2013 00:00:00 -> 16-02-2013 14:00:00
The total timespan must remain the same.
Can this easily be done with LINQ?
sure, try this (with little helper class included)
Process:
var Start = DateTime.Parse("15 Dec 2012 13:00:00");
var End = DateTime.Parse("16 Feb 2013 14:00:00");
var runningDate = Start;
while (runningDate < End)
{
var nextMonthSeed = runningDate.AddMonths(1);
var to = DateHelper.Min(new DateTime(nextMonthSeed.Year, nextMonthSeed.Month, 1), End);
Console.WriteLine("{0} -> {1}", runningDate.ToString("dd-MM-yyyy HH:mm:ss"), to.ToString("dd-MM-yyyy HH:mm:ss"));
runningDate = to;
}
Helper class:
public static class DateHelper
{
public static DateTime Min(DateTime date1, DateTime date2)
{
return (date1 < date2 ? date1 : date2);
}
}
You could try something like these extension methods:
public static class SomeExtensions {
public static IEnumerable<Tuple<DateTime, DateTime>> GetIntervals(
this DateTime from,
DateTime to) {
var currentFrom = from;
var currentTo = from.AdvanceToStartOfNextMonth();
while (currentTo < to) {
yield return Tuple.Create(currentFrom, currentTo);
currentFrom = currentTo;
currentTo = currentFrom.AdvanceToStartOfNextMonth();
}
yield return Tuple.Create(currentFrom, to);
}
public static DateTime AdvanceToStartOfNextMonth(this DateTime #this) {
var newMonth = #this.Month + 1;
var newYear = #this.Year;
if (newMonth == 13) {
newMonth = 1;
newYear++;
}
return new DateTime(newYear, newMonth, 1);
}
}
and then use them like so:
public class Etc {
public static void Foo() {
DateTime start = ...
DateTime stop = ....
Tuple<DateTime, DateTime>[] intervals = start.GetIntervals(stop).ToArray();
// or simply
foreach (var interval in start.GetIntervals(stop))
Console.WriteLine(interval);
}
}
EDIT
And here's a little test I just tried out (and it looks alright, I think):
class Program {
static void Main(string[] args) {
DateTime start = DateTime.Now.Subtract(TimeSpan.FromDays(170));
DateTime stop = DateTime.Now;
foreach (var interval in start.GetIntervals(stop))
Console.WriteLine(interval);
Console.ReadKey(intercept: true);
}
}
and that produced these results (in a console app):
END OF EDIT

Emulating Excel's YearFrac in C#

How can I get the same results of the Excel YearFrac function in my C# application?
Calculates the fraction of the year represented by the number of whole days between two dates (the start_date and the end_date). Use the YEARFRAC worksheet function to identify the proportion of a whole year's benefits or obligations to assign to a specific term.
Here is a good snippet.
The algorithm for the YearFrac function is in fact very complex. Maybe this article can provide you with more details.
You can use Excel's functionality directly to calculate YearFrac. Microsoft says you are not supposed to use it, but it works very well. If you need a 100% compatibility with Excel, this solution is hard to beat. You need to add to your project a reference to Microsoft.Office.Interop.Excel in order for this code to compile.
static void Main() {
var excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.WorksheetFunction wsf = excel.WorksheetFunction;
var start = new DateTime(1999, 11, 1);
var end = new DateTime(1999, 1, 11);
for (var basis = 0; basis != 5; basis++) {
Console.WriteLine(wsf.YearFrac(start, end, basis));
}
}
The signature of YEARFRAC is YEARFRAC(Date startDate, Date endDate, int convention). The method to calculate the YEARFRAC depends on the convention.
For convention = 2, YEARFRAC will calculate the YEARFRAC using ACT/360 method. An implementation of the ACT/360 can be found at svn.finmath.net, specifically DayCountConvention_ACT_360.java
For convention = 3, YEARFRAC will calculate the YEARFRAC using ACT/365 method. An implementation of the ACT/365 can be found at svn.finmath.net, specifically
DayCountConvention_ACT_365.java
For convention = 4, YEARFRAC will calculate the YEARFRAC using 30E/360 method. An implementation of the 30E/360 can be found at svn.finmath.net, specifically DayCountConvention_30E_360.java
For convention = 1, the documentation claims that YEARFRAC is calculate using ACT/ACT convention. However, there are a multiple versions of ACT/ACT and I believe the standard for many financial products is ACT/ACT ISDA. I found that YEARFRAC differs by a small amount from the ACT/ACT IDSA convention! An implementation of the ACT/ACT IDSA can be found at DayCountConvention_ACT_ACT_ISDA.java
I haven't checked the other act/act versions yet, but I would not rely on an emulation of YEARFRAC ACT/ACT, when it is not clear what kind of method they implement...
May I suggest:
public static double Yearfrac(DateTime startDate,DateTime endDate,DayCount daycount=DayCount.ActAct)
{
var nbDaysInPeriod = (double)(endDate - startDate).Days;
switch(daycount)
{
case (DayCount.Act360):
return nbDaysInPeriod / (double)360;
case (DayCount.Act365):
return nbDaysInPeriod / (double)365;
case (DayCount.ActAct):
return GetActAct(startDate,endDate);
case (DayCount.Days360):
var result = (endDate.Year - startDate.Year) * 360.0 + (endDate.Month - startDate.Month) * 30.0 + (Math.Min(endDate.Day, 30.0) - Math.Min(startDate.Day, 30.0));
return result/360;
default:
return nbDaysInPeriod / (double)365;
}
}
public static double GetActAct(DateTime startDate, DateTime endDate)
{
// Reproduce Excel Yearfrac as per http://www.dwheeler.com/yearfrac/excel-ooxml-yearfrac.pdf
var nbDaysInPeriod = (double)(endDate - startDate).Days;
if(startDate.Year==endDate.Year || (endDate.Year-1==startDate.Year&&(startDate.Month>endDate.Month||startDate.Month==endDate.Month&&(startDate.Day>=endDate.Day))))
{
var den = 365.0;
if (startDate.Year == endDate.Year && DateTime.IsLeapYear(startDate.Year))
{
den++;
}
else
{
if (endDate.Day == 29 && endDate.Month == 2)
{
den++;
}
else
{
if (DateTime.IsLeapYear(startDate.Year))
{
var feb = new DateTime(startDate.Year, 2, 29);
if (startDate<=feb && feb<=endDate) den++;
}
else
{
if (DateTime.IsLeapYear(endDate.Year))
{
var feb = new DateTime(endDate.Year, 2, 29);
if (startDate <= feb && feb <= endDate) den++;
}
}
}
}
}
else
{
var nbYears = endDate.Year - startDate.Year+1;
var den = nbYears * 365.0;
for (var i=0;i<nbYears;i++)
{
if (DateTime.IsLeapYear(startDate.Year + i)) den++;
}
den /= nbYears;
return nbDaysInPeriod / den;
}
return nbDaysInPeriod / 365.0;
}
If you want an exact YEARFRAC which measures to the day (so no need to specify any day-count convention) and gives a negative for periods in the past try this date extension...
public static double YearFrac(this DateTime startDate, DateTime endDate)
{
//---------------------------------------------------------------------------
startDate = DateTime.Parse(startDate.ToString("dd-MMM-yyyy"));
endDate = DateTime.Parse(endDate.ToString("dd-MMM-yyyy"));
//---------------------------------------------------------------------------
if (startDate == endDate)
return 0.0;
//---------------------------------------------------------------------------
double reverse = 1.0;
//---------------------------------------------------------------------------
if (startDate > endDate)
{
var ed = endDate;
endDate = startDate;
startDate = ed;
reverse = -1.0;
}
//---------------------------------------------------------------------------
int y1 = startDate.Year;
int y2 = endDate.Year;
int m1 = startDate.Month;
int m2 = endDate.Month;
int d1 = startDate.Day;
int d2 = endDate.Day;
int diy = startDate.DaysInYear();
int days = (endDate - startDate).Days;
//---------------------------------------------------------------------------
if (y1 == y2)
{
return (double)days / (double)diy * reverse;
}
//---------------------------------------------------------------------------
int wholeyears = y2 - y1 - 1;
DateTime lastDateA = ValidDate(y2 - 1, m2, d2);
DateTime lastDateB = ValidDate(y2 - 1, m1, d1);
int period1 = (endDate - lastDateA).Days;
int period2 = (endDate - lastDateB).Days;
//---------------------------------------------------------------------------
if (m1 > m2 || (m1 == m2 && d1 > d2))
{
return ((double)wholeyears + (double)period2 / (double)period1) * reverse;
}
//---------------------------------------------------------------------------
DateTime lastDateC = ValidDate(y2, m1, d1);
int period3 = (endDate - lastDateC).Days;
//---------------------------------------------------------------------------
return ((double)wholeyears + 1.0 + (double)period3 / (double)period1) * reverse;
//---------------------------------------------------------------------------
}
public static DateTime ValidDate(int y, int m, int d)
{
try
{
DateTime dt1 = new DateTime(y, m, d);
return dt1;
}
catch (Exception)
{
try
{
if (d == 29 && m == 2)
{
return new DateTime(y, 3, 1);
}
return new DateTime(y, m, d);
}
catch (Exception)
{
throw;
}
}
}
public static int DaysInYear(this DateTime date)
{
int year = date.Year;
DateTime d1 = DateTime.Parse("01-Jan-" + year.ToString());
DateTime d2 = DateTime.Parse("01-Jan-" + (year + 1).ToString());
int diy = (d2 - d1).Days;
return diy;
}

How to compare HH:MM in C#

Hi I have to compare HH:MM(hour and minutes). How can i do so?
var t1 = DateTime.Now.ToString("HH:mm");
var t2 = "20:03";
var res =result(t1, t2);
public int result(string t1, string t2)
{
int i = -1;
int hr1 = Convert.ToInt32(t1.Split(':')[0]);
int hr2 = Convert.ToInt32(t2.Split(':')[0]);
int min1 = Convert.ToInt32(t1.Split(':')[1]);
int min2 = Convert.ToInt32(t2.Split(':')[1]);
if (hr2 >= hr1)
{
if (min2 >= min1)
{
i = 1;
}
}
return i;
}
But it is not correct.. it is not taking care of all conditions.. how to make it perfect. Or is there any built in function that does this with thsi input only(I checked but no answer).
Thanks in advance
If you can assume the two strings are already in the right format, just use:
return t1.CompareTo(t2);
After all, they're lexicographically sorted due to the format used - no need to parse :)
With all the references to TimeSpan... Of course if you were using Noda Time you could use:
private static readonly LocalTimePattern TimePattern =
LocalTimePattern.CreateWithInvariantInfo("HH:mm");
...
public int CompareTimes(string t1, string t2)
{
// These will throw if the values are invalid. Use TryGetValue
// or the Success property to check first...
LocalTime time1 = TimePattern.Parse(t1).Value;
LocalTime time2 = TimePattern.Parse(t2).Value;
return time1.CompareTo(time2);
}
(You can use TimeSpan if you want, of course... but LocalTime represents the actual type of data you've got: a time of day, rather than an amount of time passing ;)
Use a TimeSpan:
TimeSpan s1 = TimeSpan.Parse(t1);
TimeSpan s2 = TimeSpan.Parse(t2);
return s1.CompareTo(s2);
If you're not sure the inputs are in the correct format, you can use TryParse instead.
If these represent clock times (i.e. hour is always less than 24), then DateTime.ParseExact is what you want.
Otherwise, TimeSpan.ParseExact
If you can guarantee that the provided time is always HH:mm you can use TimeSpan.ParseExact.
You can parse the time direct from the string. Beware the culture!
var time1 = DateTime.ParseExact("12:56", "hh:mm", CultureInfo.CurrentCulture);
var time2 = DateTime.ParseExact("11:21", "hh:mm", CultureInfo.CurrentCulture);
The other solutions are more elegant and simple and deal with culture issues and should be used in professional level code.
But to fix your code, you only need to compare the minute values if and only if the hour values are equal.
var t1 = DateTime.Now.ToString("HH:mm");
var t2 = "20:03";
var res =result(t1, t2);
public int result(string t1, string t2)
{
int i = -1;
int hr1 = Convert.ToInt32(t1.Split(':')[0]);
int hr2 = Convert.ToInt32(t2.Split(':')[0]);
int min1 = Convert.ToInt32(t1.Split(':')[1]);
int min2 = Convert.ToInt32(t2.Split(':')[1]);
if (hr2 > hr1)
i = 1;
else if (hr2 = hr1 && min2 >= min1)
i = 1;
return i;
}
This works
public int CompareTime(string t1, string t2)
{
int i = -1;
int hr1 = Convert.ToInt32(t1.Split(':')[0]);
int hr2 = Convert.ToInt32(t2.Split(':')[0]);
int min1 = Convert.ToInt32(t1.Split(':')[1]);
int min2 = Convert.ToInt32(t2.Split(':')[1]);
if (hr2 == hr1)
{
if (min2 >= min1)
{
i = 1;
}
}
if (hr2 > hr1)
{
i = 1;
}
return i;
}

How do DateTime.ToBinary() and DateTime.ToFileTime() differ?

Can anyone help explain the difference between DateTime.ToBinary() and DateTime.ToFileTime()? As far as I can tell they seem to always return the same value (when dealing with UTC times at least). The same applies to DateTime.FromBinary() and DateTime.FromFileTime().
I've tried using Reflector and I can see some differences, I just don't understand the relevance of the magic numbers:
public long ToBinary()
{
if (this.Kind != DateTimeKind.Local)
{
return (long) this.dateData;
}
TimeSpan utcOffset = TimeZoneInfo.Local.GetUtcOffset(this, TimeZoneInfoOptions.NoThrowOnInvalidTime);
long num2 = this.Ticks - utcOffset.Ticks;
if (num2 < 0L)
{
num2 = 0x4000000000000000L + num2;
}
return (num2 | -9223372036854775808L);
}
public long ToFileTime()
{
return this.ToUniversalTime().ToFileTimeUtc();
}
public long ToFileTimeUtc()
{
long num = ((this.InternalKind & 9223372036854775808L) != 0L) ? this.ToUniversalTime().InternalTicks : this.InternalTicks;
num -= 0x701ce1722770000L;
if (num < 0L)
{
throw new ArgumentOutOfRangeException(null, Environment.GetResourceString("ArgumentOutOfRange_FileTimeInvalid"));
}
return num;
}
public static DateTime FromFileTime(long fileTime)
{
return FromFileTimeUtc(fileTime).ToLocalTime();
}
public static DateTime FromFileTimeUtc(long fileTime)
{
if ((fileTime < 0L) || (fileTime > 0x24c85a5ed1c03fffL))
{
throw new ArgumentOutOfRangeException("fileTime", Environment.GetResourceString("ArgumentOutOfRange_FileTimeInvalid"));
}
return new DateTime(fileTime + 0x701ce1722770000L, DateTimeKind.Utc);
}
public static DateTime FromBinary(long dateData)
{
long num2;
if ((dateData & -9223372036854775808L) == 0L)
{
return FromBinaryRaw(dateData);
}
long ticks = dateData & 0x3fffffffffffffffL;
if (ticks > 0x3fffff36d5964000L)
{
ticks -= 0x4000000000000000L;
}
bool isAmbiguousLocalDst = false;
if (ticks < 0L)
{
num2 = TimeZoneInfo.Local.GetUtcOffset(MinValue, TimeZoneInfoOptions.NoThrowOnInvalidTime).Ticks;
}
else if (ticks > 0x2bca2875f4373fffL)
{
num2 = TimeZoneInfo.Local.GetUtcOffset(MaxValue, TimeZoneInfoOptions.NoThrowOnInvalidTime).Ticks;
}
else
{
DateTime time = new DateTime(ticks, DateTimeKind.Utc);
bool isDaylightSavings = false;
num2 = TimeZoneInfo.GetUtcOffsetFromUtc(time, TimeZoneInfo.Local, out isDaylightSavings, out isAmbiguousLocalDst).Ticks;
}
ticks += num2;
if (ticks < 0L)
{
ticks += 0xc92a69c000L;
}
if ((ticks < 0L) || (ticks > 0x2bca2875f4373fffL))
{
throw new ArgumentException(Environment.GetResourceString("Argument_DateTimeBadBinaryData"), "dateData");
}
return new DateTime(ticks, DateTimeKind.Local, isAmbiguousLocalDst);
}
ToBinary() and ToFileTimeUtc() do not return the same value. ToBinary provide a round-trip value, an Int64 that can preserve the properties of a DateTime. It uses the same time base, January 1st of the year 0. The value is always UTC. Bit 62 is set for the extreme corner case where a local time near the 1/1/00 would be negative when converted to UTC (paying attention to detail here :)). Bit 63 is set when the Kind is UTC. Convert the magic number to hex to see this.
ToFileTimeUtc() uses the same time base as Windows' FILETIME, January 1st of the year 1601. The magic number is the number of ticks for 12am, 1/1/1601.

Categories

Resources