从日期中添加或减去天数的算法?
我正在尝试编写一个 Date 类以尝试学习 C++。
我正在尝试找到一种算法来添加或减去日期的天数,其中日从 1 开始,月从 1 开始。事实证明它非常复杂,谷歌并没有出现太多,
有谁知道算法这是哪个?
I'm trying to write a Date class in an attempt to learn C++.
I'm trying to find an algorithm to add or subtract days to a date, where Day starts from 1 and Month starts from 1. It's proving to be very complex, and google doesn't turn up much,
Does anyone know of an algorithm which does this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
最简单的方法是实际编写两个函数,一个将日期转换为给定开始日期的天数,另一个将转换回日期。一旦日期被表示为天数,对其进行添加或减去就很简单了。
您可以在这里找到算法: http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html
The easiest way is to actually write two functions, one which converts the day to a number of days from a given start date, then another which converts back to a date. Once the date is expressed as a number of days, it's trivial to add or subtract to it.
You can find the algorithms here: http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html
您实际上并不需要这样的算法(至少不是名副其实的算法),标准库可以完成大部分繁重的工作;日历计算是出了名的棘手。只要您不需要早于 1900 年的日期,那么:
用法示例:
You don't really need an algorithm as such (at least not something worthy of the name), the standard library can do most of the heavy lifting; calender calculations are notoriously tricky. So long as you don't need dates earlier than 1900, then:
Example usage:
我假设这是为了某种练习,否则您将使用已经提供给您的时间课程。
您可以将时间存储为自特定日期以来的毫秒数。然后,您可以添加适当的值,并在调用类的访问器时将其转换为日期。
I'm assuming this is for some kind of an exercise, otherwise you would use a time class that's already provided to you.
You could store your time as the number of milliseconds since a certain date. And then you can add the appropriate value and convert from that to the date upon calling the accessors of your class.
这是一个非常简单的方法的草图。为了简单起见,我假设要添加的天数
d
为正数。很容易将下面的内容扩展到d
为负数的情况。d
小于 365 或d
大于或等于 365。如果
d
小于 365:如果
d< /code> 大于 365:
或者,您可以用 儒略形式 然后仅添加到 Julian 形式并转换为 ymd 格式。
Here's a sketch of a very simple approach. For simplicity of ideas I will assume that
d
, the number of days to add, is positive. It is easy to extend the below to cases whered
is negative.Either
d
is less than 365 ord
is greater than or equal to 365.If
d
is less than 365:If
d
is greater than 365:Alternatively, you could express the date in, say, Julian form and then merely add to the Julian form and conver to ymd format.
一种方法是将日期映射到日期的儒略数,进行整数运算,然后转换回来。
您将找到大量有关朱利安函数的资源。
One approach is to map the date to the Julian number of the date, do your integer operations and then transform back.
You will find plenty of resources for the julian functions.
我建议首先编写一个例程,将年月日转换为自固定日期(例如,自 1.01.01 以来)的天数。以及一个将其转换回来的对称例程。
不要忘记正确处理闰年!
有了这两个,你的任务就变得微不足道了。
I would suggest writing first a routine which converts year-month-day into a number of days since fixed date, say, since 1.01.01. And a symmetric routine which would convert it back.
Don't forget to process leap years correctly!
Having those two, your task would be trivial.
试试这个功能。它可以正确计算加法或减法。 dateTime 参数必须采用 UTC 格式。
并且有使用示例:
Try this function. It correctly calculates additions or subtractions. dateTime argument must be in UTC format.
And there are example of using:
我知道这是一个非常古老的问题,但在处理日期和时间时,这是一个有趣且常见的问题。所以我想分享一些计算新日期的代码,而不使用 C++ 中的任何内置时间功能。
I know this is a very old question but it's an interesting and some common one when it comes to working with dates and times. So I thought of sharing some code which calculates the new date without using any inbuilt time functionality in C++.
我知道这是一个近十年前提出的老问题。但几天前我遇到了同样的作业,这是答案,如 这里
I know it is an old question asked almost a decade before. But a few days before I came across the same for an assignment, and here is the answer as in here
不知道这是否有帮助。我正在开发一个调度系统(在第一个简单草案中),该系统将开始日期计算为到期日 - 天数提前期。我使用了几秒钟的时间(自纪元以来),以便在未来的代码草案中获得更高的精度。
Don't know if this helps or not. I was working on a scheduling system which (in the first simple draft) calculated start date as due date - days lead time. I worked with seconds elapsed (since epoch) to allow greater precision in future drafts of the code.