Comparison time 2 time difference between the date string: the distance from the current time (different time format)

Comparison time 2 time difference between the date string: the distance from the current time (different time format)

First, the background
may need to compare the time 2 time difference between the date string: the distance from the current time (different time format)
'20200401'
time now
time difference:
0 years 0 months 2 days 17 hours 46 minutes 16 seconds

Second, the analysis
using the Python,
write comparison function: gap_start2end ( '20200401', "the current date string", start_fomart = '% Y% m% d', end_fomart = '% Y-% m-% d% H:% M:% S ')
return time difference represents a combination of: ((0, 0, 2), (17, 46, 16))

Third, encapsulated function method

Time Import 
Import datetime 
Import Math 


# a same time from the date format 

def date2obj (date_str = '2015-04-07 19:11:21 ', format = '% Y-% m-% d% H:% M: S% '): 
    ' '' 
    date string transfer date objects 
    : param date_str: date string 
    : param format: date format string 
    : return: date object 
    d_obj.year output, 2015 
    d_obj.month output. 4 
    d_obj.day output 7 
    ' '' 
    t_str = DATE_STR 
    d_obj = datetime.datetime.strptime (t_str, the format) 

    return d_obj 


#. 1, sec interval S 
def time_seconds_span (start_date1, end_date2, fomart1 = '% Y-% m-% d% H:% M: S% ', fomart2 ='% Y-M-% D%% H:% M:% S '):  
    d1_obj = date2obj (START_DATE1, fomart1)
    d2_obj = date2obj (END_DATE2,fomart2)

    return (d2_obj - d1_obj).seconds


# 2, min minute intervals 
def time_minutes_span (start_date1, end_date2, fomart1 = '% Y-% m-% d% H:% M:% S', fomart2 = '% Y-% m-% d% H:% M:% S '): 
    d1_obj = date2obj (START_DATE1, fomart1) 
    d2_obj = date2obj (END_DATE2, fomart2) 
    seconds_span = (d2_obj - d1_obj) .seconds 
    # rounded down 
    mins_span = Math.floor (seconds_span / 60) 

    return mins_span 


# 3, h-hour interval 
def time_hours_span (start_date1, end_date2, fomart1 = '% Y-% m-% d% H:% M:% S', fomart2 = '% Y-% m-% d% H:% M :% S '): 
    d1_obj = date2obj(start_date1, fomart1)
    d2_obj = date2obj(end_date2, fomart2)
    seconds_span = (d2_obj - d1_obj).seconds
    # rounded down 
    hours_span = Math.floor (seconds_span / (60 * 60)) 

    return hours_span 


#. 4, D days interval. With or without an h, min, s, calculation, ignores h, min, s, counting only the number of days, months, and years.
date_days_span DEF (START_DATE1, END_DATE2, fomart1 = 'M-%%% Y-D', fomart2 = 'M-%%% Y-D'): 
    d1_obj = date2obj (START_DATE1, fomart1) 
    d2_obj = date2obj (END_DATE2, fomart2) 

    # just take a few days difference. Equivalent rounded down. 
    = days_span (d2_obj - d1_obj) .days 

    return days_span 


#. 5, May month The number of time intervals. With or without an h, min, s, calculation, ignores h, min, s, counting only the number of days, months, and years. 
date_months_span DEF (START_DATE1, END_DATE2, fomart1 = 'M-%%% Y-D', fomart2 = 'M-%%% Y-D'): 
    d1_obj = date2obj (START_DATE1, fomart1) 
    d2_obj = date2obj (END_DATE2, fomart2) 

    # rounded down. Just take a few days.
    = days_span (d2_obj - d1_obj) .days 
    months_span = Math.floor (days_span / 30) 

    return months_span 
    : param start_fomart: reading start time date string format

 
#. 6, the number of years year interval. With or without an h, min, s, calculation, ignores h, min, s, counting only the number of days, months, and years.
date_years_span DEF (START_DATE1, END_DATE2, fomart1 = 'M-%%% Y-D', fomart2 = 'M-%%% Y-D'): 
    d1_obj = date2obj (START_DATE1, fomart1) 
    d2_obj = date2obj (END_DATE2, fomart2) 

    # rounded down. Just take a few days. 
    = days_span (d2_obj - d1_obj) .days 
    years_span = Math.floor (days_span / (30 * 12 is)) 

    return years_span 


DEF gap_start2end (start_str, end_str, start_fomart = '% Y-M-% D%% H:% M:% S ', end_fomart ='% Y-M-% D%% H:% M:% S '): 
    ' '' 
    time comparison of two time difference 
    : param start_str: start date time string 
    : param end_str: time off date string 
    # exemplified: 
    '2009-02 -28 00:00:04 '
    : param end_fomart: cutoff time to read the date format string 
    : return: the number of times the difference array composition ((Y, m, D), (H, min, sec)) 
    y = years_gap
    '2010-03-01 01:02:03' 
    output ((1, 0, 6), (1, 1, 59)) 
    meaning: 1 in the time interval 0 months 6 days, 1 hour, 1 minute 59 seconds 

    # time pushback Description: 
    # sum_days = Y * 30 * 12 is + m * 30 + D 
    # sum_seconds = sum_days * 24 * 60 * 60 + H * 60 * 60 + min * 60 + sec 
    '' ' 

    difference number # of 
    years_gap = date_years_span (start_str, end_str, start_fomart, end_fomart ) 
    # of months difference 
    months_gap = date_months_span (start_str, end_str, start_fomart, end_fomart ) 
    # difference number of days 
    days_gap = date_days_span (start_str, end_str, start_fomart, end_fomart ) 
    # removed combination date indicates the number 
    difference number # hours
    12 is months_gap% = m 
    D = 30% days_gap 
    output Examples Description:

    hours_gap = time_hours_span (start_str, end_str, start_fomart, end_fomart) 
    difference number # minutes 
    mins_gap = time_minutes_span (start_str, end_str, start_fomart, end_fomart) 
    # seconds difference 
    seconds_gap = time_seconds_span (start_str, end_str, start_fomart, end_fomart) 
    # indicates a combination of the time taken number 
    H = hours_gap 
    min = 60% mins_gap 
    sec seconds_gap = 60% 

    return ((Y, m, D), (H, min, sec)) 


DEF time2date (timeint = 1565673941, the format = "% D% Y-M-% H%:% M:% S "): 
    '' ' 
    timestamp date string into the unit s, s 
    : param timeint: timestamp 
    : return: date string 
    (1565673941,"% Y-M-% D% ") output 2019-08-13
    (1565673941, "% Y-% m- % d% H:% M:% S") output 13:25:41 2019-08-13 
    (1565673941, "the Y% m% D%") output 20,190,813 
    '' ' 
    = time.localtime LOCAL_TIME (timeint) 
    data_head the time.strftime = (the format, LOCAL_TIME) 

    return data_head 

IF the __name__ == '__main__': 
    # # a same time from the date format. 
    START_DATE = # '2009-02-28 00:00:04' 
    # END_DATE = '2010-03-01 01:02:03' 
    # Cha = gap_start2end (START_DATE, END_DATE) 
    # Print (Cha) 

    # Second, a different date format the time distance. The time now distance 
    START_DATE = '20,200,401' 
    END_DATE = time2date (the time.time ()) 
    cha_now = gap_start2end (START_DATE, END_DATE, start_fomart = '% m% D% the Y', end_fomart = '

  

Guess you like

Origin www.cnblogs.com/andy9468/p/12627927.html