-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.go
121 lines (112 loc) · 3.93 KB
/
misc.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package gotime
import "time"
// IsLeapYear returns true if the year is a leap year.
//
// A leap year is a year that is evenly divisible by 4, but not by 100 unless it is also divisible by 400.
//
// Example:
// isLeap := IsLeapYear(2024)
// // isLeap == true
func IsLeapYear(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}
// DaysInMonth returns the number of days in the month.
//
// For February, the number of days depends on whether the year is a leap year or not.
// For April, June, September, and November, the number of days is 30.
// For all other months, the number of days is 31.
//
// Example:
// days := DaysInMonth(2022, 2)
// // days == 28
func DaysInMonth(year, month int) int {
switch month {
case 2:
if IsLeapYear(year) {
return 29
}
return 28
case 4, 6, 9, 11:
return 30
default:
return 31
}
}
// DaysInYear returns the number of days in the year.
//
// If the year is a leap year, it returns 366, otherwise it returns 365.
//
// Example:
// days := DaysInYear(2022)
// // days == 365
func DaysInYear(year int) int {
if IsLeapYear(year) {
return 366
}
return 365
}
// DaysInQuarter returns the number of days in the quarter.
//
// The number of days in each quarter depends on the number of days in each month.
// Q1: Jan (31) + Feb (28 or 29) + Mar (31) = 90 or 91 days
// Q2: Apr (30) + May (31) + Jun (30) = 91 days
// Q3: Jul (31) + Aug (31) + Sep (30) = 92 days
// Q4: Oct (31) + Nov (30) + Dec (31) = 92 days
//
// Example:
// days := DaysInQuarter(2022, 1)
// // days == 90
func DaysInQuarter(year, quarter int) int {
switch quarter {
case 1:
return DaysInMonth(year, 1) + DaysInMonth(year, 2) + DaysInMonth(year, 3)
case 2:
return DaysInMonth(year, 4) + DaysInMonth(year, 5) + DaysInMonth(year, 6)
case 3:
return DaysInMonth(year, 7) + DaysInMonth(year, 8) + DaysInMonth(year, 9)
default:
return DaysInMonth(year, 10) + DaysInMonth(year, 11) + DaysInMonth(year, 12)
}
}
// NewDate creates a time.Time object from the given year, month and day.
//
// It constructs a new time.Time object with the given year, month, and day, and sets the hour, minute,
// second, and nanosecond fields to 0. It also sets the location field to time.UTC.
//
// Example:
// date := NewDate(2022, 4, 15)
// // date == time.Date(2022, time.April, 15, 0, 0, 0, 0, time.UTC)
func NewDate(year, month, day int) time.Time {
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
}
// NewTime creates a time.Time object from the given hour, minute and second.
//
// It constructs a new time.Time object with the given hour, minute, and second, and sets the year, month,
// and day fields to 0. It also sets the location field to time.UTC.
//
// Example:
// timeObj := NewTime(12, 30, 0)
// // timeObj == time.Date(0, 1, 1, 12, 30, 0, 0, time.UTC)
func NewTime(hour, minute, second int) time.Time {
return time.Date(0, 0, 0, hour, minute, second, 0, time.UTC)
}
// ReplaceDate lets you replace the date part of a time.Time object with a new date.
// It keeps the time part of the time.Time object unchanged.
//
// Example:
// date := time.Date(2022, time.April, 15, 12, 30, 0, 0, time.UTC)
// newDate := ReplaceDate(date, 2023, 5, 20)
// // newDate == time.Date(2023, time.May, 20, 12, 30, 0, 0, time.UTC)
func ReplaceDate(t time.Time, year, month, day int) time.Time {
return time.Date(year, time.Month(month), day, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}
// ReplaceTime lets you replace the time part of a time.Time object with a new time.
// It keeps the date part of the time.Time object unchanged.
//
// Example:
// date := time.Date(2022, time.April, 15, 12, 30, 0, 0, time.UTC)
// newTime := ReplaceTime(date, 15, 45, 0)
// // newTime == time.Date(2022, time.April, 15, 15, 45, 0, 0, time.UTC)
func ReplaceTime(t time.Time, hour, minute, second int) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), hour, minute, second, t.Nanosecond(), t.Location())
}