riku
2024-01-10 a9e8e27e0503552b7b2a99c821da732175d4f071
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
 
 
class DateUtils:
 
    formatter = '%Y-%m-%d %H:%M:%S'
 
    @staticmethod
    def time_to_str(time: datetime) -> str:
        """格式化为字符串"""
 
        return time.strftime(DateUtils.formatter)
 
    @staticmethod
    def str_to_time(time_str: str) -> str:
        """字符串转时间格式"""
        if isinstance(time_str, str):
            return datetime.strptime(time_str, DateUtils.formatter)
 
    @staticmethod
    def time_check(time):
        """时间格式检查, 允许接收字符串和datetime两种格式"""
        if isinstance(time, str):
            return DateUtils.str_to_time(time)
        elif not isinstance(time, datetime):
            return False
        else:
            return time
 
    @staticmethod
    def now() -> str:
        """返回当前日期时间"""
        return datetime.now()
 
    @staticmethod
    def now_time() -> str:
        """返回当前日期时间
 
        Returns:
            str: 当前日期时间
        """
        return DateUtils.time_to_str(datetime.now())
 
    @staticmethod
    def time_slice(start_time, end_time, hour: int) -> list:
        """时间分割,根据给定的时间段和每段时长,将总时段分割为多端时间段"""
 
        st = DateUtils.time_check(start_time)
        et = DateUtils.time_check(end_time)
        if (st == False) or (et == False):
            return []
 
        result = []
        current_time = st
        while current_time < et:
            next_time = current_time + timedelta(hours=hour)
            # 当加了8小时的时间仍然小于结束时间,将此段时间加入结果
            if next_time < et:
                s = current_time
                e = next_time - timedelta(seconds=1)
                result.append((s, e))
                current_time = next_time
            # 否则把current_time和end_time作为最后一组时段加入结果并退出循环
            else:
                s = current_time
                e = et
                result.append((s, e))
                break
        return result
 
    @staticmethod
    def time_distance(time1, time2=datetime.now(), type: str = "day") -> int:
        """时间相差几天
 
        Args:
            time1 (str): 时间1
            time2 (str): 时间2
 
        Returns:
            int: 相差的天数
        """
        date1 = DateUtils.time_check(time1)
        date2 = DateUtils.time_check(time2)
        if date1 == False or date2 == False:
            return -1
        time_diff = abs(date2 - date1)
        if type == "day":
            return time_diff.days
        elif type == "hour":
            return time_diff.days*24 + time_diff.seconds / 3600
 
    @staticmethod
    def today_0am() -> datetime:
        """获取今日0点"""
 
        return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
 
    @staticmethod
    def today_0am() -> datetime:
        """获取今日0点"""
 
        return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)