简体   繁体   English

日期时间年份超出范围

[英]datetime year out of range

I passed filenames such as abcef-1591282803 into this function and the function worked fine:我将诸如abcef-1591282803文件名传递到此函数中,并且该函数运行良好:

def parse_unixtimestamp(filename):
        ts = re.findall(r'\d{10}', filename)[0]
        return ts

However, then I modified the function to this so the it also works when the timestamp is of 13 digits instead of 10. file20-name-1591282803734但是,然后我修改了这个函数,所以当时间戳是 13 位而不是 10 位时它也可以工作。 file20-name-1591282803734

def parse_unixtimestamp(filename):
        ts = re.findall(r'\d{13}|\d{10}', filename)[0]
        return ts

It didn't throw an error until here.直到这里才抛出错误。 But in case of 13 digits, I get ts values like this 1591282803734 .但在 13 位数字的情况下,我得到 ts 值,如1591282803734 Now when I pass this value to this function for year :现在,当我这个值传递给这个函数的year

def get_dateparts(in_unixtimestamp):
    dt_object = datetime.fromtimestamp(int(in_unixtimestamp))

    date_string = dt_object.strftime("%Y-%m-%d")
    year_string = dt_object.strftime("%Y")
    month_string = dt_object.strftime("%m")
    day_string = dt_object.strftime("%d")
    logger.info(f'year_string: {year_string}')
    result = {"date_string": date_string, "year_string": year_string, "month_string": month_string,
              "day_string": day_string}

    return result

I get an error that:我收到一个错误:

year 52395 is out of range

I wouldn't get this error when the unixtimestamp passed into parse_unixtimestamp Is only 10 digits.当 unixtimestamp 传递给parse_unixtimestamp只有 10 位数字时,我不会收到此错误。 How can I modify this function such that it works in both cases?如何修改此函数以使其在两种情况下都有效?

datetime.fromtimestamp requires you to supply the time in milli-seconds. datetime.fromtimestamp要求您提供以毫秒为单位的时间。 If your string has 13 digits instead of 9 (ie it is nanoseconds), you should be using:如果您的字符串有 13 位数字而不是 9 位(即纳秒),您应该使用:

>>> datetime.fromtimestamp(1591282803734/1000)
datetime.datetime(2020, 6, 4, 11, 0, 3, 734000)

To do this in your function, you could check the length of your timestamp before calling datetime.fromtimestamp .要在您的函数中执行此操作,您可以在调用datetime.fromtimestamp之前检查时间戳的长度。 Change your function as follows:更改您的功能如下:

def get_dateparts(in_unixtimestamp):
    if len(in_unixtimestamp) == 13:
        dt_object = datetime.fromtimestamp(int(in_unixtimestamp)/1000)
    else:
        dt_object = datetime.fromtimestamp(int(in_unixtimestamp))

    date_string = dt_object.strftime("%Y-%m-%d")
    year_string = dt_object.strftime("%Y")
    month_string = dt_object.strftime("%m")
    day_string = dt_object.strftime("%d")

    logger.info(f'year_string: {year_string}')

    result = {"date_string": date_string, 
              "year_string": year_string, 
              "month_string": month_string,
              "day_string": day_string}

    return result

>>> get_dateparts(parse_unixtimestamp("abcef-1591282803"))
{'date_string': '2020-06-04',
 'year_string': '2020',
 'month_string': '06',
 'day_string': '04'}

>>> get_dateparts(parse_unixtimestamp("file20-name-1591282803734"))
{'date_string': '2020-06-04',
 'year_string': '2020',
 'month_string': '06',
 'day_string': '04'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM