【A11】时间区间(Time Range)

发布时间:2026/10/9 2:48:27

【A11】时间区间(Time Range) 核心语义基于左闭右开区间语义[start, end)start本次时间间隔的起始点包含也是本次版次的最小时间点end下次版次的最小时间点不包含即本次区间的上界结构体定义/// 时间区间 [start, end)#[derive(Copy, Clone, Debug, PartialEq)]pubstructTimeRange{start:DateTime,end:DateTime,}构造器方法注意除from_start_to_now和from_now_to_end外所有构造器均为const函数可在编译时求值。new(start, end)—const创建区间[start, end)。pubconstfnnew(start:DateTime,end:DateTime)-Self{debug_assert!(start.lt(end),TimeRange::new: start must be end);Self{start,end}}示例letrTimeRange::new(today_start,tomorrow_start);// 包含today_start ≤ t tomorrow_startfrom_start_to_now(start)— 非const构造从起始时间到现在的区间[start, now 1ms]包含 now。pubfnfrom_start_to_now(start:DateTime)-Self{Self::new(start,DateTime::now().ms(1))}示例letrTimeRange::from_start_to_now(last_sync);// 包含last_sync ≤ t ≤ nowfrom_now_to_end(end)— 非const构造从现在到指定时间的区间[now, end)。pubfnfrom_now_to_end(end:DateTime)-Self{Self::new(DateTime::now(),end)}示例letrTimeRange::from_now_to_end(deadline);// 包含now ≤ t deadlinepoint(at)—const构造仅包含单个时间点的区间[at, at1ms)。pubconstfnpoint(at:DateTime)-Self{Self::new(at,at.ms(1))}示例letrTimeRange::point(snapshot_time);// 仅包含t snapshot_timefrom_start_duration(start, duration)—const从起始时间和时长创建区间。pubconstfnfrom_start_duration(start:DateTime,duration:Duration)-Self{Self::new(start,start.add(duration))}示例letrTimeRange::from_start_duration(start,Duration::from_hours(2));// 包含[start, start 2h)from_end_backward(end, duration)—const从结束时间和时长向前创建区间[end - duration, end)。pubconstfnfrom_end_backward(end:DateTime,duration:Duration)-Self{Self::new(end.sub(duration),end)}示例letrTimeRange::from_end_backward(end,Duration::from_hours(2));// 包含[end - 2h, end)查询方法所有查询方法均为const函数。implTimeRange{/// 获取起始时间点包含pubconstfnstart(self)-DateTime{self.start}/// 获取结束时间点不包含pubconstfnend(self)-DateTime{self.end}/// 计算区间跨度时长pubconstfnduration(self)-Duration{self.end.diff(self.start)}/// 将区间转换为 (start, end) 元组pubconstfnto_parts(self)-(DateTime,DateTime){(self.start,self.end)}}判断方法所有判断方法均为const函数。implTimeRange{/// 判断某个时间点是否在区间内pubconstfncontains(self,t:DateTime)-bool{self.start.le(t)t.lt(self.end)}/// 判断是否包含另一个区间pubconstfncontains_range(self,other:Self)-bool{self.start.le(other.start)other.end.le(self.end)}/// 判断两个区间是否相邻pubconstfnis_adjacent_to(self,other:Self)-bool{self.end.eq(other.start)||other.end.eq(self.start)}/// 合并相邻区间必须首尾相接不支持重叠pubconstfnmerge(self,other:Self)-OptionSelf{ifself.end.eq(other.start){Some(TimeRange::new(self.start,other.end))}elseifself.start.eq(other.end){Some(TimeRange::new(other.start,self.end))}else{None}}}类型转换所有From/Into实现均非constRust 稳定版暂不支持consttrait。implFrom(DateTime,DateTime)forTimeRange{fnfrom((start,end):(DateTime,DateTime))-Self{Self::new(start,end)}}implFromTimeRangefor(DateTime,DateTime){fnfrom(range:TimeRange)-Self{(range.start,range.end)}}implFromRangeDateTimeforTimeRange{fnfrom(range:RangeDateTime)-Self{Self::new(range.start,range.end)}}implFromTimeRangeforRangeDateTime{fnfrom(range:TimeRange)-Self{range.start..range.end}}implFrom[DateTime;2]forTimeRange{fnfrom([start,end]:[DateTime;2])-Self{Self::new(start,end)}}implFromTimeRangefor[DateTime;2]{fnfrom(range:TimeRange)-Self{[range.start,range.end]}}使用示例letlast_syncDateTime::from_ymd(2026,8,18);letnowDateTime::now();// 构造区间letrangeTimeRange::from_start_to_now(last_sync);// [last_sync, now 1ms]// 检查包含关系assert!(range.contains(last_sync));// 起点包含assert!(range.contains(now));// now 包含assert!(!range.contains(now.ms(1)));// now 1ms 不包含右边界// 区间跨度letdurationrange.duration();// 相邻与合并letr1TimeRange::new(t1,t2);letr2TimeRange::new(t2,t3);ifletSome(merged)r1.merge(r2){// 合并后的区间覆盖 [t1, t3)}const 函数汇总方法分类方法名是否const构造器new✅point✅from_start_duration✅from_end_backward✅from_start_to_now❌依赖DateTime::now()from_now_to_end❌依赖DateTime::now()查询start✅end✅duration✅to_parts✅判断contains✅contains_range✅is_adjacent_to✅merge✅类型转换所有From/Into❌Rust 稳定版限制设计要点概念说明start区间起始时间点包含本次版次的最小时间点end区间结束时间点不包含下次版次的最小时间点有效区间[start, end)即start ≤ t end存储形式自定义结构体TimeRange包含start和end两个字段包含 nowfrom_start_to_now(start)使用now.ms(1)作为结束边界使now被包含相邻合并end other.start或other.end start仅相邻可合并重叠不可合并const 支持除涉及now()的构造器和类型转换外所有方法均为const一句话总结start是本次版次的最小时间点包含end是下次版次的最小时间点不包含。区间[start, end)使用from_start_to_now(start)包含now时end now.ms(1)。
延伸阅读

