rust - 如何在 Rust 中获取一个月的天数?
问题描述
有没有一种惯用的 Rust 方法来获取给定月份的天数?我看过 chrono 但我在文档中没有找到任何关于此的内容。
我正在寻找可以管理闰年的东西,类似于calendar.monthrange
Python 或DateTime.DaysInMonth
C# 。
解决方案
您可以NaiveDate::signed_duration_since
从chrono
板条箱中使用:
use chrono::NaiveDate;
fn main() {
let year = 2018;
for (m, d) in (1..=12).map(|m| {
(
m,
if m == 12 {
NaiveDate::from_ymd(year + 1, 1, 1)
} else {
NaiveDate::from_ymd(year, m + 1, 1)
}.signed_duration_since(NaiveDate::from_ymd(year, m, 1))
.num_days(),
)
}) {
println!("days {} in month {}", d, m);
}
}
推荐阅读
- easeljs - 位图缓存作为图像源
- perl - 子程序未返回正确的整数
- html - PrimeNG 切换按钮图标
- bots - 需要 DialogFlow 提示语句中的建议
- ms-access - 在 Access 的组合框中显示带有破折号的社会保险号
- vue.js - 如何在插槽中定义一个组件,该组件作为子组件中定义的道具
- angular - 如何使用角度谷歌地图显示多个标记 - 角度 6
- angular - ng2-ckeditor 中的自定义函数
- sql - ORA-12505, TNS:listener 目前不知道 11g 中连接描述符中给出的 SID
- image - 有没有办法调整颜色的亮度和强度以将可读信号(数据)引入图像并检索它