
開發(fā)工具【免費(fèi)下載鏈接】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點(diǎn)擊查看免費(fèi)下載OnDate.February 是 Humanizer 流式日期Fluent DateAPI 家族中面向 2 月的專用訪問器它以嵌套類的形式為 2 月的每一天提供一個靜態(tài)屬性The1st到The29th并提供一個按天數(shù)取值的The(int dayNumber)方法返回值均為System.DateOnly。本文基于當(dāng)前倉庫中 version-3.0.10 的 API 文檔 展開并結(jié)合源碼與測試用例完整覆蓋該類全部 29 個屬性、動態(tài)取值方法、生成原理、閏年處理與測試驗證讀完即可在自己的 .NET 6 項目中直接使用。OnDate.February 的定位DateOnly 版的流式 2 月訪問器在 Humanizer 的Humanizer命名空間下OnDate是一個容器類內(nèi)部按月份嵌套了 12 個類JanuaryDecember每個類都遵循相同的模式為當(dāng)月每一天提供一個第 N 天的靜態(tài)屬性以及一個The(int dayNumber)方法。OnDate.February正是其中負(fù)責(zé) 2 月的那個嵌套類。public class OnDate.February類的繼承鏈為System.Object→February這一點(diǎn)與倉庫中 OnDate.Days.cs 源碼 的聲明一致public class February嵌套于public class OnDate內(nèi)部。關(guān)鍵設(shè)計點(diǎn)在于OnDate系列返回的是System.DateOnly而不是System.DateTime。DateOnly是 .NET 6 引入的、專門表示年-月-日且不含時間部分的日期類型因此整套OnDateAPI 被編譯指令#if NET6_0_OR_GREATER包裹僅在 .NET 6 及以上框架中可用。與之相對的倉庫中還提供了返回DateTime的 On.Days.cs 系列On.February.The11th等供需要完整時間組件的場景使用??焖偕鲜秩写a拿到 2 月日期引用 Humanizer 包后直接通過靜態(tài)屬性即可獲得 2 月某一天年份取當(dāng)前年份using Humanizer; DateOnly valentinesDay OnDate.February.The14th; // 今年 2 月 14 日 DateOnly arbitraryDay OnDate.February.The(23); // 今年 2 月 23 日與手寫new DateOnly(DateTime.Now.Year, 2, 14)相比OnDate.February.The14th以自然語言般的表達(dá)消除了魔法數(shù)字代碼自文檔化程度更高。倉庫測試 OnDateTests.cs 中給出了等價性的驗證方式[Fact] public void OnFebruaryThe() Assert.Equal(new(DateTime.Now.Year, 2, 11), OnDate.February.The(11));完整 API 清單29 個屬性加 1 個動態(tài)方法根據(jù) Humanizer.OnDate.February.md 的 API 文檔OnDate.February共暴露 29 個靜態(tài)屬性對應(yīng) 2 月的 129 天和 1 個靜態(tài)方法。所有屬性均聲明為public static System.DateOnly The{第N天} { get; }下表列出全部屬性及其語義屬性語義屬性語義The1st2 月 1 日The16th2 月 16 日The2nd2 月 2 日The17th2 月 17 日The3rd2 月 3 日The18th2 月 18 日The4th2 月 4 日The19th2 月 19 日The5th2 月 5 日The20th2 月 20 日The6th2 月 6 日The21st2 月 21 日The7th2 月 7 日The22nd2 月 22 日The8th2 月 8 日The23rd2 月 23 日The9th2 月 9 日The24th2 月 24 日The10th2 月 10 日The25th2 月 25 日The11th2 月 11 日The26th2 月 26 日The12th2 月 12 日The27th2 月 27 日The13th2 月 13 日The28th2 月 28 日The14th2 月 14 日The29th2 月 29 日The15th2 月 15 日屬性命名使用英文序數(shù)詞后綴1st、2nd、3rd、21st、22nd、23rd等與日常英語表達(dá)一致。動態(tài)取值方法 The(int dayNumber)除固定屬性外還提供按參數(shù)取任意一天的方法public static System.DateOnly The(int dayNumber);參數(shù)dayNumber類型System.Int32表示 2 月中的第幾天返回值System.DateOnly。該方法適合天數(shù)在運(yùn)行時才確定的場景如由業(yè)務(wù)規(guī)則計算出的日期例如OnDate.February.The(userInput)。源碼實現(xiàn)解析一行表達(dá)式與 T4 代碼生成OnDate.February的實現(xiàn)非常精簡。在 OnDate.Days.cs 中February 類的核心代碼如下public class February { /// summary /// The nth day of February of the current year /// /summary public static DateOnly The(int dayNumber) new(DateTime.Now.Year, 2, dayNumber); /// summary /// The 1st day of February of the current year /// /summary public static DateOnly The1st new(DateTime.Now.Year, 2, 1); // ……The2nd 到 The28th 依此類推…… /// summary /// The 29th day of February of the current year /// /summary public static DateOnly The29th new(DateTime.Now.Year, 2, 29); }從源碼結(jié)構(gòu)可以確認(rèn)幾個實現(xiàn)事實年份始終取DateTime.Now.Year所有屬性與方法都以當(dāng)前年份 固定月份 2 目標(biāo)日構(gòu)造DateOnly因此每年 2 月調(diào)用都會自動得到當(dāng)年的日期無需手動傳入年份屬性均為表達(dá)式體 getter每個屬性內(nèi)部就是一個DateOnly構(gòu)造表達(dá)式無額外邏輯開銷可忽略2 月最多提供到The29thThe29th的存在使閏年場景2 月 29 日天然可用非閏年時該日期不存在構(gòu)造DateOnly會拋出ArgumentOutOfRangeException這屬于DateOnly類型的正常邊界行為使用前需按業(yè)務(wù)判斷年份。值得說明的是這份看似重復(fù)的代碼并非手寫而是由T4 文本模板OnDate.Days.tt 自動生成的。模板的核心邏輯如下const int leapYear 2012; for (var month 1; month 12; month) { var monthName new DateTime(leapYear, month, 1).ToString(MMMM); // ……生成 public class 月份名 嵌套類…… for (var day 1; day DateTime.DaysInMonth(leapYear, month); day) { var ordinalDay day.Ordinalize(); // ……生成 public static DateOnly The{序數(shù)} new(DateTime.Now.Year, 月份, 天); } }這段模板揭示了兩個設(shè)計細(xì)節(jié)模板以 2012 年閏年為基準(zhǔn)計算每月天數(shù)DateTime.DaysInMonth(2012, 2)返回 29因此 2 月恰好生成The1stThe29th共 29 個屬性其他月份如 4 月則只生成到The30th。也就是說每個屬性對應(yīng)合法的一天這一正確性由生成邏輯保證屬性名中的序數(shù)詞由 Humanizer 自身的Ordinalize()擴(kuò)展方法生成1.Ordinalize()得到1st這正是屬性The1st命名中st/nd/rd/th后綴的來源體現(xiàn)了 Humanizer 用自身能力生成自身 API 的巧思Ordinalize定義于 OrdinalizeExtensions.cs。測試驗證倉庫如何證明 API 行為OnDate.February的行為在 OnDateTests.cs 中有直接測試覆蓋#if NET6_0_OR_GREATER public class OnDateTests { [Fact] public void OnJanuaryThe23rd() Assert.Equal(new(DateTime.Now.Year, 1, 23), OnDate.January.The23rd); [Fact] public void OnDecemberThe4th() Assert.Equal(new(DateTime.Now.Year, 12, 4), OnDate.December.The4th); [Fact] public void OnFebruaryThe() Assert.Equal(new(DateTime.Now.Year, 2, 11), OnDate.February.The(11)); } #endif測試文件的#if NET6_0_OR_GREATER預(yù)編譯指令與源碼保持一致再次確認(rèn)OnDate系列僅面向 .NET 6。其中OnFebruaryThe用例驗證了The(int dayNumber)方法當(dāng)參數(shù)為 11 時結(jié)果應(yīng)等于new DateOnly(當(dāng)前年, 2, 11)——與源碼new(DateTime.Now.Year, 2, dayNumber)的實現(xiàn)完全對應(yīng)。如果你對某一天的固定屬性有疑問也可以仿照該模式用Assert.Equal(new(DateTime.Now.Year, 2, N), OnDate.February.TheNth)自行驗證。流式日期家族的橫向?qū)Ρ萇nDate.February不是孤立的 API理解它與同族 API 的關(guān)系能幫你選對工具API返回類型框架要求源碼文件用途OnDate.February.The11thDateOnly.NET 6OnDate.Days.cs只需日期、不要時間部分時On.February.The11thDateTime所有支持的框架On.Days.cs需要帶時間組件午夜零點(diǎn)的完整日期時間時兩個系列結(jié)構(gòu)完全對稱均由各自的.tt模板OnDate.Days.tt 與 On.Days.tt生成。在 Humanizer 的流式日期體系里On/OnDate用于定位某年某月某日In/InDate則用于當(dāng)前時間加/減一段時間例如In.Two.Days與 InDate.SomeTimeFrom.cs月份全名類 API 如On.February、On.March均可在 On.Days.cs 中找到。更多流式日期的使用示例可參考 On 系列 API 文檔 與 OnDate 系列 API 文檔。使用注意事項與最佳實踐確認(rèn)目標(biāo)框架為 .NET 6OnDate.February返回DateOnly整個OnDate類在NET6_0_OR_GREATER下才編譯。若項目面向 .NET Framework 或 .NET 5請改用返回DateTime的On.February屬性方法混用按場景取舍寫死的固定日期如情人節(jié)The14th優(yōu)先用屬性表達(dá)更自然動態(tài)計算的天數(shù)用The(dayNumber)注意 2 月天數(shù)邊界The29th只在閏年存在。非閏年構(gòu)造 2 月 29 日會因超出DateOnly合法范圍而拋異常涉及 2 月 29 日時應(yīng)先判斷DateTime.IsLeapYear(DateTime.Now.Year)年份語義所有訪問器都以DateTime.Now.Year為基準(zhǔn)不提供指定年份的重載。若需某特定年份的 2 月某日應(yīng)直接使用new DateOnly(year, 2, day)無狀態(tài)、零開銷全部為靜態(tài)成員與表達(dá)式體實現(xiàn)不產(chǎn)生實例狀態(tài)可放心在熱路徑與批量循環(huán)中調(diào)用。綜上OnDate.February以最簡的 API 面解決了拿到今年 2 月某一天的日常需求配合 Humanizer 的DateOnly支持與 T4 生成機(jī)制是流式日期 API 中值得優(yōu)先采用的表達(dá)方式。贊分享開發(fā)工具【免費(fèi)下載鏈接】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點(diǎn)擊查看免費(fèi)下載相關(guān)推薦Humanizer OnDate.May 流式日期訪問器完全指南用 DateOnly 構(gòu)造今年五月的每一天Humanizer OnDate.May 流式日期訪問器完全指南用 DateOnly 構(gòu)造今年五月的每一天 Humanizer 的 OnDate 系列類型開發(fā)工具Humanizer OnDate.August 完全指南用 DateOnly 流暢構(gòu)造 8 月的每一天Humanizer OnDate.August 完全指南用 DateOnly 流暢構(gòu)造 8 月的每一天 OnDate.August 是 Humanizer F開發(fā)工具Humanizer 流式日期 API 詳解OnDate.February 與 2 月日期訪問器Humanizer 流式日期 API 詳解OnDate.February 與 2 月日期訪問器 Humanizer 提供了一套以自然語言為核心的 .NET 字開發(fā)工具上一篇3步解鎖WeMod完整功能Wand-Enhancer終極配置指南下一篇WarcraftHelper魔獸爭霸3現(xiàn)代系統(tǒng)兼容性終極解決方案創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考