ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

Humanizer FluentDate 系列详解:OnDate.October 日期访问器与源码生成机制

Humanizer FluentDate 系列详解:OnDate.October 日期访问器与源码生成机制 开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载Humanizer 的 FluentDate 子命名空间提供了一组流式日期访问器fluent date accessors让开发者可以用OnDate.October.The23rd这样接近自然语言的写法直接得到今年 10 月 23 日的DateOnly值。本指南以OnDate.October类为样本完整梳理其全部公开 API31 个静态属性与 1 个静态方法、与DateTime版On.October的差异、以及它在仓库中由 T4 模板生成并经过反射测试验证的底层实现帮助你在自己的 .NET 6 项目中安全、规范地使用这套日期访问器。一、OnDate.October 是什么OnDate.October是 Humanizer 中OnDate根类下的一个嵌套类文档定义如下见 version-2.14.1 API 文档public class OnDate.October继承链为System.Object→OnDate.October。它的作用是为十月提供流式日期访问器Provides fluent date accessors for October所有成员返回的都是System.DateOnly且年份固定取自当前年份DateTime.Now.Year。由于DateOnly是 .NET 6 引入的类型整套OnDateAPI 只在NET6_0_OR_GREATER条件下编译源码中以#if NET6_0_OR_GREATER包裹见 OnDate.Days.cs。当前仓库的 Humanizer.csproj 目标框架为net11.0; net10.0; net8.0; net48; netstandard2.0因此只有在 net8.0 及更高版本的目标框架下才能使用本类。二、完整 API 成员清单OnDate.October由 31 个静态属性和 1 个静态方法组成覆盖 10 月的每一天。2.1 静态属性The1st The31st每个属性都是一个只读的static System.DateOnly语义均为今年 10 月的第 N 天命名采用英文序数词后缀st / nd / rd / th。完整对照如下属性类型含义The1stDateOnly今年 10 月 1 日The2ndDateOnly今年 10 月 2 日The3rdDateOnly今年 10 月 3 日The4thThe9thDateOnly今年 10 月 4 日9 日The10thThe19thDateOnly今年 10 月 10 日19 日The20thThe29thDateOnly今年 10 月 20 日29 日The30thDateOnly今年 10 月 30 日The31stDateOnly今年 10 月 31 日例如 OnDate.Days.cs 中的实际实现/// summary /// The 1st day of October of the current year /// /summary public static DateOnly The1st new(DateTime.Now.Year, 10, 1); /// summary /// The 10th day of October of the current year /// /summary public static DateOnly The10th new(DateTime.Now.Year, 10, 10);2.2 静态方法The(int dayNumber)除固定属性外还提供了一个参数化入口适用于序号在编译期不确定的场景例如根据用户输入取某一天public static System.DateOnly The(int dayNumber);参数dayNumber为System.Int32表示目标日期在 10 月中的天数返回值同样是System.DateOnly。源码实现为public static DateOnly The(int dayNumber) new(DateTime.Now.Year, 10, dayNumber);值得注意的是该实现直接透传dayNumber给DateOnly构造函数因此传入0、负数或超过 31 的值时会由DateOnly构造函数本身抛出ArgumentOutOfRangeException这与测试中对非法日期的预期一致见下文测试章节。三、实际使用示例在项目中使用时直接通过静态属性或方法取值即可无需实例化using Humanizer; // 得到今年 10 月 1 日DateOnly DateOnly nationalDay OnDate.October.The1st; // 得到今年 10 月 23 日DateOnly DateOnly meetupDay OnDate.October.The23rd; // 动态取第 N 天假设 n 来自业务输入如排期编号 int n 15; DateOnly dynamicDay OnDate.October.The(n);一个典型的应用场景是以自然语言书写日期规则的代码与手写new DateOnly(DateTime.Now.Year, 10, 23)相比OnDate.October.The23rd在阅读时几乎不需要解码同时仍是类型安全的强类型 API。测试 OnDateTests.cs 也印证了这一用法[Fact] public void OnJanuaryThe23rd() Assert.Equal(new(DateTime.Now.Year, 1, 23), OnDate.January.The23rd); [Fact] public void OnFebruaryThe() Assert.Equal(new(DateTime.Now.Year, 2, 11), OnDate.February.The(11));四、源码级原理T4 模板批量生成OnDate.October乃至整个OnDate的 12 个月份类并非手写而是由 T4 文本模板 OnDate.Days.tt 批量生成的。模板的核心逻辑简化示意const int leapYear 2012; for (var month 1; month 12; month) { var firstDayOfMonth new DateTime(leapYear, month, 1); var monthName firstDayOfMonth.ToString(MMMM); // 输出一个 public class 月份名 嵌套类 public class # monthName # { public static DateOnly The(int dayNumber) new(DateTime.Now.Year, # month #, dayNumber); for (var day 1; day DateTime.DaysInMonth(leapYear, month); day) { var ordinalDay day.Ordinalize(); // 生成 1st、2nd、3rd 等命名 public static DateOnly The# ordinalDay # new(DateTime.Now.Year, # month #, # day #); } } }这里有两个值得注意的实现细节以闰年 2012 为天数基准模板用DateTime.DaysInMonth(leapYear, month)2012 为闰年来确定每个月应生成多少天因此 2 月会生成The1stThe29th共 29 个属性而 10 月生成 31 个The1stThe31st借助Ordinalize()生成属性名属性命名中的序数后缀1st、2nd、3rd、4th…直接调用了 Humanizer 自身的整数序数化扩展number.Ordinalize()定义于 OrdinalizeExtensions.cs这本身就体现了Humanizer 用自己来生成自己的自举式设计。生成的.cs文件与模板的绑定关系在 Humanizer.csproj 中声明None UpdateFluentDate\OnDate.Days.tt GeneratorTextTemplatingFileGenerator LastGenOutputOnDate.Days.cs / Compile UpdateFluentDate\OnDate.Days.cs DesignTimeTrue AutoGenTrue DependentUponOnDate.Days.tt /所以如果你在源码里看到 OnDate.Days.cs 中public class October位于第 1752 行共 2336 行不必惊讶——它只是模板对 12 个月份循环展开后的产物October 恰好排在第 10 个月份按 112 顺序生成。五、测试如何保证 31 个属性全部正确OnDate.October的每个成员都由反射驱动的测试逐一验证。核心测试位于 GeneratedFluentDateTests.cs其中两个测试专门覆盖OnDateOnDateDayPropertiesCoverAllGeneratedDayAccessors遍历OnDate下所有月份嵌套类的公开静态属性解析The1stThe31st这类属性名中的天数断言其值等于new DateOnly(当前年份, 月份, 天数)见AssertMonthDayPropertiesGeneratedFluentDateTests.csOnDateTheMethodsCoverAllGeneratedMonthFactories对每个月份类断言存在签名为The(int)的静态方法并以1为参数验证返回该月 1 日见AssertMonthTheMethodsGeneratedFluentDateTests.cs。此外AssertMonthDayProperties还显式捕获TargetInvocationException并断言其内部异常为ArgumentOutOfRangeException即验证了非法天数属性如某月不存在的日期在被访问时会抛异常这一行为约定GeneratedFluentDateTests.cs。六、与 On.OctoberDateTime 版的对照Humanizer 同时提供了面向DateTime的On.October类其 API 文档位于 Humanizer.On.October.md。两者结构完全同构唯一区别是返回类型类型根类返回类型适用框架On.OctoberOnDateTime所有目标框架OnDate.OctoberOnDateDateOnly仅 .NET 6NET6_0_OR_GREATER对应实现可见 On.Days.cspublic static DateTime The(int dayNumber) new(DateTime.Now.Year, 10, dayNumber);选择建议如果只需要年-月-日而不关心时间部分如节假日、排期、生日优先使用OnDate.October返回的DateOnly语义更精确且避免无意义的零点时刻如果确实需要完整的DateTime值则使用On.October。七、使用注意事项年份以本地时钟为准所有成员都基于DateTime.Now.Year取当前年份跨年瞬间如 12 月 31 日 23:59:59 → 1 月 1 日 00:00:00取值会自然切换到新一年方法参数需合法The(int dayNumber)不校验参数范围非法值由DateOnly构造逻辑抛出ArgumentOutOfRangeException调用方应自行保证1 dayNumber 3110 月固定 31 天The31st始终存在这与 10 月恒为 31 天的日历事实一致而像 2 月这样的月份模板只生成到The29th框架门槛使用本类要求目标框架为 .NET 6 或更高仓库当前最低支持为 net8.0面向 .NET Framework 或 netstandard2.0 的项目请改用On.October的DateTime版本。综上OnDate.October是 Humanizer FluentDate 体系中按自然月份组织日期访问器的典型代表31 个属性 1 个方法覆盖 10 月全部日期背后由 T4 模板统一生成、由反射测试全量校验是理解整个OnDate/On系列 API 的最佳切入点。赞分享开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载相关推荐Humanizer 流式日期 API 全解OnDate.October 类的 31 天访问器与源码生成原理Humanizer 流式日期 API 全解OnDate.October 类的 31 天访问器与源码生成原理 本篇技术指南聚焦 Humanizer 开源库中 F开发工具Humanizer FluentDate 流式日期 API 详解On.September 与 9 月日期访问器的使用与生成原理Humanizer FluentDate 流式日期 API 详解On.September 与 9 月日期访问器的使用与生成原理 Humanizer 的 Flu开发工具Humanizer 流式日期 API 详解OnDate.October 十月日期访问器使用指南Humanizer 流式日期 API 详解OnDate.October 十月日期访问器使用指南 本篇技术指南聚焦 Humanizer 库中 OnDate.Oc开发工具上一篇制造业预测性维护机器学习如何让设备故障提前“说话”下一篇如何高效使用Win11Debloat免费优化Windows系统性能实用技巧指南创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表