Best way to store time above 24:00:00 in postgresql?

﹥>﹥吖頭↗ 提交于 2019-12-01 04:20:26

问题


I'm storing GTFS feeds into a SQL database and some times are expected to be stored above the 24:00:00 cap on time values. For example, some trains run at 12:30AM, but are listed for the previous days service, and that running time is stored as 24:30 in the GTFS specifications.

What would be the best way of getting around this? Should I just store it as a string?


回答1:


Suggest to use int for that... your value could be:

Sec + Min * 60 + Hour * 3600

For the 24:30:00, you will get 88200.

When loading your value from DB, you could reverse your value by simple math equation:

Hour = int(value / 3600)
Min  = int(value % 3600 / 60)
Sec  = value % 3600 % 1800



回答2:


I'd store two fields:

departure_time timestamp with time zone,
service_date date

Departure time would be calculated like this:

 => select '2015-07-08'::timestamptz+'24:30'::interval;
 2015-07-09 00:30:00+02

This way:

  • you have a normal moment of time field for sorting events;
  • you'd not loose service date information;
  • you'd be able to calculate back the original GTFS data if needed.


来源:https://stackoverflow.com/questions/32465991/best-way-to-store-time-above-240000-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!