在 Informix 中将 DATETIME 转换为 Unix 纪元
Posted
技术标签:
【中文标题】在 Informix 中将 DATETIME 转换为 Unix 纪元【英文标题】:Convert DATETIME to Unix Epoch in Informix 【发布时间】:2013-01-06 22:40:25 【问题描述】:我有一个 DATETIME 列:
SELECT mytime FROM mytable;
mytime
--------------------
1/6/2013 10:41:41 PM
我想编写一条 SQL 语句,以 Unix 时间格式(自 Unix 纪元以来的秒数 - 01/01/1970 00:00:00)返回 INTEGER
。我曾尝试使用DATEDIFF
和CAST
,但没有运气。这是 Informix 数据库。
【问题讨论】:
mytime
变量的类型是什么,因为这是一种非标准格式,结果呈现在其中(也是一种可怕的格式)。
【参考方案1】:
假设 mytime
列是 DATETIME YEAR TO SECOND 列(尽管问题中显示了格式),那么下面的存储过程就可以完成这项工作。它的评论多于程序,但 cmets 解释了它在做什么。
# "@(#)$Id: tounixtime.spl,v 1.6 2002/09/25 18:10:48 jleffler Exp $"
#
# Stored procedure TO_UNIX_TIME written by Jonathan Leffler (previously
# jleffler@informix.com and now jleffler@us.ibm.com). Includes fix for
# bug reported by Tsutomu Ogiwara <Tsutomu.Ogiwara@ctc-g.co.jp> on
# 2001-07-13. Previous version used DATETIME(0) SECOND TO SECOND
# instead of DATETIME(0:0:0) HOUR TO SECOND, and when the calculation
# extended the shorter constant to DATETIME HOUR TO SECOND, it added the
# current hour and minute fields, as documented in the Informix Guide to
# SQL: Syntax manual under EXTEND in the section on 'Expression'.
# Amended 2002-08-23 to handle 'eternity' and annotated more thoroughly.
# Amended 2002-09-25 to handle fractional seconds, as companion to the
# new stored procedure FROM_UNIX_TIME().
#
# If you run this procedure with no arguments (use the default), you
# need to worry about the time zone the database server is using because
# the value of CURRENT is determined by that, and you need to compensate
# for it if you are using a different time zone.
#
# Note that this version works for dates after 2001-09-09 when the
# interval between 1970-01-01 00:00:00+00:00 and current exceeds the
# range of INTERVAL SECOND(9) TO SECOND. Returning DECIMAL(18,5) allows
# it to work for all valid datetime values including fractional seconds.
# In the UTC time zone, the 'Unix time' of 9999-12-31 23:59:59 is
# 253402300799 (12 digits); the equivalent for 0001-01-01 00:00:00 is
# -62135596800 (11 digits). Both these values are unrepresentable in
# 32-bit integers, of course, so most Unix systems won't handle this
# range, and the so-called 'Proleptic Gregorian Calendar' used to
# calculate the dates ignores locale-dependent details such as the loss
# of days that occurred during the switch between the Julian and
# Gregorian calendar, but those are minutiae that most people can ignore
# most of the time.
CREATE PROCEDURE to_unix_time(d DATETIME YEAR TO FRACTION(5)
DEFAULT CURRENT YEAR TO FRACTION(5))
RETURNING DECIMAL(18,5);
DEFINE n DECIMAL(18,5);
DEFINE i1 INTERVAL DAY(9) TO DAY;
DEFINE i2 INTERVAL SECOND(6) TO FRACTION(5);
DEFINE s1 CHAR(15);
DEFINE s2 CHAR(15);
LET i1 = EXTEND(d, YEAR TO DAY) - DATETIME(1970-01-01) YEAR TO DAY;
LET s1 = i1;
LET i2 = EXTEND(d, HOUR TO FRACTION(5)) -
DATETIME(00:00:00.00000) HOUR TO FRACTION(5);
LET s2 = i2;
LET n = s1 * (24 * 60 * 60) + s2;
RETURN n;
END PROCEDURE;
【讨论】:
它不能正常工作,昨天的值比今天的高。执行程序 to_unix_time('2020-05-14 10:50:10') 5490414400 执行程序 to_unix_time('2020-05-14 21:50:10') 9450414400 执行程序 to_unix_time('2020-05-15 10:50: 10') 5490500800 执行程序 to_unix_time('2020-05-15 21:50:10') 9450500800 @Max — 请给我发电子邮件(请参阅我的个人资料)。我无法重现您的结果(在装有 macOS 10.14.6 Mojave 的 MacBook Pro 上运行的 Informix 12.10.FC6(64 位))。我为这四个调用得到了这些值:1589453410、1589493010、1589539810、1589579410(尽管每个调用的小数点后都有 5 个零)。您正在使用哪个版本的 Informix,在哪个平台上运行?您引用的值对应于 2143 年和 2269 年的时间,令人费解。但是,这不能在 cmets 中明智地讨论,并且添加到答案中会使它的大小加倍,只会引起混乱。 我的 VERSION 时代 9.50C1 你是怎么推断出版本是9.50C1
的?一种方法是使用SELECT DBINFO('version', 'full') FROM informix.systables WHERE tabid = 1
。原定为 9.50 的版本已恢复到版本 10.00。
是否可以在 14.10 版本上使用更简单的方法来实现这一点?就我而言,我无法创建 SP。是否有直接的日期函数将日期时间转换为纪元?【参考方案2】:
函数 dbinfo('utc_current')
返回纪元时间(自 1970-01-01 00:00:00 UTC 以来的秒数)。
【讨论】:
是的,但它只返回当前时间的 Unix 时间戳,而不是问题所要求的任意时间。以上是关于在 Informix 中将 DATETIME 转换为 Unix 纪元的主要内容,如果未能解决你的问题,请参考以下文章
在 pandas.Series 中将时间戳转换为 datetime.datetime
在查询中将 DateTime.Ticks 转换为 MySQL DateTime
在 Python 3.4 中将 datetime.time 转换为 datetime.timedelta
在 PHP 中将 DOMTimeStamp 转换为 DATETIME
转换器无法在 windows phone 8.1 Datepicker 中将类型“system.datetime”的值转换为类型“datetime”