#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author : lonie
#
# _____ ______
# ____==== ]OO|_n_n__][. | |]
# [________]_||________)<
# oo oo 'oo OOOO-| oo\_ ~o~~~o~'
# +--+--+--+--+--+--+--+--+--+--+--+--+--+
# @Time : 2022/9/15 10:08
# @FIle: TimeUtil.py
# @Software: PyCharm
import datetime
d = datetime.datetime.now()
print(type(d))
class TimeUtil(object):
@staticmethod
def day_get(time: datetime.datetime) -> tuple():
"""
获得今天00:00 到今天23:59
:return:
date_from:2022-09-14 00:00:00
date_to:2022-09-14 23:59:59
"""
oneday = datetime.timedelta(days=1)
day = time - oneday
date_from = datetime.datetime(day.year, day.month, day.day, 0, 0, 0)
date_to = datetime.datetime(day.year, day.month, day.day, 23, 59, 59)
print('---'.join([str(date_from), str(date_to)]))
# date_from = int(date_from.timestamp())
# date_to = int(date_to.timestamp())
# print('---'.join([str(date_from), str(date_to)]))
return date_from, date_to
@staticmethod
def week_get(time: datetime.datetime) -> tuple():
"""
获得一周之前的时间,从当前时间到一周之前
:return:
"""
dayscount = datetime.timedelta(days=time.isoweekday())
dayto = time - dayscount
sixdays = datetime.timedelta(days=6)
dayfrom = dayto - sixdays
date_from = datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day, 0, 0, 0)
date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59)
print('---'.join([str(date_from), str(date_to)]))
return date_from, date_to
@staticmethod
def month_get(time: datetime.datetime) -> tuple():
"""
返回一个月之前的时间
:return
date_from: 2022-08-15 00:00:00
date_to: 2022-09-15 23:59:59
"""
dayscount = datetime.timedelta(days=31)
dayto = time - dayscount
date_from = datetime.datetime(dayto.year, dayto.month, dayto.day, 0, 0, 0)
date_to = datetime.datetime(dayto.year, time.month, dayto.day, 23, 59, 59)
print('---'.join([str(date_from), str(date_to)]))
return date_from, date_to
if __name == 'main':
d = datetime.datetime.now()
tmp = TimeUtil.day_get(time=d)
print("tmp", tmp)
tmp = TimeUtil.week_get(time=d)
print("tmp", tmp)
beginTime, endTime = TimeUtil.month_get(time=d)
print('beginTime', str(beginTime), 'endTime', str(endTime))
print('转化为时间戳:秒级别', int(beginTime.timestamp()))
print('转化为时间戳毫秒级别', int(beginTime.timestamp() * 1000))
print("tmp", str(tmp))