更多相关文章

2026/10/8 4:44:20

生产管理系统和WMS有什么区别?制造企业怎么搭配?

本文要点 本文澄清制造企业在生产管理系统与 WMS(仓储管理系统)选型上常见的概念误区,以某 PCB 设备企业大规模搭建业务流程的真实案例为参照,讲清两者边界与搭配方式。文章逐条澄清"WMS 能管排程"、“生产系统自带完整…

2026/10/7 23:30:56

JVM与多线程面试核心要点解析

1. 为什么JVM与多线程成为校招面试的"重灾区"?每年校招季,我都会参与公司的Java技术面试工作。一个明显的现象是:近90%的应届生在JVM和多线程这两个核心领域表现欠佳。这并非偶然——JVM涉及内存管理、类加载机制等底层原理&#x…

2026/10/8 4:44:13

AI Agent行为诊断框架:从可观测性到根因分析的工程实践

1. 项目概述:为什么我们需要一个AI Agent诊断框架?最近在折腾AI Agent项目时,我遇到了一个非常典型的问题:Agent在测试环境中运行良好,但一到生产环境,面对更复杂的用户输入和任务链,就开始出现…

2026/10/9 2:44:37

大模型学习路线图:12步小白也能轻松入门并收藏!

本文提供一张清晰的十二步大模型学习路线图,帮助读者从入门到落地高效搭建完整知识体系。路线涵盖Python基础、Transformer原理、提示词工程、LangGraph、LangChain、RAG、Agent、多Agent协同、私有化部署、多模态技术、量化技术和模型微调。建议按顺序学习&#xf…

2026/10/9 2:44:37

2026瓷砖一线品牌有哪些?家装瓷砖品牌推荐

2025年全国陶瓷砖产量掉了17.8%,现在行业开窑率连一半都不到。大家都在抢存量,挑瓷砖早就不只看花色和单价了。新国标GB/T 45817-2025把防污、耐磨这些指标分成了3A到5A三级。现在买砖得看品牌实力、制造产能、产品性能、研发技术、市场渠道、品牌口碑和…

2026/10/9 2:39:37

【回眸】上海金桥沪东考点低压电工实操考试体验

目录 前言 考试流程 总结 前言 26年9月20日,前往沪东考点进行低压电工实操考试。 考试之前准备还算充分,打听了一下大家考试出现问题的地方。 第一个是绝缘手套没戴,第二个是安全帽没规范佩戴,需要把安全帽的下颚带拉好&…

2026/10/8 10:03:18

Jev+Agent接管浏览器:browser-use实战与jev-ultrafast性能优化

1. 从“Jev”说起:为什么我要把Agent接进浏览器“Jev”这个词最近在圈子里出现的频率越来越高,很多人第一次听到会以为是某个新模型的名字,其实它更像是一种思路——把Jev模型的能力当作底座,通过Agent的方式去接管浏览器&#xf…

2026/10/8 10:03:20

多智能体集群实战:DeepAgents编排、MCP与A2A协议及Skills体系

1. 从"单兵作战"到"集群协同":多智能体编排到底在解决什么问题如果你最近在折腾 Agent 相关的东西,大概率会有一种感觉:单个 Agent 能做的事情,其实很快就摸到天花板了。你给它一个提示词,挂几个工…

2026/10/8 6:05:44

无源低通滤波器设计实战:从RC到LC,手把手教你避开那些坑

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/9 0:04:27

毕业论文初稿完成后首次进行AIGC疑似度自查的摸底与分流策略

毕业论文初稿完成后首次进行AIGC疑似度自查的摸底与分流策略当数万字的学位论文初稿经历开题、实验、问卷与多轮文献梳理最终成形时,绝大多数研究生都会面临一道全新的形式审查关卡:AIGC 疑似度排查。在高校毕业审核流程中,盲审前的文本检测通…

2026/10/9 0:04:27

食堂节能改造源头工厂,商用厨房设备焕新方案广受好评

商用厨房作为餐饮经营、单位供餐的核心后勤阵地,其设备配置、动线规划与运维体系直接决定后厨作业效率、运营成本与合规性。从基础的灶具、制冷存储设备,到油烟净化、水处理等配套系统,每一个环节的合理性都与食品安全、能耗管控、消防安全挂…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