当前位置: 首页>>代码示例>>Python>>正文


Python DateTime.localZone方法代码示例

本文整理汇总了Python中DateTime.DateTime.DateTime.localZone方法的典型用法代码示例。如果您正苦于以下问题:Python DateTime.localZone方法的具体用法?Python DateTime.localZone怎么用?Python DateTime.localZone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DateTime.DateTime.DateTime的用法示例。


在下文中一共展示了DateTime.localZone方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: strptime

# 需要导入模块: from DateTime.DateTime import DateTime [as 别名]
# 或者: from DateTime.DateTime.DateTime import localZone [as 别名]
def strptime(context, value):
    """given a string, this function tries to return a DateTime.DateTime object
    with the date formats from i18n translations
    """
    val = ""
    for fmt in ['date_format_long', 'date_format_short']:
        fmtstr = context.translate(fmt, domain='bika', mapping={})
        fmtstr = fmtstr.replace(r"${", '%').replace('}', '')
        try:
            val = _strptime(value, fmtstr)
        except ValueError:
            continue
        try:
            val = DateTime(*list(val)[:-6])
        except DateTimeError:
            val = ""
        if val.timezoneNaive():
            # Use local timezone for tz naive strings
            # see http://dev.plone.org/plone/ticket/10141
            zone = val.localZone(safelocaltime(val.timeTime()))
            parts = val.parts()[:-1] + (zone,)
            val = DateTime(*parts)
        break
    else:
        try:
            # The following will handle an rfc822 string.
            value = value.split(" +", 1)[0]
            val = DateTime(value)
        except:
            logger.warning("DateTimeField failed to format date "
                           "string '%s' with '%s'" % (value, fmtstr))
    return val
开发者ID:fqblab,项目名称:bika.lims,代码行数:34,代码来源:__init__.py

示例2: set

# 需要导入模块: from DateTime.DateTime import DateTime [as 别名]
# 或者: from DateTime.DateTime.DateTime import localZone [as 别名]
    def set(self, instance, value, **kwargs):
        """
        Check if value is an actual date/time value. If not, attempt
        to convert it to one; otherwise, set to None. Assign all
        properties passed as kwargs to object.
        """
        val = value
        if not value:
            val = None
        elif not isinstance(value, DateTime):
            for fmt in ['date_format_long', 'date_format_short']:
                fmtstr = instance.translate(fmt, domain='bika', mapping={})
                fmtstr = fmtstr.replace(r"${", '%').replace('}', '')
                try:
                    val = strptime(value, fmtstr)
                except ValueError:
                    continue
                try:
                    val = DateTime(*list(val)[:-6])
                except DateTimeError:
                    val = None
                if val.timezoneNaive():
                    # Use local timezone for tz naive strings
                    # see http://dev.plone.org/plone/ticket/10141
                    zone = val.localZone(safelocaltime(val.timeTime()))
                    parts = val.parts()[:-1] + (zone,)
                    val = DateTime(*parts)
                break
            else:
                logger.warning("DateTimeField failed to format date "
                               "string '%s' with '%s'" % (value, fmtstr))

        super(DateTimeField, self).set(instance, val, **kwargs)
开发者ID:Ammy2,项目名称:Bika-LIMS,代码行数:35,代码来源:datetimefield.py

示例3: result

# 需要导入模块: from DateTime.DateTime import DateTime [as 别名]
# 或者: from DateTime.DateTime.DateTime import localZone [as 别名]
    def result(self, date=None,
               use_ampm=False,
               starting_year=None,
               ending_year=None,
               future_years=None,
               minute_step=5):
        """Returns a dict with date information.
        """
        ptool = getToolByName(self.context, 'portal_properties')

        site_props = ptool.site_properties

        # Get the date format from the locale
        context = aq_inner(self.context)
        portal_state = getMultiAdapter((context, self.request), name=u'plone_portal_state')

        dates = portal_state.locale().dates

        timepattern = dates.getFormatter('time').getPattern()
        if 'a' in timepattern:
             use_ampm = True
        month_names = dates.getFormatter('date').calendar.months

        # 'id' is what shows up.  December for month 12. 
        # 'value' is the value for the form.
        # 'selected' is whether or not it is selected.

        default=0
        years=[]
        days=[]
        months=[]
        hours=[]
        minutes=[]
        ampm=[]
        now=DateTime()

        if isinstance(date, basestring):
            date=date.strip()
            if not date:
                date=None
            else:
                # Please see datecomponents.txt for an explanation of 
                # the next few lines. Also see #11423
                dateParts = date.split(" ")
                dateParts[0] = dateParts[0].replace("-", "/")
                date=' '.join(dateParts)

        if date is None:
            date=now
            default=1
        elif not isinstance(date, DateTime):
            try:
                date=DateTime(date)
            except (TypeError, DateTimeError):
                date=now
                default=1

        # Anything above PLONE_CEILING should be PLONE_CEILING
        if date.greaterThan(PLONE_CEILING):
            date = PLONE_CEILING

        # Represent the date in the local timezone
        local_zone = date.localZone(localtime(date.timeTime()))
        date = date.toZone(local_zone)

        # Get portal year range
        if starting_year is None:
            min_year = site_props.getProperty('calendar_starting_year', 1999)
        else:
            min_year = int(starting_year)
        if ending_year is None:
            if future_years is None:
                max_year = site_props.getProperty('calendar_future_years_available', 5) + now.year()
            else:
                max_year = int(future_years) + now.year()
        else:
            max_year = int(ending_year)

        # keeps the existing date if it's out of range
        if not default:
            if min_year > date.year():
                min_year = date.year()
            if max_year < date.year():
                max_year = date.year()

        year=int(date.strftime('%Y'))

        if default:
            years.append({'id': '--', 'value': '0000', 'selected': 1})
        else:
            years.append({'id': '--', 'value': '0000', 'selected': None})

        for x in range(min_year, max_year+1):
            d={'id': x, 'value': x, 'selected': None}
            if x==year and not default:
                d['selected']=1
            years.append(d)

        month=int(date.strftime('%m'))

