python時間文字列相互変換

2607 ワード

#              
from dateutil import parser
time_string = '20181012151855'
datetime_struct = parser.parse(time_string)
time_paid = datetime_struct.strftime('%Y-%m-%d %H:%M:%S')


#   0    
import time
temp_timestamp = time.mktime(
    time.strptime(time.strftime('%Y-%m-%d 00:00:00', time.localtime(time.time())),
                  '%Y-%m-%d %H:%M:%S'))
#      
timestamp = time.time()
timestring = '2016-12-21 10:22:56'  #           
timestamp = time.mktime(time.strptime(timestring, '%Y-%m-%d %H:%M:%S'))  # 1482286976.0


#     datetime  "2018-10-13 14:23:43.861477"
import datetime
#     datetime
now = datetime.datetime.now()
datetime_struct = datetime.datetime.fromtimestamp(timestamp)
#           /  / 
three_days_ago = datetime_struct + datetime.timedelta(days=-3)
one_week_ago = datetime_struct + datetime.timedelta(weeks=-1)
three_days_later = datetime_struct + datetime.timedelta(days=3)
# datetime     
ret_time = datetime_struct.strftime('%Y-%m-%d %H:%M:%S')


#       
now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
year = time.localtime().tm_year  # tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst


#         
cur_time = datetime.datetime.today()
day_num = cur_time.isoweekday()  #                 
monday = (cur_time - datetime.timedelta(days=day_num))
'''contacts = Zlh_list_contacts.objects.filter(create_time__range=(monday, now_time))'''


#       
from datetime import timedelta
from celery.schedules import crontab
def time_stamp(param):
    pub_time = str(datetime.datetime.strptime(str(param), '%Y-%m-%d %H:%M:%S'))
    local_time = str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))  #     
    d1 = datetime.datetime.strptime(pub_time, '%Y-%m-%d %H:%M:%S')
    d2 = datetime.datetime.strptime(local_time, '%Y-%m-%d %H:%M:%S')
    delta = d2 - d1  #     
    # days,seconds, microseconds, milliseconds, minutes,hours,weeks
    # if delta > datetime.timedelta(minutes=1):
    arg = {
        u'pay_detail       ': {
            "task": "djcelery_tasks.task.pay_detail_off",
            'schedule': timedelta(seconds=1),
            "args": (),
        },
        u'            ': {
            "task": "djcelery_tasks.task.change_service_status",
            'schedule': crontab(hour=u'10,14', minute=30),
            "args": (),
        },
    }
    return delta