Contributors mailing list archives

contributors@odoo-community.org

Browse archives

Avatar

Re: A module to add methods to convert datetime between UTC and user's timezone

by
Quartile Limited, Yoshi Tashiro
- 06/01/2021 07:25:00
Thanks Graeme and Dominique.  What I'm looking for is a collection of methods that are not covered in Date and Datetime classes of vanilla Odoo.  For example:
    # Convert datetime object to date string
    def _convert_to_user_date_string(selfdatetimehours):
        user_datetime = datetime + relativedelta(hours=hours)
        return fields.Date.to_string(fields.Date.to_date(user_datetime))

    # Convert date string with timestamp to datetime object
    def _convert_to_user_datetime_string(selfdatetimestamphours):
        datetime_value = fields.Datetime.from_string("{} {}".format(date, timestamp))
        return fields.Datetime.to_string(datetime_value - relativedelta(hours=hours))

    # Return the time difference (in hours), e.g. if user's timezone is in
    # GMT+9 this method will return 9.0
    def _get_tz_hours_diff(self):
        tz = timezone(self.env.user.tz or "UTC")
        current_time = fields.Datetime.now()
        user_time = pytz.utc.localize(current_time).astimezone(tz)
        utc_time = timezone("UTC").localize(current_time)
        hours_diff = (
            (user_time.utcoffset() - utc_time.utcoffset()) / timedelta(minutes=1) / 60
        )
        return hours_diff

I vaguely remember seeing somewhere someone extending Date and Datetime in a somewhat similar manner.  If there is nothing under OCA we may create a module and make a PR.

-- 
Yoshi Tashiro


On Wed, Jan 6, 2021 at 2:12 PM Dominique k <dominique.k@elico-corp.com.sg> wrote:
oh... will check that. thanks a lot

On Wed, 6 Jan 2021 at 12:42 PM, Graeme Gellatly <gdgellatly@gmail.com> wrote:
It is built in to odoo and sometimes uses babel under hood.

For today jobs fields.Date.context_today(record with tz context, optional timestamp) is my general go to available in views as context_today.

odoo.tools.format_datetime or something like that does it too for datetime plus locale specific representation as string.

There is some more. Check fields.Date* and odoo.tools 

In record rules it is tons harder but if you have the patience you can handroll a very ugly rule using time module.

Regards from UTC+13.


On Wed, 6 Jan 2021, 5:22 pm Dominique k, <dominique.k@elico-corp.com.sg> wrote:
i would be curious if there is one, or contribute for developing one

UTC --> user timezone is a real real pain to develop. particularly when trying to get "today" jobs

Regards,
Dominique


On Wed, 6 Jan 2021 at 11:42, Yoshi Tashiro <tashiro@quartile.co> wrote:
I think I've seen somewhere under OCA a module to add generic methods to convert datetime between UTC and user's timezone, but I somehow can't find it now.  A pointer would be appreciated.

-- 
Yoshi Tashiro

_______________________________________________
Mailing-List: https://odoo-community.org/groups/contributors-15
Post to: mailto:contributors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe

_______________________________________________
Mailing-List: https://odoo-community.org/groups/contributors-15
Post to: mailto:contributors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe

_______________________________________________
Mailing-List: https://odoo-community.org/groups/contributors-15
Post to: mailto:contributors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe

--
Dominique KON-SUN-TACK  [Project Manager]
Odoo Gold Partner, best Odoo Partner 2014 for APAC

Mobile: + 65 8502 2399
Skype: dominique_elico


_______________________________________________
Mailing-List: https://odoo-community.org/groups/contributors-15
Post to: mailto:contributors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe

Reference