多彩编程 多彩编程MZPH · CODE BLOG
ARTICLE DETAIL

文章详情

深耕前端与后端开发技术的一线实战笔记与踩坑复盘。

Humanizer 流式日期 API 深度解析:On.September 类参考与实现原理

Humanizer 流式日期 API 深度解析:On.September 类参考与实现原理 开发工具【免费下载链接】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点击查看免费下载On.September是 Humanizer 流式日期Fluent Date体系下专为 9 月提供的静态日期访问器类它通过The1stThe30th共 30 个静态属性与一个The(int)静态方法让开发者可以以当前年的某月某日的口语化方式构造DateTime值。本文以该类的官方 API 参考website/docs/api/Humanizer.On.September.md为主体结合仓库内的真实源码T4 模板与生成产物、测试用例与使用场景文档完整讲解它的成员构成、生成机制、运行时行为边界以及在整个On/OnDate类族中的定位。读完本文你将能够正确使用该类的全部成员并理解其当前年 午夜零点的取值语义与参数校验规则。一、类的定位与继承结构On.September声明在Humanizer命名空间下是顶层类On的嵌套类官方 API 参考将其签名描述为public class On.September其类注释为 Provides fluent date accessors for September为 September 提供流式日期访问器。从官方参考文档看继承链为System.Object → September需要特别注意的是虽然September拥有公开的参数化默认构造函数public September()用于Initializes a new instance of the September class但该类的所有 API 成员都是static的——The方法与全部TheNth属性均为静态成员。因此实际使用中无需实例化直接以On.September.The15th的形式访问即可。On顶层类本身也是一个纯容器其定义见 On.Days.csnamespace Humanizer; public class On { /// summary /// Provides fluent date accessors for January /// /summary public class January { ... } }从源码结构看On内部嵌套了 1 月至 12 月共 12 个与月份同名的类January、February……DecemberSeptember是其中第 9 个结构与其他月份完全对称。二、完整成员清单以下内容完整覆盖官方 API 参考页面是该类的全部公开成员。2.1 构造函数成员签名说明September()public September()初始化September类的新实例2.2 静态属性The1st The30th共 30 个每个属性返回当前年 9 月的对应日期返回类型均为System.DateTime属性签名语义The1stpublic static DateTime The1st { get; }当前年 9 月 1 日The2ndpublic static DateTime The2nd { get; }当前年 9 月 2 日The3rdpublic static DateTime The3rd { get; }当前年 9 月 3 日The4thpublic static DateTime The4th { get; }当前年 9 月 4 日The5thpublic static DateTime The5th { get; }当前年 9 月 5 日The6thpublic static DateTime The6th { get; }当前年 9 月 6 日The7thpublic static DateTime The7th { get; }当前年 9 月 7 日The8thpublic static DateTime The8th { get; }当前年 9 月 8 日The9thpublic static DateTime The9th { get; }当前年 9 月 9 日The10thpublic static DateTime The10th { get; }当前年 9 月 10 日The11thpublic static DateTime The11th { get; }当前年 9 月 11 日The12thpublic static DateTime The12th { get; }当前年 9 月 12 日The13thpublic static DateTime The13th { get; }当前年 9 月 13 日The14thpublic static DateTime The14th { get; }当前年 9 月 14 日The15thpublic static DateTime The15th { get; }当前年 9 月 15 日The16thpublic static DateTime The16th { get; }当前年 9 月 16 日The17thpublic static DateTime The17th { get; }当前年 9 月 17 日The18thpublic static DateTime The18th { get; }当前年 9 月 18 日The19thpublic static DateTime The19th { get; }当前年 9 月 19 日The20thpublic static DateTime The20th { get; }当前年 9 月 20 日The21stpublic static DateTime The21st { get; }当前年 9 月 21 日The22ndpublic static DateTime The22nd { get; }当前年 9 月 22 日The23rdpublic static DateTime The23rd { get; }当前年 9 月 23 日The24thpublic static DateTime The24th { get; }当前年 9 月 24 日The25thpublic static DateTime The25th { get; }当前年 9 月 25 日The26thpublic static DateTime The26th { get; }当前年 9 月 26 日The27thpublic static DateTime The27th { get; }当前年 9 月 27 日The28thpublic static DateTime The28th { get; }当前年 9 月 28 日The29thpublic static DateTime The29th { get; }当前年 9 月 29 日The30thpublic static DateTime The30th { get; }当前年 9 月 30 日注意属性命名遵循英文序数词1st/2nd/3rd 之后统一为 Nth且 9 月的有效日数上限为 30因此属性恰好到The30th为止不存在The31st。2.3 静态方法 The(int)成员签名参数返回值The(int)public static DateTime The(int dayNumber)dayNumberSystem.Int329 月的第 N 天System.DateTime官方文档对它的描述为 The nth day of September of the current year即动态版本——当日期来自变量而非编译期常量时用On.September.The(day)替代硬编码属性。三、源码实现生成机制与真实代码3.1 September 的实际源码On.September的真实实现位于 On.Days.cs核心代码非常直白/// summary /// Provides fluent date accessors for September /// /summary public class September { /// summary /// The nth day of September of the current year /// /summary public static DateTime The(int dayNumber) new(DateTime.Now.Year, 9, dayNumber); /// summary /// The 1st day of September of the current year /// /summary public static DateTime The1st new(DateTime.Now.Year, 9, 1); // ... The2nd ~ The30th 同构 ... /// summary /// The 30th day of September of the current year /// /summary public static DateTime The30th new(DateTime.Now.Year, 9, 30); }可以看到每个属性与方法体的实现都是对DateTime三参数构造函数new DateTime(year, month, day)的一次调用月份参数固定为9。3.2 T4 模板12 个月类族的统一生成源On.Days.cs共 2345 行是由 T4 模板 On.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 monthName { The(int) TheNth 属性 } }从模板结构可以确认三点实现事实月份类名来自 .NET 内置月份名模板用DateTime(leapYear, month, 1).ToString(MMMM)得到JanuaryDecember即September这个类名是 BCL 在默认en-US区域下格式化 9 月得到的结果属性数量由该月实际天数决定内层循环以DateTime.DaysInMonth(leapYear, month)为上限——闰年 2012 被特意选作参照年因此 1/3/5/7/8/10/12 月生成 31 个属性4/6/9/11 月生成 30 个September正属此类2 月生成 29 个属性名的序数词后缀由 Humanizer 自身生成模板调用day.Ordinalize()把1、2、3、21等变成1st、2nd、3rd、21st这正是The1st、The22nd这类命名的来源。3.3 同源类族On / In / OnDate / InDate仓库中FluentDate目录下存在一组对称文件In.Days.cs、On.Days.cs、InDate.Days.cs、OnDate.Days.cs 及各自的.tt模板。其中 OnDate.Days.cs 内同样存在一个September嵌套类官方 API 参考对应 website/docs/api/Humanizer.OnDate.September.md。两者区别在于OnDate系列面向DateOnly类型适用于支持DateOnly的 .NET 框架而On系列面向DateTime。场景文档 fluent-dates-and-time-spans.mdx 对此的概括是On.April.The3rd…… are readable constructors aroundDateTime.InDateandOnDateprovide correspondingDateOnlyvalues on compatible frameworks.四、运行时行为与边界条件结合实现new(DateTime.Now.Year, 9, dayNumber)可以明确以下运行时语义4.1 当前年语义年取自DateTime.Now.Year即执行时机器本地时间的年份。这意味着On.September.The15th在不同年份运行会得到不同的年2026 年运行时为2026-09-15。这一设计在测试代码中体现得非常清楚——OnTests.cs 中每个断言都以相同方式动态构造期望值[Fact] public void OnJanuaryThe23rd() Assert.Equal(new(DateTime.Now.Year, 1, 23), On.January.The23rd); [Fact] public void OnDecemberThe4th() Assert.Equal(new(DateTime.Now.Year, 12, 4), On.December.The4th); [Fact] public void OnFebruaryThe() Assert.Equal(new(DateTime.Now.Year, 2, 11), On.February.The(11));从该测试文件的组织方式看测试策略是与被测代码使用相同的DateTime.Now.Year来源来消除年漂移因此断言在跨年运行时依然稳定。需要说明的是当前仓库中的OnTests覆盖了 January、December 的属性与The(int)方法两种形态September类与其余月份共享同一生成模板与实现模式结构上同构。4.2 时间部分与 DateTimeKindDateTime三参数构造函数的行为.NET BCL 定义是时间部分为00:00:00当日午夜Kind为Unspecified。因此On.September.The1st得到的是当前年 9 月 1 日 00:00。若需要携带具体时刻可在其结果上再做加法运算或改用DateTimeOffset等类型另行构造。4.3 The(int) 的参数边界The(int dayNumber)将用户输入直接透传给DateTime构造函数。依据 .NET 构造函数的校验规则当dayNumber小于 1 或大于该月天数9 月为 30时会抛出ArgumentOutOfRangeException年数越界DateTime.Now.Year恒在合法范围内实际不会触发同理。因此在调用On.September.The(day)前业务代码应保证1 day 30。4.4 使用示例using Humanizer; // 静态属性9 月的固定日期当前年 DateTime labourDay On.September.The1st; // 当前年 9 月 1 日 00:00 DateTime deadline On.September.The30th; // 当前年 9 月 30 日 00:00 // 动态方法日期来自运行时变量 int day 15; DateTime midMonth On.September.The(day); // 当前年 9 月 15 日 00:00 // 与时间相加得到具体时刻 DateTime meeting On.September.The23rd.AddHours(9).AddMinutes(30);上述用法与场景文档 fluent-dates-and-time-spans.mdx 中On……build calendar values的定位一致它适合按日历日构造值的场景而不是表示经过时长时长由1.5.Days()等数值扩展方法负责。该场景文档还提示Inject a starting date instead of relying on now when code or tests must be repeatable.——对于需要可重复结果的单元测试建议显式注入起始日期而非依赖On.September内部读取的当前年。五、与相关 API 的对照API类型来源语义参考文档On.September.The1st/The(int)DateTime午夜零点Kind为Unspecified当前年 9 月的某一日Humanizer.On.September.mdOnDate.September系列DateOnly兼容框架上当前年 9 月的某一日纯日期类型Humanizer.OnDate.September.mdIn.September系列DateTime9 月内某一天的前置词访问器Humanizer.In.September.mdOn顶层类—承载 12 个月份嵌套类的静态容器仅有默认构造函数Humanizer.On.mdOn顶层类本身不提供任何日期成员其 API 参考 仅列出On()构造函数它是纯粹的命名空间容器真正的日期能力全部落在JanuaryDecember这些嵌套类上On.September是其中第 9 个。六、要点总结成员构成On.September提供 1 个默认构造函数、30 个静态属性The1stThe30th和 1 个静态方法The(int dayNumber)全部返回DateTime月参数固定为 9实现本质每个成员都是一次new DateTime(DateTime.Now.Year, 9, day)调用年份取自执行时的本地当前年时间部分为当日 00:00代码出处实现由 On.Days.tt 模板统一生成 12 个月类September因 9 月有 30 天而恰好拥有 30 个序数词属性属性数量与序数词后缀均由模板依据DaysInMonth与Ordinalize()自动推导使用边界The(int)透传校验dayNumber超出 130 会抛出ArgumentOutOfRangeException编写跨年运行的测试时应与被测代码一致地以DateTime.Now.Year构造期望值参见 OnTests.cs选型建议需要纯日期语义且目标框架支持DateOnly时优先使用同源的OnDate.September需要DateTime日历值时使用On.September。赞分享开发工具【免费下载链接】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 详解In.Nine 类完整参考与源码级解析Humanizer 流式日期 API 详解 In.Nine 类完整参考与源码级解析 本篇文章聚焦 Humanizer 流式日期FluentDateAPI开发工具amis 布局工具类 align-content 完全指南Flex/Grid 多行对齐的 6 个实用类amis 布局工具类 align content 完全指南Flex/Grid 多行对齐的 6 个实用类 导读 amis 作为一款通过 JSON 配置生成页面的开发工具Planka 开源项目管理工具指南拖拽看板 Docker 快速自建3 步跑起来Planka 开源项目管理工具指南拖拽看板 Docker 快速自建3 步跑起来 Planka 是一款可自托管的开源项目管理工具用看板、拖拽卡片和实时同开发工具上一篇WrenAI 新手指南3 条命令把自然语言变成可信的 SQL下一篇DebugSwift数据库浏览器零基础掌握SQLite查询与编辑的终极工具创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表