|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | + |
| 4 | +namespace ExcelNumberFormat |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Similar to regular .NET DateTime, but also supports 0/1 1900 and 29/2 1900. |
| 8 | + /// </summary> |
| 9 | + internal class ExcelDateTime |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// The closest .NET DateTime to the specified excel date. |
| 13 | + /// </summary> |
| 14 | + public DateTime AdjustedDateTime { get; } |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Number of days to adjust by in post. |
| 18 | + /// </summary> |
| 19 | + public int AdjustDaysPost { get; } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Constructs a new ExcelDateTime from a numeric value. |
| 23 | + /// </summary> |
| 24 | + public ExcelDateTime(double numericDate, bool isDate1904) |
| 25 | + { |
| 26 | + if (isDate1904) |
| 27 | + { |
| 28 | + numericDate += 1462.0; |
| 29 | + AdjustedDateTime = new DateTime(DoubleDateToTicks(numericDate), DateTimeKind.Unspecified); |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + // internal dates before 30/12/1899 should add two days to get the real date |
| 34 | + // internal dates on 30/12 19899 should add two days, but subtract a day post to get the real date |
| 35 | + // internal dates before 28/2/1900 should add one day to get the real date |
| 36 | + // internal dates on 28/2 1900 should use the same date, but add a day post to get the real date |
| 37 | + |
| 38 | + var internalDateTime = new DateTime(DoubleDateToTicks(numericDate), DateTimeKind.Unspecified); |
| 39 | + if (internalDateTime < Excel1900ZeroethMinDate) |
| 40 | + { |
| 41 | + AdjustDaysPost = 0; |
| 42 | + AdjustedDateTime = internalDateTime.AddDays(2); |
| 43 | + } |
| 44 | + |
| 45 | + else if (internalDateTime < Excel1900ZeroethMaxDate) |
| 46 | + { |
| 47 | + AdjustDaysPost = -1; |
| 48 | + AdjustedDateTime = internalDateTime.AddDays(2); |
| 49 | + } |
| 50 | + |
| 51 | + else if (internalDateTime < Excel1900LeapMinDate) |
| 52 | + { |
| 53 | + AdjustDaysPost = 0; |
| 54 | + AdjustedDateTime = internalDateTime.AddDays(1); |
| 55 | + } |
| 56 | + |
| 57 | + else if (internalDateTime < Excel1900LeapMaxDate) |
| 58 | + { |
| 59 | + AdjustDaysPost = 1; |
| 60 | + AdjustedDateTime = internalDateTime; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + AdjustDaysPost = 0; |
| 65 | + AdjustedDateTime = internalDateTime; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static DateTime Excel1900LeapMinDate = new DateTime(1900, 2, 28); |
| 71 | + static DateTime Excel1900LeapMaxDate = new DateTime(1900, 3, 1); |
| 72 | + static DateTime Excel1900ZeroethMinDate = new DateTime(1899, 12, 30); |
| 73 | + static DateTime Excel1900ZeroethMaxDate = new DateTime(1899, 12, 31); |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Wraps a regular .NET datetime. |
| 77 | + /// </summary> |
| 78 | + /// <param name="value"></param> |
| 79 | + public ExcelDateTime(DateTime value) |
| 80 | + { |
| 81 | + AdjustedDateTime = value; |
| 82 | + AdjustDaysPost = 0; |
| 83 | + } |
| 84 | + |
| 85 | + public int Year => AdjustedDateTime.Year; |
| 86 | + |
| 87 | + public int Month => AdjustedDateTime.Month; |
| 88 | + |
| 89 | + public int Day => AdjustedDateTime.Day + AdjustDaysPost; |
| 90 | + |
| 91 | + public int Hour => AdjustedDateTime.Hour; |
| 92 | + |
| 93 | + public int Minute => AdjustedDateTime.Minute; |
| 94 | + |
| 95 | + public int Second => AdjustedDateTime.Second; |
| 96 | + |
| 97 | + public int Millisecond => AdjustedDateTime.Millisecond; |
| 98 | + |
| 99 | + public DayOfWeek DayOfWeek => AdjustedDateTime.DayOfWeek; |
| 100 | + |
| 101 | + public string ToString(string numberFormat, CultureInfo culture) |
| 102 | + { |
| 103 | + return AdjustedDateTime.ToString(numberFormat, culture); |
| 104 | + } |
| 105 | + |
| 106 | + public static bool TryConvert(object value, bool isDate1904, CultureInfo culture, out ExcelDateTime result) |
| 107 | + { |
| 108 | + if (value is double doubleValue) |
| 109 | + { |
| 110 | + result = new ExcelDateTime(doubleValue, isDate1904); |
| 111 | + return true; |
| 112 | + } |
| 113 | + if (value is int intValue) |
| 114 | + { |
| 115 | + result = new ExcelDateTime(intValue, isDate1904); |
| 116 | + return true; |
| 117 | + } |
| 118 | + if (value is short shortValue) |
| 119 | + { |
| 120 | + result = new ExcelDateTime(shortValue, isDate1904); |
| 121 | + return true; |
| 122 | + } |
| 123 | + else if (value is DateTime dateTimeValue) |
| 124 | + { |
| 125 | + result = new ExcelDateTime(dateTimeValue); |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + result = null; |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + // From DateTime class to enable OADate in PCL |
| 134 | + // Number of 100ns ticks per time unit |
| 135 | + private const long TicksPerMillisecond = 10000; |
| 136 | + private const long TicksPerSecond = TicksPerMillisecond * 1000; |
| 137 | + private const long TicksPerMinute = TicksPerSecond * 60; |
| 138 | + private const long TicksPerHour = TicksPerMinute * 60; |
| 139 | + private const long TicksPerDay = TicksPerHour * 24; |
| 140 | + |
| 141 | + private const int MillisPerSecond = 1000; |
| 142 | + private const int MillisPerMinute = MillisPerSecond * 60; |
| 143 | + private const int MillisPerHour = MillisPerMinute * 60; |
| 144 | + private const int MillisPerDay = MillisPerHour * 24; |
| 145 | + |
| 146 | + // Number of days in a non-leap year |
| 147 | + private const int DaysPerYear = 365; |
| 148 | + |
| 149 | + // Number of days in 4 years |
| 150 | + private const int DaysPer4Years = DaysPerYear * 4 + 1; |
| 151 | + |
| 152 | + // Number of days in 100 years |
| 153 | + private const int DaysPer100Years = DaysPer4Years * 25 - 1; |
| 154 | + |
| 155 | + // Number of days in 400 years |
| 156 | + private const int DaysPer400Years = DaysPer100Years * 4 + 1; |
| 157 | + |
| 158 | + // Number of days from 1/1/0001 to 12/30/1899 |
| 159 | + private const int DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367; |
| 160 | + |
| 161 | + private const long DoubleDateOffset = DaysTo1899 * TicksPerDay; |
| 162 | + |
| 163 | + internal static long DoubleDateToTicks(double value) |
| 164 | + { |
| 165 | + long millis = (long)(value * MillisPerDay + (value >= 0 ? 0.5 : -0.5)); |
| 166 | + |
| 167 | + // The interesting thing here is when you have a value like 12.5 it all positive 12 days and 12 hours from 01/01/1899 |
| 168 | + // However if you a value of -12.25 it is minus 12 days but still positive 6 hours, almost as though you meant -11.75 all negative |
| 169 | + // This line below fixes up the millis in the negative case |
| 170 | + if (millis < 0) |
| 171 | + { |
| 172 | + millis -= millis % MillisPerDay * 2; |
| 173 | + } |
| 174 | + |
| 175 | + millis += DoubleDateOffset / TicksPerMillisecond; |
| 176 | + return millis * TicksPerMillisecond; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments