Time into summary

Acquires a time stamp

Import Time 
A = the time.time () stamp with fractions
B = int (A)     
Print (A)

 

Timestamp into Date

Converting the timestamp to date python

import datetime

import time

ltime=time.localtime(1395025933)

timeStr=time.strftime("%Y-%m-%d %H:%M:%S", ltime)

print timeStr

E.g:

date = 1563023653203254 timestamp

ltime = time.localtime(date)
end_date = time.strftime("%Y-%m-%d ", ltime)

 

Get the current date and time

import datetime
a=datetime.datetime.now()   具体时间
print(a)

2019-04-29 10:51:36.45995

# Get the current time and the time into the object
TIME_NOW = datetime.datetime.now () the strftime (. 'M-%%% Y-D' )
TIME_NOW datetime.date = (int (TIME_NOW [:. 4]), int (TIME_NOW [5: 7]), int (time_now [-2:]))

 

Python next to the time stamp of the conversion date

import datetime

import time

dateC=datetime.datetime(2010,6,6,8,14,59)

timestamp=time.mktime(dateC.timetuple())

print timestamp

 

Time objects up to the specified month

b="2016-08-31"
d="2019-02-20"
time_b=datetime.date(int(b[:4]),int(b[5:7]),int(b[-2:]))
time_d=datetime.date(int(d[:4]),int(d[5:7]),int(d[-2:]))

 

 

def add_months(dt, months):
    targetmonth = months + dt.month
    try:
        dt = dt.replace(year=dt.year + int(targetmonth / 12), month=(targetmonth % 12))
    except:
        if targetmonth+1==12:
            dt=dt.replace(year=dt.year+int((targetmonth+1)/12),month=(int(12)),day=1)
        else:
            dt=dt.replace(year=dt.year+int((targetmonth+1)/12),month=((targetmonth+1)%12),

day=1)

            dt = datetime.timedelta (days = 1)

            return dt

 

time_c=add_months1(time_b,1)

print(time_c)

 

Timestamp converted to time format

.FROM_UNIXTIME () function

FROM_UNIXTIME(unix_timestamp,format)

Parameter unix_timestamp  timestamp can be stored in the database field time data

Parameters format   format to be transformed , such as ""% Y-% m-% d  "" time after formatting is thus 2017-11-30 

FROM_UNIXTIME(create_time, "%Y-%m-%d") AS dat

 

Time stamp format into

UNIX_TIMESTAMP(date)

Which may be a date DATE string, a DATETIME string, number, or YYYMMDD format YYMMDD or a local time in a TIMESTAMP

This function can be used to help us filter out some day in timestamp data

 

 

