-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
75 lines (64 loc) · 2.04 KB
/
date.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
package xlib
import (
"fmt"
"math"
"time"
)
// NowB - returns the beginning of today
func NowB() time.Time {
return NowBegin()
}
// NowBegin - начало сегодняшнего дня
func NowBegin() time.Time {
return time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
}
// DateBegin - начало переданного дня
func DateBegin(date time.Time) time.Time {
return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC)
}
// WeekdayR - Сокращённое название дня недели на русском языке
func WeekdayR(t time.Time) string {
days := []string{
"Вс",
"Пн",
"Вт",
"Ср",
"Чт",
"Пт",
"Сб",
}
wd := t.Weekday()
return days[wd]
}
// FirstDay - первый день текущего года
func FirstDay() time.Time {
currentYear, _, _ := time.Now().Date()
firstDate, _ := time.Parse("2006-01-02", fmt.Sprintf("%d-01-01", currentYear))
return firstDate
}
// LastDay - последний день текущего года
func LastDay() time.Time {
currentYear, _, _ := time.Now().Date()
lastDate, _ := time.Parse("2006-01-02", fmt.Sprintf("%d-12-31", currentYear))
return lastDate
}
// Index - номер дня t в текущем году, первый день года имеет индекс 0
func Index(t time.Time) int {
return int(math.Round(t.Sub(FirstDay()).Hours() / 24))
}
// Day - получить день в текущем году по его номеру, первый день года имеет индекс 0
func Day(i int) time.Time {
if (i < 0) || (i >= DayCount()) {
return time.Time{}
}
return FirstDay().AddDate(0, 0, i)
}
// DayCount - количество дней в текущем году
func DayCount() int {
return Index(LastDay()) + 1
}
// Yesterday - ровно на начало вчера
func Yesterday() time.Time {
now, _ := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))
return now.AddDate(0, 0, -1) // целевым является вчерашний день
}