Coder Social home page Coder Social logo

Comments (5)

vmihailenco avatar vmihailenco commented on July 17, 2024

I will add support for timestamp with timezone data type, but I strongly recommend you to use timestamp without timezone.

from pg.

johto avatar johto commented on July 17, 2024

I don't mean to barge in with metaphorical guns blazing, but in almost every case using timestamp without time zone is a mistake. The behaviour of Go's time.Time is also closer to timestamp with time zone.

from pg.

vmihailenco avatar vmihailenco commented on July 17, 2024

Your experience is undeniable, but can you please explain why? I can't think of the situation where timestamp with time zone is useful... timestamp without timezone + separate field to store user time zone looks like a clear winner for me in all cases I can imagine...

from pg.

johto avatar johto commented on July 17, 2024

timestamp without timezone + separate field to store user time zone looks like a clear winner for me in all cases I can imagine...

The most common use for timestamps in the database (in my experience, at least) is recording the absolute point in time when something happened. If you truly meant to say using timestamp and the user's time zone (i.e. the time zone in which the event happened), there's no way to tell when exactly it happened during the repeated hour at the DST boundary. E.g.:

SELECT '2014-03-30 02:00:00'::timestamp AT TIME ZONE 'Europe/Stockholm';
        timezone        
------------------------
 2014-03-30 03:00:00+02
(1 row)

Of course, you can get around that by storing the offset as well, but now you have three fields to worry about and the math gets very hairy. Even ignoring that issue, querying gets more difficult. Say you want all events that happened within the last five minutes:

timestamptz:
    SELECT * FROM events WHERE time >= now() - interval '5 minutes'
timestamp + user's location:
    SELECT * FROM events WHERE time AT TIME ZONE tz >= now() - interval '5 minutes'
timestamp in UTC:
    SELECT * FROM events WHERE time >= (now() - interval '5 minutes') AT TIME ZONE 'Etc/UTC'

The first one is clearly the easiest. Storing the time in timestamp+user's time zone is the most difficult, and it requires an extra index. The other two can work with just an index on "time" alone.

from pg.

vmihailenco avatar vmihailenco commented on July 17, 2024

Tests can be found here: https://github.com/vmihailenco/pg/blob/master/main_test.go#L247

from pg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.