6

While doing the mapping of some database columns into Java classes I stumbled onto this obscure SQL-92 Standard type (implemented by PostgreSQL, H2, and HyperSQL afaik). I haven't ever used it, but I wanted to understand how clearly map it to a Java type if I ever find it.

Here are the variants I can see:

  • Case A: The TIME type, such as 15:20:01. It's a "local time". The time zone is evident to the application so the database doesn't record it.

  • Case B: The TIME with offset, as in 15:20:01+04:00. It represents a "world time". This time can be converted trivially to UTC, or to any other world clock.

  • Case C: A TIME with a time zone, such as 15:20:01 EDT. Since the rules to interpret a time strongly depend on the specific date I can't really make any sense of it without the date; but then, if I add the date, it becomes a TIMESTAMP, and that's something totally different.

So, did the SQL Standard get it wrong? Or maybe "TIME with time zone" should be always interpreted as "time with offset" (case B)?

The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • 1
    I don't know the answer, but I like the format of the question so you get an up :) – Mech Mar 02 '20 at 15:09
  • ANSI SQL's TIME WITH TIMEZONE contains a TIMEZONE_HOUR field and a TIMEZONE_MINUTE field. (I.e. Case B.) – jarlh Mar 02 '20 at 15:13
  • Note that at least in the Postgres community the general consent is that `time with time zone` should not be used at all (and is only there to comply with the SQL standard and that the standard did get it wrong) –  Mar 02 '20 at 15:13
  • @TheImpaler, ISO/IEC 9075-2:2016(E) 4.1 Data types. – jarlh Mar 02 '20 at 15:17
  • @a_horse_with_no_name I would totally agree with PostgreSQL recommendation. This type is confusing. – The Impaler Mar 02 '20 at 15:21
  • @jarlh I got a hold to a copy of the doc you mention. I think you are right. Maybe the SQL standard type should be renamed to `TIME WITH OFFSET`. The "TIME ZONE" phrase is blatantly misleading. – The Impaler Mar 02 '20 at 15:25

2 Answers2

1

Case C, a TIME with a time zone, such as 15:20:01 EDT, can be meaningful for things like store opening hours. Imagine you have a nationwide chain of stores. You want to store each store's standard opening hours in the database. The opening and closing time is a local time with an associated time zone. It isn't a time with a UTC offset (your case B), since it is defined in each store's local time zone, and hence daylight savings–or more rarely a change in the time zone definition–will change the UTC offset without actually changing the value of the opening time column. This store opens at 9am year round, but because its time zone has daylight savings, that is a different UTC offset at different times of year. But we aren't storing a date, because the standard opening/closing times are date-independent. (Maybe we'd have effective-from/effective-to dates, or similar, to track changes to standard opening hours over time.)

It isn't exactly case A, because imagine you have a table of stores, with opening_time and closing_time columns – if they are in different timezones, then case A would make those columns be a mix of data from different time zones, without being explicit about that. Now, given the poor support for case C in most databases, that's probably what happens – you'll probably store the time zone as an additional column. But Case C isn't useless in principle, unlike what many people think.

Simon Kissane
  • 4,373
  • 3
  • 34
  • 59
0

For lots of reasons, that you described well, interpreting a point in time with time of day and variable time zone but without a date is effectively undefined. There are use cases though, where you're establishing a policy within an international context this would be a helpful data type. Everyday at 15:20:01+04:00 the cats need to take a nap. Now the intention isn't to evaluate value in iosolation but within the context of adding it to a baseline date. Standards are all about supporting theoretical possibilities eaven if they're not super common.

user3112728
  • 395
  • 1
  • 12
  • `15:20:01+04:00` is a case B, that actually is useful. Case C is the one that I cannot find any use for... at all. – The Impaler Apr 25 '23 at 12:48