“保存”日期有两种方法:分别保存年、月、日或从“0 点”开始保存总天数(或小时或分钟或秒或毫秒......在此处选择测量单位) . 例如DateTime
,.NET 使用 100 纳秒作为Tick
0001 年 1 月 1 日作为“0 点”。Unix 使用从 1970 年 1 月 1 日开始的秒数。显然 .NET 和 Unix 的方式在内存中更紧凑(保存单个值),如果您想添加/减去一个数量(只需添加/减去它),这非常有用。问题是将此内部数字转换为年/月/日或将年/月/日转换为此数字更为复杂。
关于如何将年/月/日转换为内部编号的简单示例:
public class MyDate
{
public int TotalDaysFrom00010101 { get; private set; }
private const int DaysIn400YearCycle = 365 * 400 + 97;
private const int DaysIn100YearCycleNotDivisibleBy400 = 365 * 100 + 24;
private const int DaysIn4YearCycle = 365 * 4 + 1;
private static readonly int[] DaysPerMonthNonLeap = new[]
{
31,
31 + 28,
31 + 28 + 31,
31 + 28 + 31 + 30,
31 + 28 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 // Useless
};
private static readonly int[] DaysPerMonthLeap = new[]
{
31,
31 + 29,
31 + 29 + 31,
31 + 29 + 31 + 30,
31 + 29 + 31 + 30 + 31,
31 + 29 + 31 + 30 + 31 + 30,
31 + 29 + 31 + 30 + 31 + 30 + 31,
31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 // Useless
};
public static bool IsLeap(int year)
{
if (year % 400 == 0) return true;
return (year % 4 == 0) && (year % 100 != 0);
}
public void SetDate(int year, int month, int day)
{
TotalDaysFrom00010101 = 0;
{
int year2 = year - 1;
// Full 400 year cycles
TotalDaysFrom00010101 += (year2 / 400) * DaysIn400YearCycle;
year2 %= 400;
// Remaining 100 year cycles (0...3)
if (year2 >= 100)
{
year2 -= 100;
TotalDaysFrom00010101 += DaysIn100YearCycleNotDivisibleBy400;
if (year2 >= 100)
{
year2 -= 100;
TotalDaysFrom00010101 += DaysIn100YearCycleNotDivisibleBy400;
if (year2 >= 100)
{
year2 -= 100;
TotalDaysFrom00010101 += DaysIn100YearCycleNotDivisibleBy400;
}
}
}
// Full 4 year cycles
TotalDaysFrom00010101 += (year2 / 4) * DaysIn4YearCycle;
year2 %= 4;
// Remaining non-leap years
TotalDaysFrom00010101 += year2 * 365;
}
// Days from the previous month
if (month > 1)
{
// -2 is because -1 is for the 1 January == 0 index, plus -1
// because we must add only the previous full months here.
// So if the date is 1 March 2016, we must add the days of
// January + February, so month 3 becomes index 1.
TotalDaysFrom00010101 += DaysPerMonthNonLeap[month - 2];
if (month > 2 && IsLeap(year))
{
TotalDaysFrom00010101 += 1;
}
}
// Days (note that the "instant 0" in this class is day 1, so
// we must add day - 1)
TotalDaysFrom00010101 += day - 1;
}
public void GetDate(out int year, out int month, out int day)
{
int days = TotalDaysFrom00010101;
// year
{
year = days / DaysIn400YearCycle * 400;
days %= DaysIn400YearCycle;
if (days >= DaysIn100YearCycleNotDivisibleBy400)
{
year += 100;
days -= DaysIn100YearCycleNotDivisibleBy400;
if (days >= DaysIn100YearCycleNotDivisibleBy400)
{
year += 100;
days -= DaysIn100YearCycleNotDivisibleBy400;
if (days >= DaysIn100YearCycleNotDivisibleBy400)
{
year += 100;
days -= DaysIn100YearCycleNotDivisibleBy400;
}
}
}
year += days / DaysIn4YearCycle * 4;
days %= DaysIn4YearCycle;
// Special case: 31 dec of a leap year
if (days != 1460)
{
year += days / 365;
days %= 365;
}
else
{
year += 3;
days = 365;
}
year++;
}
// month
{
bool isLeap = IsLeap(year);
int[] daysPerMonth = isLeap ? DaysPerMonthLeap : DaysPerMonthNonLeap;
for (month = 0; month < daysPerMonth.Length; month++)
{
if (daysPerMonth[month] > days)
{
if (month > 0)
{
days -= daysPerMonth[month - 1];
}
break;
}
}
month++;
}
// day
{
day = days;
day++;
}
}
public void AddDays(int days)
{
TotalDaysFrom00010101 += days;
}
}
这里的重点是我们知道有 400 年的“周期”,这些“周期”中的每一个都有365 * 400 + 97
天。减去这些“周期”后,还有更小的 100 年“周期”,每个周期都有365 * 100 + 24
天。然后我们有 4 年的“周期”,每一年都有365 * 4 + 3
天,再加上剩余的年(0...3),每一年有 365 天。
在添加了所有“完整前几年”的天数后,我们可以添加“完整前几个月”的天数。这里我们必须考虑这一年是闰年的可能性。
最后我们添加选定的日期。
如何写这个GetDate()
留作练习。
现在...如何检查结果是否正确?我们可以根据DateTime
实现编写一些单元测试......比如:
var date = default(DateTime);
var endDate = new DateTime(2017, 1, 1);
while (date < endDate)
{
int year = date.Year;
int month = date.Month;
int day = date.Day;
int totalDays = (int)(date - default(DateTime)).TotalDays;
var md = new MyDate();
md.SetDate(year, month, day);
if (totalDays != md.TotalDaysFrom00010101)
{
Console.WriteLine("{0:d}: {1} vs {2}", date, totalDays, md.TotalDaysFrom00010101);
}
int year2, month2, day2;
md.GetDate(out year2, out month2, out day2);
if (year != year2 || month != month2 || day != day2)
{
Console.WriteLine("{0:d}: {1:D4}-{2:D2}-{3:D2} vs {4:D4}-{5:D2}-{6:D2}", date, year, month, day, year2, month2, day2);
}
date = date.AddDays(1);
}
(我知道这不是单元测试......但展示了如何使用DateTime
来进行比较)
请注意,这是一个关于如何实现它的示例。我不会以这种方式实现它。struct
我会DateTime
用构造函数而不是SetDate()
. 但你的似乎是一个练习,我认为应该从小步骤进行:首先正确构建一些东西,然后使其“正式正确”。所以第一步是建立一个正确的GetDate()
/ SetDate()
,然后你可以正确地封装在一个更正式的正确数据结构中。这个小样本甚至缺少一些参数验证:你可以SetDate(-1, 13, 32)
:-)
GetDate()
建造起来相当复杂。比我想象的要多得多。终于明白怎么写了。请注意,最终它与 Microsoft 的实现非常相似(请参阅GetDatePart(int part)
.