You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将带符号整数转换为以公元1年为基准的日期(含BC日期)

带符号整数转日期(支持BC年份)

基准对应规则

先明确整数与日期的映射关系:

  • x=0 → 1/1/1(公元1年1月1日)
  • x>0 → 公元1年1月1日之后的日期
  • x<0 → 公元1年1月1日之前的BC日期(注意:无公元0年,x=-1直接对应公元前1年12月31日)

原正整数逻辑优化

先将你原有的正整数处理逻辑优化得更清晰:

function int_to_date(x):
    day = 1
    month = 1
    year = 1

    if x > 0:
        while x > 0:
            # 获取当前年份的总天数
            current_year_days = 366 if is_leap(year) else 365
            if x >= current_year_days:
                x -= current_year_days
                year += 1
            else:
                # 按月份拆分天数
                days_per_month = [31,28,31,30,31,30,31,31,30,31,30,31]
                if is_leap(year):
                    days_per_month[1] = 29
                for idx in range(12):
                    if x < days_per_month[idx]:
                        month = idx + 1
                        day = x + 1
                        x = 0
                        break
                    x -= days_per_month[idx]
                break

负数输入(BC日期)处理逻辑

针对负数输入,核心是处理「无公元0年」的问题,以及反向计算年份和月日:

elif x < 0:
        abs_x = -x
        bc_year = 0  # 用0标记公元前1年,后续转换为1 BC
        while abs_x > 0:
            # 判断当前BC年份的闰年
            abs_bc_year = abs(bc_year)
            is_leap = False
            if abs_bc_year % 4 == 0:
                if abs_bc_year % 100 != 0 or abs_bc_year % 400 == 0:
                    is_leap = True
            current_year_days = 366 if is_leap else 365
            
            if abs_x > current_year_days:
                abs_x -= current_year_days
                bc_year -= 1  # 进入更早的BC年份,比如0→-1对应公元前2年
            else:
                # 转换为当年的第几天(从年初开始计数)
                day_of_year = current_year_days - abs_x + 1
                # 拆分月日
                days_per_month = [31,28,31,30,31,30,31,31,30,31,30,31]
                if is_leap:
                    days_per_month[1] = 29
                cumulative = 0
                for idx in range(12):
                    if day_of_year <= cumulative + days_per_month[idx]:
                        month = idx + 1
                        day = day_of_year - cumulative
                        abs_x = 0
                        break
                    cumulative += days_per_month[idx]
                break
        # 转换为BC年份格式
        output_year = f"{abs(bc_year)} BC"
    else:
        output_year = str(year)
    
    return f"{day}/{month}/{output_year}"

辅助闰年判断函数

# 公元后年份闰年判断
function is_leap(year):
    if year % 4 != 0:
        return False
    elif year % 100 != 0:
        return True
    else:
        return year % 400 == 0

关键注意事项

  • 无公元0年:这是BC日期处理的核心,x=-1直接对应公元前1年12月31日,无需经过不存在的公元0年。
  • BC年份闰年规则:与公元后一致,能被4整除但非整百年,或能被400整除的年份为闰年,比如公元前1年(bc_year=0)是闰年。
  • 反向天数转换:负数输入是从公元1年1月1日往前数,因此在BC年份内需要将偏移量转换为「从年初开始的天数」,再用常规月日拆分逻辑。

内容的提问来源于stack exchange,提问作者Bailey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 15:40:21