添加时间戳的讨论和廉价
This commit is contained in:
parent
46a2ec185a
commit
d8ab9980e2
|
@ -13,4 +13,6 @@
|
||||||
|
|
||||||
| 文章标题 | 简要介绍 |
|
| 文章标题 | 简要介绍 |
|
||||||
|---|---|
|
|---|---|
|
||||||
| [Python With 关键字和语句](https://www.ossez.com/t/python-with/13387) | 针对 with 关键字的使用进行了一些解答。 |
|
| [Python With 关键字和语句](https://www.ossez.com/t/python-with/13387) | 针对 with 关键字的使用进行了一些解答 |
|
||||||
|
| [Python DataTime 日期处理](https://www.ossez.com/t/python-datatime/13388) | 关于日期函数处理的一些小的介绍和讨论 |
|
||||||
|
| [Python 日期格式和时间以及当前时间和时间戳](https://www.ossez.com/t/python/13389) | 关于时间和当前时间的时间戳的获取 |
|
||||||
|
|
|
@ -1,39 +1,24 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Python script to Process DateTime
|
# Python script to Process DateTime
|
||||||
# Author - yucheng.hu@ingith.com
|
# Author - HoneyMoose
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
|
||||||
|
|
||||||
import ruamel.yaml as yaml
|
|
||||||
from dateutil.relativedelta import relativedelta
|
|
||||||
|
|
||||||
json_filename = 'resources/black_rock_test.json'
|
json_filename = 'resources/black_rock_test.json'
|
||||||
yaml_filename = 'resources/black_rock_test.yaml'
|
|
||||||
|
|
||||||
ELIGIBLE_FOR_RETIREMENT = 'eligible_for_retirement'
|
today = datetime.date.today()
|
||||||
|
print("Today's date:", today)
|
||||||
|
|
||||||
|
# 从字符串创建日期对象
|
||||||
|
date_user = datetime.datetime.strptime('1/1/2021', '%m/%d/%Y')
|
||||||
|
print("格式化后的日期对象:", date_user)
|
||||||
|
|
||||||
# Get Difference Years
|
# 日期对象通过对格式化输出
|
||||||
def get_age(data_input):
|
print("日期对象格式化输出 1 =", today.strftime("%d/%m/%Y"))
|
||||||
date_user = datetime.datetime.strptime(data_input, '%m/%d/%Y')
|
print("日期对象格式化输出 2 =", today.strftime("%B %d, %Y"))
|
||||||
date_current = datetime.datetime.now()
|
print("日期对象格式化输出 3 =", today.strftime("%m/%d/%y"))
|
||||||
|
print("日期对象格式化输出 4 =", today.strftime("%b-%d-%Y"))
|
||||||
|
|
||||||
time_difference = relativedelta(date_current, date_user)
|
# 当前时间戳
|
||||||
return time_difference.years
|
now = datetime.datetime.now()
|
||||||
|
print("当前时间戳:", now)
|
||||||
|
|
||||||
# Read and process JSON
|
|
||||||
with open(json_filename) as json_file:
|
|
||||||
data = json.load(json_file)
|
|
||||||
data_bod = data['date_of_birth']
|
|
||||||
|
|
||||||
if get_age(data_bod) >= 65:
|
|
||||||
data[ELIGIBLE_FOR_RETIREMENT] = True
|
|
||||||
else:
|
|
||||||
data[ELIGIBLE_FOR_RETIREMENT] = False
|
|
||||||
|
|
||||||
# Write to YAML
|
|
||||||
with open(yaml_filename, 'w') as yaml_file:
|
|
||||||
yaml.dump(data, yaml_file, allow_unicode=True)
|
|
||||||
|
|
Loading…
Reference in New Issue