【问题标题】:how can I use the epoch我怎样才能使用时代
【发布时间】:2021-08-15 15:31:12
【问题描述】:

我需要打印出“你的生日是 2001 年 3 月 31 日(a 年 b 天 c 小时 d 分 e 秒前)。”

我创建输入

birth_day = int(input("your birth day?"))

birth_month = int(input("your birth month?"))

birth_year = int(input("your birth year?"))

我明白

print("your birthday is"+(birth_day)+(birth_month)+(birth_year)) 打印第一句。但我遇到了第二个问题,即这部分(a 年,b 天,c 小时,d 分钟和 e 秒前)

我想我必须使用“时代”

并使用一些不同的,如下所示

year_sec=365*60*60*24
day_sec=60*60*24
hour_sec=60*60
min_sec=60

计算自 1970 年 1 月 1 日 00:00:00 UTC 以来日期的秒数:

import datetime, time
t = datetime.datetime(2001, 3, 31, 0, 0)
time.mktime(t.timetuple())

985960800.0

谁能解决我的问题?

非常感谢

【问题讨论】:

标签: python-3.x


【解决方案1】:

编辑:请参阅上面的the thread kaya3 mentioned 中的this answer,以获得更一致可靠的方式来做同样的事情。我将在下面留下我的原始答案,因为它有助于理解如何思考这个问题,但请注意,由于Gregorian calendar 的怪癖,我下面的答案可能会在棘手的情况下搞砸,特别是:

能被 4 整除的每一年都是闰年,但能被 100 整除的年份除外,但如果这些百年年能被 400 整除,则它们就是闰年。例如,1700 年、1800 年、和 1900 年不是闰年,但 1600 年和 2000 年是。


原始答案:

您可以尝试使用time 模块:

import time
import datetime

def main(ask_for_hour_and_minute, convert_to_integers):
    year, month, day, hour, minute = ask_for_birthday_info(ask_for_hour_and_minute)
    calculate_time_since_birth(year, month, day, hour, minute, convert_to_integers)


def ask_for_birthday_info(ask_for_hour_and_minute):
    birthday_year = int(input('What year were you born in?\n'))
    birthday_month = int(input('What month were you born in?\n'))
    birthday_day = int(input('What day were you born on?\n'))
    if ask_for_hour_and_minute is True:
        birthday_hour = int(input('What hour were you born?\n'))
        birthday_minute = int(input('What minute were you born?\n'))
    else:
        birthday_hour = 0   # set to 0 as default
        birthday_minute = 0 # set to 0 as default
    return (birthday_year, birthday_month, birthday_day, birthday_hour, birthday_minute)

def calculate_time_since_birth(birthday_year, birthday_month, birthday_day, birthday_hour, birthday_minute, convert_to_integers):
    year   = 31557600 # seconds in a year
    day    = 86400    # seconds in a day
    hour   = 3600     # seconds in a hour
    minute = 60       # seconds in a minute
    # provide user info to datetime.datetime()
    birthdate = datetime.datetime(birthday_year, birthday_month, birthday_day, birthday_hour, birthday_minute)
    birthdate_tuple = time.mktime(birthdate.timetuple())
    # figure out how many seconds ago birth was
    seconds_since_birthday = time.time() - birthdate_tuple
    # start calculations
    years_ago   = seconds_since_birthday // year
    days_ago    = seconds_since_birthday // day % 365
    hours_ago   = seconds_since_birthday // hour % 24
    minutes_ago = seconds_since_birthday // minute % 60
    seconds_ago = seconds_since_birthday % minute
    # convert calculated values to integers if convert_to_integers is True
    if convert_to_integers is True:
        years_ago   = int(years_ago) 
        days_ago    = int(days_ago) 
        hours_ago   = int(hours_ago) 
        minutes_ago = int(minutes_ago) 
        seconds_ago = int(seconds_ago) 
    # print calculations
    print(f'Your birthday was {years_ago} years, {days_ago}, days, {hours_ago} hours, {minutes_ago} minutes, {seconds_ago} seconds ago.')

