Skip to content

auto_now and auto_now_add to use local timezone #458

Open

Description

@mojimi

Hello, my team just ended up realizing that one of our bugs was due to tortoise using UTC time on auto_now and auto_now_add.

Could the timezone it uses be configurable or maybe just use the local server time?

Thanks

Activity

  1. long2ice commented on Jul 13, 2020

    @long2ice
    Member
  2. mojimi commented on Jul 13, 2020

    @mojimi
    Author

    Now it get current time from datetime.datetime.utcnow(), see https://github.com/tortoise/tortoise-orm/blob/develop/tortoise/fields/data.py#L310

    Yes! I'm aware, but utcnow() does not return the server time, it specifically returns UTC time

  3. maxim-s-barabash commented on Jul 14, 2020

    @maxim-s-barabash

    @mojimi, I recommend storing datetime in the database always in UTC, and lead to local time in the application and even better on the client-application. IMHO current implementation is correct

  4. sunshineinwater commented on Jul 18, 2020

    @sunshineinwater

    数据库存时间最好就是存utc时间,不然多端调用时候很容易出意想不到的bug

  5. fangaofeng commented on Aug 5, 2020

    @fangaofeng

    但是数据库是支持timezone的,此时应该符合数据库格式。比如postgresql的datatime可以选择是否timezone,这个timezone会记录对应的时区。

  6. fangaofeng commented on Aug 5, 2020

    @fangaofeng

    django reffer:
    DateField.auto_now¶
    Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. Note that the current date is always used; it’s not just a default value that you can override.

    The field is only automatically updated when calling Model.save(). The field isn’t updated when making updates to other fields in other ways such as QuerySet.update(), though you can specify a custom value for the field in an update like that.

    DateField.auto_now_add¶
    Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it’s not just a default value that you can override. So even if you set a value for this field when creating the object, it will be ignored. If you want to be able to modify this field, set the following instead of auto_now_add=True:

    For DateField: default=date.today - from datetime.date.today()
    For DateTimeField: default=timezone.now - from django.utils.timezone.now()

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions