Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL & MySQL returning different timezones on timestamp query #3668

Open
3 tasks done
Valentin-Metz opened this issue Jun 28, 2023 · 3 comments
Open
3 tasks done

Comments

@Valentin-Metz
Copy link

Valentin-Metz commented Jun 28, 2023

Versions

  • Rust: cargo 1.71.0-nightly (9e586fbd8 2023-04-25)
  • Diesel: 2.01
  • Database: PostgreSQL & MariaDB
  • Operating System Manjaro

Feature Flags

  • diesel: diesel = { version = "2.0.1", features = ["chrono", "serde_json"] }

Problem Description

Postgres and MySQL queries behave differently when querying timestamps:

Postgres query:

.select(my_table::login_timestamp)
.filter(my_table::login_timestamp.gt(Local::now().naive_local() - chrono::Duration::seconds(5)))
.first::<chrono::NaiveDateTime>(database)

(The select for Postgres returns local time)

Identical query for MySQL:

.select(my_table::login_timestamp)
.filter(my_table::login_timestamp.gt(Local::now().naive_utc() - chrono::Duration::seconds(5)))
.first::<chrono::NaiveDateTime>(database)

(The select for MySQL returns UTC instead of local time)
A SELECT timestamp from my_table; in the mysql-console returns local time correctly.
Column datatype is a "Timestamp" in both cases.

Checklist

  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
  • This issue can be reproduced without requiring a third party crate
@Valentin-Metz Valentin-Metz changed the title PostgreSQL PostgreSQL & MySQL returning different timezones on timestamp query Jun 28, 2023
@weiznich
Copy link
Member

This bug report is missing important information about the relevant schema used for both database systems. Ideally you would add/link a completely reproducible example of the underlying issue. It is at least required to add the relevant schema definition (as sql), otherwise I can only close this issue due to missing informarion.

@Valentin-Metz
Copy link
Author

Postgres:

create table public.my_table
(
    id                            serial8
        constraint connections_pk
            primary key,
    login_timestamp               timestamp not null
);

MariaDB:

create table my_table
(
    id                            bigint auto_increment primary key,
    login_timestamp               timestamp not null
);

Let me know if this is enough. Otherwise I can try to reproduce this in a Docker compose.

@mattdowdell
Copy link

mattdowdell commented Jul 9, 2023

Timestamp in MariaDB is stored as seconds since the Unix epoch in UTC. Timestamp in PostgreSQL can be made timezone aware, although your example is not.

Perhaps try using timestamp with time zone or timestamptz for the type in your PostgreSQL schema?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants