|
| 1 | +/* |
| 2 | +TencentBlueKing is pleased to support the open source community by making |
| 3 | +蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. |
| 4 | +
|
| 5 | +Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved. |
| 6 | +
|
| 7 | +Licensed under the MIT License (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +
|
| 10 | +You may obtain a copy of the License at |
| 11 | +https://opensource.org/licenses/MIT |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package utils |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "time" |
| 25 | +) |
| 26 | + |
| 27 | +// 常量定义常用的日期时间格式 |
| 28 | +const ( |
| 29 | + LayoutRFC3339 = time.RFC3339 // RFC3339 格式 |
| 30 | + LayoutISO8601 = "2006-01-02T15:04:05Z07:00" // ISO8601 格式 |
| 31 | + LayoutYYYYMMDD = "2006-01-02" // 年-月-日 格式 |
| 32 | + LayoutYYYYMMDDHHMM = "2006-01-02 15:04" // 年-月-日 时:分 格式 |
| 33 | + LayoutYYYYMMDDHHMMSS = "2006-01-02 15:04:05" // 年-月-日 时:分:秒 格式 |
| 34 | + LayoutYYYYMMDDHHMMSSZ = "2006-01-02 15:04:05Z" // 年-月-日 时:分:秒 Z 格式 |
| 35 | + LayoutUnixTimestamp = "1136239445" // Unix 时间戳(秒) |
| 36 | +) |
| 37 | + |
| 38 | +// Now 返回当前时间的字符串表示,如果 format 为空,则使用 RFC3339 格式 |
| 39 | +func Now(format string) string { |
| 40 | + if format == "" { |
| 41 | + return time.Now().Format(LayoutRFC3339) |
| 42 | + } |
| 43 | + return time.Now().Format(format) |
| 44 | +} |
| 45 | + |
| 46 | +// Parse 将时间字符串解析为 time.Time 对象,如果 format 为空,则使用 RFC3339 格式 |
| 47 | +// 如果 timeStr 为空,返回错误 |
| 48 | +func Parse(timeStr, format string) (time.Time, error) { |
| 49 | + if timeStr == "" { |
| 50 | + return time.Time{}, fmt.Errorf("空的时间字符串") |
| 51 | + } |
| 52 | + if format == "" { |
| 53 | + return time.Parse(LayoutRFC3339, timeStr) |
| 54 | + } |
| 55 | + return time.Parse(format, timeStr) |
| 56 | +} |
| 57 | + |
| 58 | +// Format 将 time.Time 对象格式化为字符串,如果 format 为空,则使用 RFC3339 格式 |
| 59 | +func Format(t time.Time, format string) string { |
| 60 | + if format == "" { |
| 61 | + return t.Format(LayoutRFC3339) |
| 62 | + } |
| 63 | + return t.Format(format) |
| 64 | +} |
| 65 | + |
| 66 | +// Diff 计算两个时间之间的差值,并返回秒、分钟、小时和天的差值 |
| 67 | +// 确保 t1 <= t2 |
| 68 | +func Diff(t1, t2 time.Time) (seconds, minutes, hours, days int64) { |
| 69 | + if t1.After(t2) { |
| 70 | + t1, t2 = t2, t1 // 确保 t1 <= t2 |
| 71 | + } |
| 72 | + diff := t2.Sub(t1) |
| 73 | + seconds = int64(diff.Seconds()) |
| 74 | + minutes = seconds / 60 |
| 75 | + hours = minutes / 60 |
| 76 | + days = hours / 24 |
| 77 | + return seconds, minutes, hours, days |
| 78 | +} |
| 79 | + |
| 80 | +// Add 给 time.Time 对象添加一个时间间隔,并返回新的时间 |
| 81 | +func Add(t time.Time, duration time.Duration) time.Time { |
| 82 | + return t.Add(duration) |
| 83 | +} |
| 84 | + |
| 85 | +// Sub 从 time.Time 对象中减去一个时间间隔,并返回新的时间 |
| 86 | +func Sub(t time.Time, duration time.Duration) time.Time { |
| 87 | + return t.Add(-duration) |
| 88 | +} |
| 89 | + |
| 90 | +// UnixTimestamp 返回当前时间的 Unix 时间戳(秒) |
| 91 | +func UnixTimestamp() int64 { |
| 92 | + return time.Now().Unix() |
| 93 | +} |
| 94 | + |
| 95 | +// UnixTimestampMillis 返回当前时间的 Unix 时间戳(毫秒) |
| 96 | +func UnixTimestampMillis() int64 { |
| 97 | + return time.Now().UnixMilli() |
| 98 | +} |
| 99 | + |
| 100 | +// UnixTimestampMicro 返回当前时间的 Unix 时间戳(微秒) |
| 101 | +func UnixTimestampMicro() int64 { |
| 102 | + return time.Now().UnixMicro() |
| 103 | +} |
| 104 | + |
| 105 | +// FromUnixTimestamp 将 Unix 时间戳(秒)转换为 time.Time 对象 |
| 106 | +func FromUnixTimestamp(timestamp int64) time.Time { |
| 107 | + return time.Unix(timestamp, 0) |
| 108 | +} |
| 109 | + |
| 110 | +// FromUnixTimestampMillis 将 Unix 时间戳(毫秒)转换为 time.Time 对象 |
| 111 | +func FromUnixTimestampMillis(timestamp int64) time.Time { |
| 112 | + return time.UnixMilli(timestamp) |
| 113 | +} |
| 114 | + |
| 115 | +// FromUnixTimestampMicro 将 Unix 时间戳(微秒)转换为 time.Time 对象 |
| 116 | +func FromUnixTimestampMicro(timestamp int64) time.Time { |
| 117 | + return time.UnixMicro(timestamp) |
| 118 | +} |
| 119 | + |
| 120 | +// IsZero 检查 time.Time 对象是否为零值(即未设置) |
| 121 | +func IsZero(t time.Time) bool { |
| 122 | + return t.IsZero() |
| 123 | +} |
| 124 | + |
| 125 | +// StartOfDay 返回当天的开始时间(00:00:00) |
| 126 | +func StartOfDay(t time.Time) time.Time { |
| 127 | + year, month, day := t.Date() |
| 128 | + return time.Date(year, month, day, 0, 0, 0, 0, t.Location()) |
| 129 | +} |
| 130 | + |
| 131 | +// EndOfDay 返回当天的结束时间(23:59:59.999999999) |
| 132 | +func EndOfDay(t time.Time) time.Time { |
| 133 | + year, month, day := t.Date() |
| 134 | + return time.Date(year, month, day, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location()) |
| 135 | +} |
| 136 | + |
| 137 | +// IsWeekend 判断给定时间是否是周末(周六或周日) |
| 138 | +func IsWeekend(t time.Time) bool { |
| 139 | + weekday := t.Weekday() |
| 140 | + return weekday == time.Saturday || weekday == time.Sunday |
| 141 | +} |
| 142 | + |
| 143 | +// NextWorkday 返回当前时间的下一个工作日(跳过周末) |
| 144 | +func NextWorkday(t time.Time) time.Time { |
| 145 | + for { |
| 146 | + t = t.AddDate(0, 0, 1) |
| 147 | + if !IsWeekend(t) { |
| 148 | + return t |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments