Get default datetime value for now in Tryton

100 views
Skip to first unread message

Jordi Esteve

unread,
Mar 19, 2014, 3:26:11 PM3/19/14
to tryto...@googlegroups.com
There is a way to get the default datetime value for now in Tryton?

To get a default date for today there is today() method in Date class.
It is overwritten in company module to take into account the timezone of
the company.

But I am missing a now() method to get the a default datetime value that
also takes into account the timezone of the company. It will be useful
to fill a default datetime field, for example. Maybe I must write an issue.

--
Jordi Esteve
Consultor Zikzakmedia SL
jes...@zikzakmedia.com
Mòbil 679 170 693

Zikzakmedia SL
Dr. Fleming, 28, baixos
08720 Vilafranca del Penedès
Tel 93 890 2108

Raimon Esteve

unread,
Mar 19, 2014, 4:09:17 PM3/19/14
to tryto...@googlegroups.com

I
El 19/03/2014 20:29, "Jordi Esteve" <jes...@zikzakmedia.com> va escriure:


>
> There is a way to get the default datetime value for now in Tryton?
>
> To get a default date for today there is today() method in Date class. It is overwritten in company module to take into account the timezone of the company.
>
> But I am missing a now() method to get the a default datetime value that also takes into account the timezone of the company. It will be useful to fill a default datetime field, for example. Maybe I must write an issue.

It's older codereview and need to add timezone

http://codereview.tryton.org/507002/

Arguement about this topic:

https://groups.google.com/forum/m/#!msg/tryton-dev/-XjVVeQQ3XU/0q5q_VHM5IUJ

IMHO is necessary now method when like get date today and hour now.

Example with now and timezone

Company = Pool().get('company.company')

timezone = None
if Transaction().context.get('company'):
    company = Company(Transaction().context['company'])
    if company.timezone and pytz:
        timezone = pytz.timezone(company.timezone)
return datetime.now(timezone)

Cédric Krier

unread,
Mar 19, 2014, 5:07:33 PM3/19/14
to tryto...@googlegroups.com
On 19 Mar 21:09, Raimon Esteve wrote:
> I
> El 19/03/2014 20:29, "Jordi Esteve" <jes...@zikzakmedia.com> va escriure:
> >
> > There is a way to get the default datetime value for now in Tryton?
> >
> > To get a default date for today there is today() method in Date class. It
> is overwritten in company module to take into account the timezone of the
> company.
> >
> > But I am missing a now() method to get the a default datetime value that
> also takes into account the timezone of the company. It will be useful to
> fill a default datetime field, for example. Maybe I must write an issue.
>
> It's older codereview and need to add timezone
>
> http://codereview.tryton.org/507002/
>
> Arguement about this topic:
>
> https://groups.google.com/forum/m/#!msg/tryton-dev/-XjVVeQQ3XU/0q5q_VHM5IUJ
>
> IMHO is necessary now method when like get date today and hour now.

It is not. datetime.now() will return now in UTC which is always now in
any timezone. Tryton switched for 3.2 to good practice which is to
always work/store/manipulate UTC.
So adding a Date.now() that return now with an other timezone will just
create bad practice.

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Raimon Esteve

unread,
Mar 20, 2014, 3:55:26 AM3/20/14
to tryto...@googlegroups.com
It is not. datetime.now() will return now in UTC which is always now in
any timezone.

I think is not true. datetime.now() return current local date and time, not UTC (1).
 
Tryton switched for 3.2 to good practice which is to
always work/store/manipulate  UTC.
So adding a Date.now() that return now with an other timezone will just
create bad practice.

Great! I understand because we have to save the date in UTC!

* Save datetime in database -> UTC
* Show datetime field -> Use company timezone

Examples using datetime.now and timezone:

>>> from datetime import datetime
>>> timezone = None
>>> datetime.now(timezone)
datetime.datetime(2014, 3, 20, 8, 35, 15, 491292)

>>> import pytz
>>> timezone = pytz.timezone('Europe/Madrid')
>>> datetime.now(timezone)
datetime.datetime(2014, 3, 20, 8, 35, 50, 707283, tzinfo=<DstTzInfo 'Europe/Madrid' CET+1:00:00 STD>)

>>> timezone = pytz.timezone('America/New_York')
>>> datetime.now(timezone)
datetime.datetime(2014, 3, 20, 3, 36, 43, 27351, tzinfo=<DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>)

>>> timezone = pytz.timezone('UTC')
>>> datetime.now(timezone)
datetime.datetime(2014, 3, 20, 7, 41, 11, 347495, tzinfo=<UTC>)

I think best option is the last, pass UTC option and return a datetime.now() in UTC.

Thanks

(1) http://docs.python.org/2/library/datetime.html#datetime.datetime.now

--
Raimon Esteve
// Tryton y OpenERP en la nube: http://www.zzsaas.com
// Documentación: http://doc.zzsaas.com
www.zikzakmedia.com

Raimon Esteve

unread,
Mar 20, 2014, 4:52:19 AM3/20/14
to tryto...@googlegroups.com
2014-03-20 8:55 GMT+01:00 Raimon Esteve <res...@zikzakmedia.com>:
It is not. datetime.now() will return now in UTC which is always now in
any timezone.

I think is not true. datetime.now() return current local date and time, not UTC (1).

trytond force UTC. Ok, datetime.now() return UTC in trytond

http://hg.tryton.org/trytond/file/e0fa0ce19d7a/trytond/__init__.py#l10

Reply all
Reply to author
Forward
0 new messages