在 MySQL 中存储负日期来表示 BC/BCE(与 AD/CE)?
我现在正在开发一个项目,该项目需要我使用户能够在数据库中存储负日期。 这些日期有可能跨越公元前数千年,最晚可达“今天”。 我参与过许多存储日期的项目,但它们都是相对较新的日期,并且没有一个是负值(BC)。
处理此类数据时我应该考虑哪些类型的事情? 我完全了解历史上发生的许多日历转换,但我将使用现代日历,而不是太担心进行转换。 然而,任何有关转换的信息将不胜感激,因为它是一个非常相关的主题,也是我在学术上感兴趣的一个主题,尽管在这个项目期间不需要关注。
更新:我将存储YYYY-MM-DD
(不需要该格式)。
I'm working on a project now that will require me to enable users to store negative-dates in the database. These dates have the potential to span many thousands of years BC, and as late as 'Today.' I've worked on many projects that stored dates, but they were all relatively-recent dates, and none of them were ever negative (BC).
What types of things should I take into considering when working with this type of data? I'm fully aware of the many calendar-shifts that have taken place in history, but I will be working with the modern calendar and not worrying too much about doing conversions. However, any information about conversions would be greatly appreciated as it is a very relevant topic, and one that I am academically interested in although not required to focus on during this project.
Update: I'll be storing YYYY-MM-DD
(That format isn't required).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的存储需要多精确? 是几年、几天还是几小时/分钟。
如果您只查看天或年,那么也许您不需要实际存储日期,而是需要存储 0 日期的 +/- 天/年的偏移量
How precise does your storage need to be? Is it years, days, or hours/minutes.
If you are only looking at Days or Years, then maybe you don't need to actually store the Date, but rather an Offset of +/- Day/Years from your 0 Date
我遇到了类似的问题,我想继续中继数据库中的日期字段,以允许我使用日期范围搜索,历史值的精确度可达一天。
(我的数据库包括出生日期和罗马皇帝的日期...)
解决方案是向所有日期添加一个常量年份(例如:3000),然后将它们添加到数据库并减去相同的日期向用户显示查询结果之前的编号。
如果您的数据库中已经有一些日期值,请记住使用新的常量值更新现有值。
I had the similar problem and I wanted to continue relay on date fields in the DB to allow me use date range search with accuracy of up-to a day for historic values.
(My DB includes date of birth and dates of roman emperors...)
The solution was to add a constant year (example: 3000) to all the dates before adding them to the DB and subtracting the same number before displaying the query results to the users.
If you DB has already some dates value in it, remember to update the exiting value with the new const number.
这个答案应该涵盖所有书面/记录的人类历史
MySQL 可接受的日期范围是从 1000-01-01 到 9999-12-31。 (来源:Dev.MySQL.com:11.2.2 DATE、DATETIME 和 TIMESTAMP 类型。)因此,事实上,问题不是以负数或BCE日期开始,而是以 1000 年之前的所有日期开始,例如图拉真 (Trajan) 的诞生、公元 53 年等。
所以,让我们劫持一些日期! 我们将把3000年视为1000年。我知道其他人已经建议了这一点,但是代码和演示在哪里? 这就是我想要填写的内容。
当减去/添加年份来劫持日期范围时,我们需要处理一些奇怪的边缘情况。 例如,BC 和 AD(直到公元 9999 年)都已损坏,但这两个日期范围的工作方式不同。 什么样的逻辑可以成功地满足以下两个要求的重新映射?
无论如何,让我们编码吧!
从任何 DB 值转换为
老子的出生日期,正式为“公元前 571 年”,技术上为“公元-571 年”。 它存储为
4571
。从任何日期转换为 DB 值
因此,老子的诞生,“公元前 571 年”。 它被视为
-571
。附录——关于破折号
如果您正在使用这些东西,您很快就会意识到许多日期格式库只需要两个破折号“年-月-日”等,并使用负数“-年-月-”天”,只会造成破坏。 就个人而言,我重写了上面的内容,将“BCE”前置到 bce 日期,即,您将拥有诸如“bce571-01-01”而不是“-571-01-01”等值。
This Answer Should Cover all of Written/Recorded, Human History
The MySQL acceptable range for dates is from 1000-01-01 to 9999-12-31. (Source: Dev.MySQL.com: 11.2.2 The DATE, DATETIME, and TIMESTAMP Types.) So, in fact, the problem doesn't begin with negative or BCE dates, but with all dates preceding the year 1000, for instance, the birth of Trajan, 53 AD, etc..
So, let's hijack some dates! We are going to treat the year 3000 as the year 1000. I know this has been suggested by others, but where's the code and the demonstration? That's what I want to fill in.
When subtracting/adding years to hijack date ranges, we need to handle some odd edge cases. For instance, both BC and AD (up to 9999 AD) are broken, but these two date ranges work differently. What kind of logic can do a successful remap with both of the follow requirements?
Anyway, let's code!
Convert From Any DB Value to Date
Birth of Laozi, formally "571 BCE", technically, "-571 AD". It stores as
4571
.Convert From Any Date to DB Value
So, once again, birth of Laozi, "571 BCE". It is treating as
-571
.Addendum -- About Dashes
If you're playing with this stuff, you'll soon realize that many date format libraries require only two dashes "year-month-day", etc., and using a negative, "-year-month-day", can just wreak havok. Personally, I have rewritten the above to have "BCE" prepended to bce dates, i.e., you'll have values like "bce571-01-01" instead of "-571-01-01", etc..