如何区分 COleDateTime 中的无时间和午夜 class

how to distinguish between no time and midnight in COleDateTime class

我们在前端和后端之间使用 DD-MM-YYYY HH:mm:ss 日期和时间格式 API。

但是,由于前端没有选择时间,所以后端有时只获取日期。 GET 调用参数将如下所示:

startTime=16-11-2021

在后端,我们使用 COleDateTime class 来解析日期和时间。问题是上面例子中的时间被初始化为 00:00:00 而实际上没有发送时间。我需要一种方法来区分未发送时间和午夜,因为实际时间在我们的系统中很重要,并且未发送时间和午夜之间存在很大差异。

似乎 ParseDateTime 函数希望在其第二个参数 VAR_TIMEVALUEONLY 中获得一个标志,以便在解析期间忽略日期部分。 因此,为此,我需要实现一个解析函数,以便知道我是否有时间,预计 ParseDateTime 函数将被设置为未定义的时间,如 -1 没有时间已发送。 COleDateTimeclass有没有办法区分午夜未到?如果没有,也许你有另一种方法我可以使用 public 库,比如 <chrono> 这可以解决我的问题?

COleDateTime has a function GetStatus() that is able to tell if the object has no value at all. In all other cases, it's valid. This means that there is no distinction between "no time" and midnight. The type seems designed to encode 绝对时间点。仅日期或仅时间通过 0 组件劫持此编码。没有日期很容易被发现,因为日期编号从 0100-01-01 开始,不能与 0000-00-00 混淆。没有什么支持发现无时间。

如果您需要更好地处理时间的不确定性,这种格式不适合原样。不幸的是,大多数其他常见的替代品都有类似的问题。 <chrono> 将提供等效的功能,即 time_point. It also supports dates and time of day separately, with lots of conversion. Boost has similar limitations, as does DateTime(除非您误用了毫秒,这看起来很奇怪)。

因为你从前端获取的时间是DD-MM-YYYY HH:mm:ssDD-MM-YYYY格式的,所以你很容易解析有没有时间。最简单的可能是创建一个 DateTimeWrapper 来将 COleDateTimeisTimeAccurate 指标包装在一起。