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

How to use system time_zone in MySqlConnection? #329

Open
pymongo opened this issue May 20, 2020 · 10 comments
Open

How to use system time_zone in MySqlConnection? #329

pymongo opened this issue May 20, 2020 · 10 comments

Comments

@pymongo
Copy link
Contributor

pymongo commented May 20, 2020

I want to use local/system timezone in MySqlPool.

let datetime = chrono::Utc::now().naive_local();
// The insert time is not equal to local time
sqlx::query("INSERT INTO orders (created_at) VALUES (?);")
    .bind(datetime)
    .execute(&pool)
    .await
    .unwrap();

But each MySqlConnection execute SET time_zone = '+00:00'; after it is established.

sql-core/src/connection.rs:317

        self_.execute(r#"
SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE'));
SET time_zone = '+00:00';
SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;
        "#).await?;

Potential Solutions

Execute SET time_zone = SYSTEM; Before insert/update datetime value,

but SET time_zone and Insert maybe using different connection.

@mehcode
Copy link
Member

mehcode commented May 20, 2020

See #263 (comment) for a potential solution to amend what SQLx is doing on start-up or for general purpose execution.


Another idea is to allow setting system variables via query parameters. The Go MySQL driver allows this.

mysql://root:password@localhost/db?time_zone=SYSTEM
mysql://root:password@localhost/db?time_zone=%27Europe%2FParis%27

Currently, there is no good answer here. #263 is probably a minimum to fix issues like this.

@pymongo
Copy link
Contributor Author

pymongo commented May 20, 2020

Here is my dirty way to write local timezone datetime.

// Assume local timezone is +08:00

// FIXME Local+naive_local()=+16 timezone!!!
let datetime = chrono::Local::now().naive_local();
sqlx::query("INSERT INTO trades (created_at) VALUES (?);")
    .bind(datetime)
    .execute(&pool)
    .await
    .unwrap();

MySqlConnection(0) + NaiveDateTime(+16) = local timezone(+8)

@pymongo pymongo closed this as completed May 20, 2020
@mehcode mehcode reopened this May 20, 2020
@mehcode
Copy link
Member

mehcode commented May 20, 2020

Let's keep this open until there is a solution here.

@abonander
Copy link
Collaborator

abonander commented May 21, 2020

For properly decoding DateTime<(any TimeZone)> we'd have to know what the system time zone is, and that the connection is set to use it instead of UTC.

Or I guess the onus is on the user if they decide to set the timezone to non-UTC to only use NaiveDateTime. Now that I think about it more, maybe this belongs on the ConnectOptions struct we're discussing in #174.

@comicfans
Copy link

Hello, I have similar problem, I want my result field DateTime, but query_as didn't accept it.

@CallumDowling
Copy link

How is this going? When selecting NOW() on sqlx I get a utc+0 timestamp, when using mysql terminal I get utc+11. This causes havoc for my views for example when they operate on datetimes within functions that I have made that assume a +11.

@gfan8w
Copy link

gfan8w commented Mar 30, 2022

Here is my dirty way to write local timezone datetime.

// Assume local timezone is +08:00

// FIXME Local+naive_local()=+16 timezone!!!
let datetime = chrono::Local::now().naive_local();
sqlx::query("INSERT INTO trades (created_at) VALUES (?);")
    .bind(datetime)
    .execute(&pool)
    .await
    .unwrap();

MySqlConnection(0) + NaiveDateTime(+16) = local timezone(+8)

This is best answer!!!!

@gudaoxuri
Copy link

How is this going? When selecting NOW() on sqlx I get a utc+0 timestamp, when using mysql terminal I get utc+11. This causes havoc for my views for example when they operate on datetimes within functions that I have made that assume a +11.

c

See #263 (comment) for a potential solution to amend what SQLx is doing on start-up or for general purpose execution.

Another idea is to allow setting system variables via query parameters. The Go MySQL driver allows this.

mysql://root:password@localhost/db?time_zone=SYSTEM
mysql://root:password@localhost/db?time_zone=%27Europe%2FParis%27

Currently, there is no good answer here. #263 is probably a minimum to fix issues like this.

Setting a session-level timezone via uri seems generic and reasonable.

@moom-en
Copy link

moom-en commented Jul 30, 2024

I also encountered this problem, now I get the time all through rust

@dojiong
Copy link

dojiong commented Aug 9, 2024

see #3418

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

No branches or pull requests

9 participants