# to ask for just the year, month, and day
main(False, False)
# to ask for just the year, month, and day AND convert the answer to integer values
main(False, True)
# to ask for just the year, month, day, hour, and minute
main(True, False)
# to ask for just the year, month, day, hour, and minute AND convert the answer to integer values
main(True, True)

尝试使用描述性变量名称,以便变量有意义,但操作员可能需要一些解释:

10 // 3 # the // operator divides the numerator by the denominator and REMOVES the remainder, so answer is 3

10 % 3 # the % operator divides the numerator by the denominator and RETURNS the remainder, so the answer is 1

理解了操作符之后,剩下的代码应该就有意义了。为了清楚起见,让我们来看看它

  1. 通过在ask_for_birthday_info() 函数中询问用户的信息来创建birthdate
  2. 将用户提供的信息提供给calculate_time_since_birth()函数
  3. 将birthdate 转换为元组并将其存储在birthdate_tuple
  4. 计算自生日以来经过了多少秒并将其存储在seconds_since_birthday
  5. 通过将seconds_since_birthday 除以year 中的秒数来计算自生日以来已经过去了多少年
  6. 通过将seconds_since_birthday 除以day 中的秒数并保留最近的 365 天(即% 365 days_ago)
  7. 通过将seconds_since_birthday 除以hour 中的秒数并保留最近的 24 小时(即% 24 hours_ago)
  8. 通过将seconds_since_birthday 除以minute 中的秒数并保留最近的 60 分钟(即% 60 在minutes_ago)
  9. 除以seconds_since_birthday 并保留最近的 60 秒(即seconds_ago 中的% 60),计算出自生日以来经过了多少秒

然后,我们只需要打印结果:

print(f'Your birthday was {years_ago} years, {days_ago}, days, {hours_ago} hours, {minutes_ago} minutes, {seconds_ago} seconds ago.')

# if you're using a version of python before 3.6, use something like
print('Your birthday was ' + str(years_ago) + ' years, ' + str(days_ago) + ' days, ' + str(hours_ago) + ' hours, ' + str(minutes_ago) + ' minutes, ' + str(seconds_ago) + ' seconds ago.')

最后,您可以添加一些错误检查以确保用户输入有效信息,这样如果他们说他们出生在月份 15 或月份 -2,您的程序会告诉用户他们提供了无效信息回答。例如,您可以在从用户那里获取生日信息之后,但在调用calculate_time_since_birth() 函数之前执行以下操作:

if not (1 <= month <= 12):
    print('ERROR! You provided an invalid month!')
    return
if not (1 <= day <= 31):
    # note this isn't a robust check, if user provides February 30 or April 31, that should be an error - but this won't catch that
    # you'll need to make it more robust to catch those errors
    print('ERROR! You provided an invalid day!')
    return
if not (0 <= hour <= 23):
    print('ERROR! You provided an invalid hour!')
    return
if not (0 <= minute <= 59):
    print('ERROR! You provided an invalid minute!')
    return
if not (0 <= second <= 59):
    print('ERROR! You provided an invalid second!')
    return

【讨论】:

  • 非常感谢。我将尝试使用此代码。其实这是我第一次学习python。非常感谢您的帮助。 :)
  • 我还有一个问题。从此代码birthdate = datetime.datetime(2001, 3, 31, 0, 0) 我想输入日期而不是 2001,3,31,0,0 如何在此代码中输入日期?我只使用birthdate=datetime.datetime(input1,input2,input3,0,0) 吗?
  • 刚刚编辑了答案以询问用户该信息 - 我认为这应该可行。为了回答您的问题,我认为您可以使用datetime.datetime(input1,input2,input3,0,0) 询问信息,但它会变得混乱,所以我将所有这些信息放在更新答案中的ask_for_birthday_info() 函数中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
相关资源
最近更新 更多