yats.git

ref: f56066c28fc1ce985d2ac984664b9b02d7a9534b

server/dates/ranges.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
/**
 * Yats - yats
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Paolo Lulli <kevwe.com>
 * @copyright Paolo Lulli 2024
 */

package dates

import (
	"fmt"
	"time"
)

type DaysRange struct {
	From string
	To   string
}

func CalculatePreviousMonth(t time.Time) string {
	currentMonth := t.Month()
	previousMonth := currentMonth - 1
	previousMonthYear := t.Year()
	if currentMonth == time.January {
		previousMonth = time.December
		previousMonthYear = t.Year() - 1
	}

	lastMonth := fmt.Sprintf("%d%.2d", previousMonthYear, previousMonth)

	fmt.Println("month YYYYMM : ", lastMonth)

	return lastMonth
}

func CalculatePreviousMonthRange(t time.Time) DaysRange {
	currentMonth := t.Month()
	previousMonth := currentMonth - 1
	previousMonthYear := t.Year()
	if currentMonth == time.January {
		previousMonth = time.December
		previousMonthYear = t.Year() - 1
	}

	beginningOfLastMonth := time.Date(previousMonthYear, previousMonth, 1, 00, 00, 00, 00, time.UTC)
	beginningOfCurrentMonth := time.Date(t.Year(), currentMonth, 1, 00, 00, 00, 00, time.UTC)

	sBeginning := fmt.Sprintf("%d-%.2d-%.2d", beginningOfLastMonth.Year(), beginningOfLastMonth.Month(), beginningOfLastMonth.Day())
	sEnd := fmt.Sprintf("%d-%.2d-%.2d", beginningOfCurrentMonth.Year(), beginningOfCurrentMonth.Month(), beginningOfCurrentMonth.Day())

	fmt.Printf("Range today YYYY-MM-DD : %s \n", sBeginning)
	fmt.Printf("Range tomorrow YYYY-MM-DD : %s \n", sEnd)
	return DaysRange{sBeginning, sEnd}
}

func CalculateFullDay(t time.Time) DaysRange {
	yesterday := t.AddDate(0, 0, -1)
	tomorrow := t.AddDate(0, 0, +1)
	fmt.Println("today YYYY-MM-DD : ", t.Format("2006-02-01"))
	fmt.Println("tomorrow YYYY-MM-DD : ", tomorrow.Format("2006-02-01"))
	return DaysRange{yesterday.Format("2006-02-01"), t.Format("2006-02-01")}
}

func CalculateFullDayNDaysAgo(n int) DaysRange {
	t := time.Now().AddDate(0, 0, -n)
	yesterday := t.AddDate(0, 0, -1)
	tomorrow := t.AddDate(0, 0, +1)
	fmt.Println("today YYYY-MM-DD : ", t.Format("2006-02-01"))
	fmt.Println("tomorrow YYYY-MM-DD : ", tomorrow.Format("2006-02-01"))
	return DaysRange{yesterday.Format("2006-02-01"), t.Format("2006-02-01")}
}