简体   繁体   English

在编程语言和数据库中存储日期和时间的不同结构如何工作?

[英]How do different structures for storing date and time in programming languages and databases work?

Given that languages and databases use different structures for storing date and time, what piece of code is responsible for transforming the date and time data structure when moving the date and time value from programming language to database and vice versa?鉴于语言和数据库使用不同的结构来存储日期和时间,当将日期和时间值从编程语言移动到数据库时,哪段代码负责转换日期和时间数据结构,反之亦然?

Most systems store DATETIME as a floating point number.大多数系统将 DATETIME 存储为浮点数。 The value to the left of the decimal point is the number of days from day zero(0).小数点左边的值是从第零 (0) 天算起的天数。 Day zero can be anything.零日可以是任何事情。 In Unix, day 0 is 1/1/1970.在 Unix 中,第 0 天是 1/1/1970。 The year of Unix birth.出生年份Unix。 However, this is not very intuitive to the average user.然而,这对普通用户来说不是很直观。 Oracle uses the actual year 0 and SQL Server uses 1/1/1900. Oracle 使用实际年份 0 和 SQL 服务器使用 1/1/1900。

Convert the number 0 to a DATETIME and you'll get that systems Day 0.将数字 0 转换为 DATETIME,您将获得该系统第 0 天。

The numbers to the right of the decimal point is the time.小数点右边的数字是时间。 This can be recorded in milliseconds, microseconds, whatever granularity your system can handle.这可以以毫秒、微秒为单位记录,无论您的系统可以处理的粒度如何。

The best part of this method is date math becomes very simple.这种方法最好的部分是日期数学变得非常简单。 Converting the number to the actual date and time is were all the complexity is involved.将数字转换为实际日期和时间是非常复杂的。

Most programming languages will follow this same pattern.大多数编程语言都会遵循同样的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM