date.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:wuye.vim 作者: zhaoyingnan911 项目源码 文件源码
def ustrftime(somedate, fmt='%Y-%m-%d'):
    """like strftime, but returns a unicode string instead of an encoded
    string which may be problematic with localized date.
    """
    if sys.version_info >= (3, 3):
        # datetime.date.strftime() supports dates since year 1 in Python >=3.3.
        return somedate.strftime(fmt)
    else:
        try:
            if sys.version_info < (3, 0):
                encoding = getlocale(LC_TIME)[1] or 'ascii'
                return unicode(somedate.strftime(str(fmt)), encoding)
            else:
                return somedate.strftime(fmt)
        except ValueError:
            if somedate.year >= 1900:
                raise
            # datetime is not happy with dates before 1900
            # we try to work around this, assuming a simple
            # format string
            fields = {'Y': somedate.year,
                      'm': somedate.month,
                      'd': somedate.day,
                      }
            if isinstance(somedate, datetime):
                fields.update({'H': somedate.hour,
                               'M': somedate.minute,
                               'S': somedate.second})
            fmt = re.sub('%([YmdHMS])', r'%(\1)02d', fmt)
            return unicode(fmt) % fields
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号