#.........这里部分代码省略.........
开发者ID:rochecompaan,项目名称:plone.app.form,代码行数:103,代码来源:datecomponents.py

示例4: result

# 需要导入模块: from DateTime.DateTime import DateTime [as 别名]
# 或者: from DateTime.DateTime.DateTime import localZone [as 别名]
    def result(self, date=None,
               use_ampm=False,
               starting_year=None,
               ending_year=None,
               future_years=None,
               minute_step=5):
        """Returns a dict with date information.
        """
        ptool = getToolByName(self.context, 'portal_properties')
        site_props = getattr(ptool, 'site_properties', None)

        # Get the date format from the locale
        dates = self.request.locale.dates

        timepattern = dates.getFormatter('time').getPattern()
        if 'a' in timepattern:
            use_ampm = True
        month_names = dates.getFormatter('date').calendar.months

        # 'id' is what shows up.  December for month 12.
        # 'value' is the value for the form.
        # 'selected' is whether or not it is selected.

        default = 0
        years = []
        days = []
        months = []
        hours = []
        minutes = []
        ampm = []
        now = DateTime()

        if isinstance(date, basestring):
            date = date.strip()
            if not date:
                date = None
            else:
                # Please see datecomponents.txt for an explanation of
                # the next few lines. Also see #11423
                dateParts = date.split(" ")
                dateParts[0] = dateParts[0].replace("-", "/")
                date = ' '.join(dateParts)

        if date is None:
            date = now
            default = 1
        elif not isinstance(date, DateTime):
            try:
                date = DateTime(date)
            except (TypeError, DateTimeError):
                date = now
                default = 1

        # Anything above PLONE_CEILING should be PLONE_CEILING
        if date.greaterThan(PLONE_CEILING):
            date = PLONE_CEILING

        # Represent the date in the local timezone
        try:
            local_zone = date.localZone(localtime(date.timeTime()))
        except ValueError:
            # Dates before 1970 use a negative timeTime() value, which on
            # on some platforms are not handled well and lead to a ValueError.
            # In those cases, calculate the local timezone (which is DST based)
            # from the same date in the *current year* instead. This is better
            # than failing altogether!
            timeZoneDate = DateTime(localtime().tm_year, *date.parts()[1:])
            local_zone = date.localZone(localtime(timeZoneDate.timeTime()))
        date = date.toZone(local_zone)

        # Get portal year range
        min_year = starting_year
        if starting_year is None and site_props is not None:
            min_year = site_props.getProperty('calendar_starting_year', 1999)
        else:
            min_year = int(starting_year or 1999)
        if ending_year is None:
            if future_years is None and site_props is not None:
                future_years = site_props.getProperty('calendar_future_years_available', 5)
            else:
                future_years = int(future_years or 5)
            max_year = int(future_years) + now.year()
        else:
            max_year = int(ending_year)

        # keeps the existing date if it's out of range
        if not default:
            if min_year > date.year():
                min_year = date.year()
            if max_year < date.year():
                max_year = date.year()

        year = date.year()

        if default:
            years.append({'id': '--', 'value': '0000', 'selected': 1})
        else:
            years.append({'id': '--', 'value': '0000', 'selected': None})

        for x in range(min_year, max_year+1):
#.........这里部分代码省略.........
开发者ID:Goldmund-Wyldebeast-Wunderliebe,项目名称:Products.Archetypes,代码行数:103,代码来源:datecomponents.py


注:本文中的DateTime.DateTime.DateTime.localZone方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。