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 can I specify a time zone #335

Closed
Ailibert opened this issue Mar 27, 2020 · 13 comments · Fixed by #557
Closed

How can I specify a time zone #335

Ailibert opened this issue Mar 27, 2020 · 13 comments · Fixed by #557
Labels
enhancement New feature or request

Comments

@Ailibert
Copy link

await Tortoise.init(db_url="mysql://root:123456@127.0.0.1:3306/example",
                    modules={'models': ['models']}
                    )
@Ailibert Ailibert changed the title 我怎么去指定时区 How can I specify a time zone Mar 27, 2020
@grigi
Copy link
Member

grigi commented Mar 27, 2020

Ah... Somehow we haven't gotten to timezone-aware datetimes :-(

I can't find the ticket now, so I'm leaving this as the issue to work towards.

There was an abandoned attempt #103 but it appeared to have beem forgotten. Thank you for bringing it up again. This is important.

@grigi grigi added the enhancement New feature or request label Mar 27, 2020
long2ice added a commit that referenced this issue Nov 15, 2020
long2ice added a commit that referenced this issue Nov 16, 2020
@mookrs
Copy link

mookrs commented Nov 21, 2020

@long2ice I think this PR brings breaking change. If I use old config which has no timezone info, after executing Tortoise.init(), time in my program becomes confused. Here is my logs:

2020-11-21 13:25:45.098 | INFO     | uvicorn.main:serve:405 - Started server process [19978]
2020-11-21 13:25:45.098 | INFO     | uvicorn.lifespan.on:startup:22 - Waiting for application startup.
2020-11-21 05:25:47.902 | INFO     | app.core.events:connect_db:32 - Tortoise-ORM startup, connections: {'db1': <tortoise.backends.mysql.client.MySQLClient object at 0x10f8cac10>, 'db2': <tortoise.backends.mysql.client.MySQLClient object at 0x110946550>}
2020-11-21 05:25:47.904 | INFO     | uvicorn.lifespan.on:startup:34 - Application startup complete.
2020-11-21 05:25:47.906 | INFO     | uvicorn.main:startup:492 - Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

As above, setting TZ environment variable also affects other functions in my program.

@long2ice
Copy link
Member

Yes, so changelog said that, be careful to upgrade.

@mookrs
Copy link

mookrs commented Nov 21, 2020

I tend to this is a breaking change, maybe 0.17.0 version number will be more suitable. And the worst part is if I don't specified timezone, the default TZ environment variable also affects whole program, not only Tortoise ORM...

@long2ice
Copy link
Member

You are right, maybe default UTC timezone setted is not suitable, so in version 0.16.18 you can specify your local timezone to resolve that.

@mookrs
Copy link

mookrs commented Nov 21, 2020

OK, I have pined 0.16.17 to resolve the problem. To be more clear, here is the situation we are facing. Look forward to getting a better solution:

python
Python 3.8.2 (default, May 13 2020, 11:47:24)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2020, 11, 21, 14, 14, 50, 253867)
>>> os.environ['TZ'] = 'UTC'
>>> datetime.now()
datetime.datetime(2020, 11, 21, 6, 14, 58, 217979)

@long2ice
Copy link
Member

Read the complete docs maybe helpful, https://tortoise-orm.readthedocs.io/en/latest/timezone.html

@mookrs
Copy link

mookrs commented Nov 21, 2020

I said "here is the situation we are facing" means "you and me and Tortoise ORM"... I have already read manual and source code, fixed my case before I commenting this issue, thanks for your reminding. 😄

So... The problem is not I don't know how to use new params use_tz and timezone, or tortoise.timezone.now(), the problem is this PR made Tortoise ORM always set TZ environment variable and I think this is implicit behavior.

If I have time, I maybe commit a PR to fix this behavior. (But I think I have no time... 🤣)

@long2ice
Copy link
Member

Yes, set TZ globally is not good, I noticed that and updated it to TIMEZONE already, thanks!

@mookrs
Copy link

mookrs commented Nov 21, 2020

Appreciate it!

@Tayum
Copy link

Tayum commented Nov 25, 2020

@long2ice @mookrs sorry, any possibility I can get back to same behavior in 0.16.18 as in 0.16.17?
Even with use_tz set to False, I get tzinfo=<UTC> in my datetime fields in tests (using SQLite).

See example:

import datetime

from tortoise import fields, models


class DatetimeModel(models.Model):
    id = fields.IntField(pk=True, null=False)
    created_at = fields.DatetimeField(null=True)

await DatetimeModel.create(created_at=datetime.datetime.utcnow())

created = await DatetimeModel.get(id=1)

assert created.tzinfo is None  # Good on 0.16.17, fails on 0.16.18

PS: +1 to that it should be at least 0.17, not 0.16.18.

@long2ice
Copy link
Member

try set timezone with your local timezone, default is UTC

@Tayum
Copy link

Tayum commented Nov 25, 2020

@long2ice

But I don't need the timezone there at all, I want the returned datetimes to be timezone-naive 🤔

I want the default timezone to be absent at all, e.g. assert created.tzinfo is None.
I don't want it to be my local timezone, or any other.

UPD: see #569.

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

Successfully merging a pull request may close this issue.

5 participants