DEF edit_timestamp ( timestamp , is_timing , _type = 0):
    "" "
    : param timestamp: incoming timestamp
    : param _type: Edit the timestamp type 1 start_time, 2 to end_time, 3 to date
    : param is_timing: whether timing job
    : return: time series string type child '2018-06-26'
    "" "
    IF _type == 0:
        # error incoming parameters, return null
        return ''
    elif _type ==. 1:
        # configuration '12 -21 24:00 (week 4) '
        day_of_the_week_dict = {0:' (Monday) ", 1:" (Tuesday) ",
                                2:" (Wednesday), '3:' (Thursday) ", 4: '(Friday)', 5: '(Saturday),' 6: '(Sunday) '}
        on DAY_OF_THE_WEEK =timestamp // 86400 % 7
        time_array = time.localtime(timestamp)
        time_string = time.strftime("%m-%d %H:%M", time_array)
        return time_string + day_of_the_week_dict.get(time_array.tm_wday, '')
    elif _type == 2:
        # 构成 '12-21 24:00' 或者 '定时发送12-21 24:00'
        time_array = time.localtime(timestamp)
        time_string = time.strftime("%m-%d %H:%M", time_array)
        return '定时发送' + time_string if is_timing else time_string
    elif _type == 3:
        # 构成 '2017年12月'
        time_array = time.localtime(timestamp)
        time_string = time.strftime("%Y-%m:", time_array).replace('-', '年').replace(':', '月')
        return time_string
    elif _type == 4:
        # 构成 '12月11日'
        time_array = time.localtime(timestamp)
        time_string = time.strftime("%m-%d:", time_array).replace('-', '月').replace(':', '日')
        return time_string + ' '
    elif _type == 5:
        # 构成 '12月11日 23:00'
        time_array = time.localtime(timestamp)
        time_string = time.strftime("%m-%d! %H:%M", time_array).replace('-', '月').replace('!', '日')
        return time_string
    elif _type ==. 6:
        day_of_the_week_dict = {0: '(Monday) ", 1:" (Tuesday) ",
                                2:" (Wednesday),' 3: '(Thursday) ", 4:" (Friday)', 5: "(Sat)", 6: "(Sunday)"}
        # configuration '2017-12-12 12:12'
        time_array time.localtime = ( timestamp )
        TIME_STRING The time.strftime = ( "Y-%%% M- D ", time_array)
        return TIME_STRING + day_of_the_week_dict.get (time_array.tm_wday, '')
    elif _type ==. 7:
        day_of_the_week_dict = {0: '(Monday)", 1: "(Tuesday),'
                                2: '(Wednesday ) '3:' (Thursday) ", 4:" (Friday) ', 5:' (Saturday), '6:'(Sunday) '}
        # constitute' 2019 Nian 06 Yue 20 Ri (Xingqi Si) '
        time_array = time.localtime (timestamp)
        time_string = time.strftime("%Y-%m-%d", time_array)
        time_list=time_string.split("-")
        time_string="%s年%s月%s日" %(time_list[0],time_list[1],time_list[2])
        return time_string+day_of_the_week_dict.get(time_array.tm_wday, '')

 

 

 

 

def edit_timestamp(timestamp, is_timing, _type=0):

    """

    : Param timestamp: incoming timestamp

    : Param _type: Edit the timestamp type 1 start_time, 2 to end_time, 3 to date

    : Param is_timing: whether the timing job

    : Return: time series string type child '2018-06-26'

    """

    if _type == 0:

        # Error incoming parameters, return null

        return ''

    elif _type == 1:

        # Constituting '12 -21 24:00 (week 4) '

        day_of_the_week_dict = {0: '(Monday) ", 1:" (Tuesday),' 2: '(Wednesday)',

                      3: '(Thursday) ", 4:" (Friday)', 5: '(Saturday),' 6: "(Sunday) '}

        day_of_the_week = timestamp // 86400 % 7

        time_array = time.localtime(timestamp)

        time_string = time.strftime("%m-%d %H:%M", time_array)

        return time_string + day_of_the_week_dict.get(time_array.tm_wday, '')

    elif _type == 2:

        # 2400 constituting '12 -21 'or' timing of transmitting 12-21 24:00 '

        time_array = time.localtime(timestamp)

        time_string = time.strftime("%m-%d %H:%M", time_array)

        return 'Sending' + time_string if is_timing else time_string

    elif _type == 3:

        # Constitute 'December 2017'

        time_array = time.localtime(timestamp)

        time_string = time.strftime("%Y-%m:", time_array).replace('-', '年').replace(':', '月')

        return time_string

    elif _type == 4:

        # Constitute '12 May 11 '

        time_array = time.localtime(timestamp)

        time_string = time.strftime("%m-%d:", time_array).replace('-', '月').replace(':', '日')

        return time_string + ' '

    elif _type == 5:

        # Constitute '12 May at 23:00 on the 11th '

        time_array = time.localtime(timestamp)

        time_string = time.strftime("%m-%d! %H:%M", time_array).replace('-', '月').replace('!', '日')

        return time_string

    elif _type == 6:

        day_of_the_week_dict = {0: '(Monday) ", 1:" (Tuesday),' 2: '(Wednesday)',

3: '(Thursday) ", 4:" (Friday)', 5: '(Saturday),' 6: "(Sunday) '}

        # Configuration '2017-12-12 12:12'

        time_array = time.localtime(timestamp)

        time_string = time.strftime("%Y-%m-%d ", time_array)

        return time_string+day_of_the_week_dict.get(time_array.tm_wday, '')

 

 

Download excel spreadsheet processing

Under FLASK

DEF save_excel_sheets (data_qy, data_ds, data_kh):
    "" "
   
Function Description : In one excel storage manager in the region, city manager, data manager to the client . 3 th sheet in
   
work_book = openpyxl.Workbook ()
    sheet_qy work_book.create_sheet = ( " regional manager " , 0) # sheet1
    sheet_ds = work_book.create_sheet ( " the city manager " , 1)
    sheet_kh = work_book.create_sheet ( " Account manager " , 2)
    for obj_qy in data_qy: # day_qy a header that contains tabular data
        sheet_qy.append (obj_qy)
    for obj_ds indata_ds:
        sheet_ds.append(obj_ds)
    for obj_kh in data_kh:
        sheet_kh.append(obj_kh)
    content = save_virtual_workbook(workbook=work_book)
    work_book.close()
    response = make_response(content)
    response.headers["Content-Disposition"] = f'attachment'
   
response.headers['Content-Type'] = 'application/octet-stream'
   
return response

 

Django under

try:
    wb = Workbook(encoding='utf-8')
    qy = wb.add_sheet('区域经理')
    ds = wb.add_sheet('地市经理')
    kh = wb.add_sheet('客户经理')
    for row_i, row in enumerate(data_qy):
        for col_j, col in enumerate(row):
            qy.write(row_i, col_j, label=col)
    for row_i, row in enumerate(data_ds):
        for col_j, col in enumerate(row):
            ds.write(row_i, col_j, label=col)
    for row_i, row in enumerate(data_kh):
        for col_j, col in enumerate(row):
            kh.write(row_i, col_j, label=col)
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename={}.xls'.format(file_name)
    wb.save(response)
    return response
except Exception as e:
    print e

 

Guess you like

Origin www.cnblogs.com/laowang-106/p/11101972.html