uliweb.contrib.timezone 現行版本還可以發揮功能嗎?

1 view
Skip to first unread message

SeedSeek

unread,
Feb 25, 2017, 3:38:45 AM2/25/17
to Uliweb
OS: Linux
uliweb: github 提交時間 Feb 19, 2017 
settings.ini
    TIME_ZONE = 'GMT +8'
    LOCAL_TIME_ZONE = 'GMT +0'

使用 Model.to_dict() 不會對 datetime 做轉換。 to_dict() 本身不支持timezone 轉換?



limodou

unread,
Feb 26, 2017, 7:34:01 AM2/26/17
to uliweb
目前to_dict()直接使用 get_value_for_datastore() 来获得数据,它只是把保存数据库的值直接取出来,不会进行转换。可以使用

from uliweb.utils import date
date.to_local(d)

来进行转换。如果方便,还可以考虑在to_dict()上加一个to_local的参数,如果为True,则自动转成本地时区,这个方式如何?不过使用时区,date和time就不合适了,可能要使用datetime
> --
> -- ----
> Project : https://github.com/limodou/uliweb
> doc : http://limodou.github.com/uliweb-doc
> ---
> You received this message because you are subscribed to the Google Groups
> "Uliweb" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to uliweb+un...@googlegroups.com.
> To post to this group, send email to uli...@googlegroups.com.
> Visit this group at https://groups.google.com/group/uliweb.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/uliweb/1914a271-4abb-4c7c-895b-602e7ff6f446%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou

cnidance

unread,
Feb 28, 2017, 2:21:48 AM2/28/17
to uli...@googlegroups.com
這是我做的修改,供參考
應用方法,以GMT+8 為例
.to_dict(**{'tz':['pytz', 'Etc/GMT-8'], })
.to_dict(**{'tz':{0:'pytz', 1:'Etc/GMT-8'}, })
.to_dict(**{'tz':['internal', 'GMT+8'], })
.to_dict(**{'tz':{0:'internal', 1:'GMT+8'}, })



    def to_dict(self, fields=None, convert=True, manytomany=False, **kw):
        d = {}
        fields = fields or []
        for k, v in self.properties.items():
            if fields and not k in fields:
                continue
            if not isinstance(v, ManyToMany):
                if convert:
                    t = v.get_value_for_datastore(self)
                    d[k] = self.field_str(t, **kw)
                else:
                    t = getattr(self, k)
                    d[k] = t
            else:
                if manytomany:
                    d[k] = getattr(self, v._lazy_value(), [])
        return d
    
    def field_str(self, v, strict=False, **kw):
        from uliweb.utils.date import to_datetime, to_local
        if v is None:
            if strict:
                return ''
            return v
        if isinstance(v, datetime.datetime):
            if kw.get('tz'):
                if isinstance(kw['tz'], int):
                    v = to_local( to_datetime(v) )
                elif isinstance(kw['tz'], (list, dict)):
                    if kw['tz'][0] == 'internal':
                        v = to_local( v,  kw['tz'][1])
                    if kw['tz'][0] == 'pytz':
                        import pytz
                        tz_new = pytz.timezone( kw['tz'][1] )
                        v = v.astimezone(tz_new)
            return v.strftime('%Y-%m-%d %H:%M:%S')








~Ten ~

2017-02-26 20:33 GMT+08:00 limodou <lim...@gmail.com>:
目前to_dict()直接使用 get_value_for_datastore() 来获得数据,它只是把保存数据库的值直接取出来,不会进行转换。可以使用

from uliweb.utils import date
date.to_local(d)

来进行转换。如果方便,还可以考虑在to_dict()上加一个to_local的参数,如果为True,则自动转成本地时区,这个方式如何?不过使用时区,date和time就不合适了,可能要使用datetime

On Sat, Feb 25, 2017 at 4:38 PM, SeedSeek <frt5...@gmail.com> wrote:
> OS: Linux
> uliweb: github 提交時間 Feb 19, 2017
> settings.ini
>     TIME_ZONE = 'GMT +8'
>     LOCAL_TIME_ZONE = 'GMT +0'
>
> 使用 Model.to_dict() 不會對 datetime 做轉換。 to_dict() 本身不支持timezone 轉換?
>
>
>
> --
> -- ----
> Project : https://github.com/limodou/uliweb
> doc : http://limodou.github.com/uliweb-doc
> ---
> You received this message because you are subscribed to the Google Groups
> "Uliweb" group.
> To unsubscribe from this group and stop receiving emails from it, send an

> To post to this group, send email to uli...@googlegroups.com.
> Visit this group at https://groups.google.com/group/uliweb.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/uliweb/1914a271-4abb-4c7c-895b-602e7ff6f446%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou
--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+unsubscribe@googlegroups.com.

To post to this group, send email to uli...@googlegroups.com.
Visit this group at https://groups.google.com/group/uliweb.

Chunlin Zhang

unread,
Feb 28, 2017, 8:14:14 PM2/28/17
to uli...@googlegroups.com
不如提一个pull request上去得了

limodou

unread,
Mar 2, 2017, 10:24:19 AM3/2/17
to uliweb
还需要不同的模块啊。我觉得还是传一般参数更好,如:

do_dict(timezone_module='internal', timezone=None)

On Tue, Feb 28, 2017 at 3:21 PM, cnidance <frt5...@gmail.com> wrote:
>> > email to uliweb+un...@googlegroups.com.
>> > To post to this group, send email to uli...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/uliweb.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/uliweb/1914a271-4abb-4c7c-895b-602e7ff6f446%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> I like python!
>> UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
>> UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
>> My Blog: http://my.oschina.net/limodou
>>
>> --
>> -- ----
>> Project : https://github.com/limodou/uliweb
>> doc : http://limodou.github.com/uliweb-doc
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Uliweb" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to uliweb+un...@googlegroups.com.
>> To post to this group, send email to uli...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/uliweb.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/uliweb/CADCtg99jYTTearEgNweCpBxXHzByfnxcjuwyhe8xsXYyba4JsQ%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> -- ----
> Project : https://github.com/limodou/uliweb
> doc : http://limodou.github.com/uliweb-doc
> ---
> You received this message because you are subscribed to the Google Groups
> "Uliweb" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to uliweb+un...@googlegroups.com.
> To post to this group, send email to uli...@googlegroups.com.
> Visit this group at https://groups.google.com/group/uliweb.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/uliweb/CAHLXMV9HQy3e-MXPn8tFtuViiQucN7gQ0Zn1GEXjA1D1xXZuWA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages