简体   繁体   English

GPX和PostgreSQL的夏令时

[英]Daylight savings time for GPX and PostgreSQL

So here I'm working with gpx files. 所以在这里我正在处理gpx文件。 Take note of an excerpt of one: 注意一个摘录:

<?xml version="1.0" encoding="UTF-8"?>
<!-- GPSTrack 2.2.1 — http://bafford.com/gpstrack -->
<gpx xmlns="http://www.topografix.com/GPX/1/1">
<trk>
<name><![CDATA[2016-03-31 10-17-54]]></name>
<trkseg>
<trkpt lat="38.704859" lon="-8.970304"><ele>13.050667</ele><time>2016-03-31T09:17:51Z</time><!-- hAcc=95.768176 vAcc=10.000000 --></trkpt>
<trkpt lat="38.704894" lon="-8.970324"><ele>13.050667</ele><time>2016-03-31T09:17:55Z</time><!-- hAcc=141.087476 vAcc=10.000000 --></trkpt>
<trkpt lat="38.704859" lon="-8.970304"><ele>13.050667</ele><time>2016-03-31T09:17:55Z</time><!-- hAcc=95.768176 vAcc=10.000000 --></trkpt>
<trkpt lat="38.704878" lon="-8.970343"><ele>13.150488</ele><time>2016-03-31T09:18:43Z</time><!-- hAcc=165.000000 vAcc=10.000000 --></trkpt>

See the name of the file? 看到文件名了吗? It gives us the time 10H17. 它给了我们10H17的时间。 Now check the time for each point. 现在检查每个时间点。 It's counted with one less hour. 减少一小时。

I still don't get how the times are messed up here. 我仍然不明白这里的时光如何。 But this is the beginning of the problem. 但这是问题的开始。

Now, I'm parsing these many gpx files, and loading them into a PostgreSQL database. 现在,我解析了许多gpx文件,并将它们加载到PostgreSQL数据库中。 More specifically this table: 更具体地说,该表:

CREATE TABLE IF NOT EXISTS trips (
  trip_id SERIAL PRIMARY KEY,

  start_location TEXT REFERENCES locations(label),
  end_location TEXT REFERENCES locations(label),

  start_date TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  end_date TIMESTAMP WITHOUT TIME ZONE NOT NULL,

  bounds geography(POLYGONZ, 4326) NOT NULL,
  points geography(LINESTRINGZ, 4326) NOT NULL,

  timestamps TIMESTAMP WITHOUT TIME ZONE[] NULL
);

Even while using TIMESTAMP WITHOUT TIME ZONE the points are being loaded with the wrong hour (one less hour). 即使在使用TIMESTAMP WITHOUT TIME ZONETIMESTAMP WITHOUT TIME ZONE ,也会以错误的小时数(少一小时)加载点。 This happens only in the days after the Daylight Savings Time is in effect. 仅在夏令时生效后的几天内发生这种情况。 The point here: is there any way to check if a date is on DST time and add one hour to it if that checks? 这里的重点是:有什么方法可以检查日期是否在DST时间上,如果可以的话再加一个小时呢?

I checked for tm_isdst and datetime.dst() but I still don't understand it. 我检查了tm_isdstdatetime.dst()但我仍然不明白。

If you know the time zone name where this local time is supposed to be set in (assuming 'Europe/Vienna' in the example), you can normalize the value to correct UTC time (or any other time zone) with this expression: 如果您知道应该在其中设置本地时间的时区名称 (在示例中假设为“欧洲/维也纳” ),则可以使用以下表达式对值进行规范化以更正UTC时间(或任何其他时区):

SELECT '2016-03-31T09:17:51Z'::timestamp AT TIME ZONE 'Europe/Vienna' AT TIME ZONE 'UTC'

Result: 结果:

timezone
--------------------
2016-03-31 07:17:51

Yes, apply AT ZIME ZONE twice . 是的,在AT ZIME ZONE申请两次

Since you seem to be dealing with different time zones I would consider using timestamptz instead of timestamp in your tables. 由于您似乎要处理不同的时区,因此我会考虑在表中使用timestamptz而不是timestamp

Be sure to use time zone names , not abbreviations or plain time offsets to get precise adjustments for DST. 确保使用时区名称 ,而不是缩写或普通时间偏移量,以对DST进行精确调整。
I hate the moronic concept of "daylight saving time" - it never saves any daylight but keeps wasting valuable time of people being confused by it. 我讨厌“夏时制”的简单概念-它从不节省任何日光,但是却一直在浪费人们被它迷惑的宝贵时间。

Detailed explanation for all of that: 所有这些的详细说明:

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

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