|
| 1 | +package lotime |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +// GetWeekOfMonth returns the week number as int in month by date |
| 9 | +func GetWeekOfMonth(dt time.Time, isFirstDayMon bool) int { |
| 10 | + // Get the first day of the month |
| 11 | + firstDayOfMonth := time.Date(dt.Year(), dt.Month(), 1, 0, 0, 0, 0, dt.Location()) |
| 12 | + |
| 13 | + // Get offset for first weekday |
| 14 | + adjustedWeekday := 0 |
| 15 | + if isFirstDayMon { |
| 16 | + adjustedWeekday = (int(firstDayOfMonth.Weekday()) + 6) % 7 |
| 17 | + } |
| 18 | + |
| 19 | + // Day of month |
| 20 | + dayOfMonth := dt.Day() |
| 21 | + |
| 22 | + // Calculate adjusted day |
| 23 | + adjustedDay := dayOfMonth + adjustedWeekday - 1 |
| 24 | + |
| 25 | + // Calculate week number |
| 26 | + return (adjustedDay / 7) + 1 |
| 27 | +} |
| 28 | + |
| 29 | +// GetDayOfYear return day of year by date |
| 30 | +func GetDayOfYear(year int, month int, day int) int { |
| 31 | + monthName := time.Month(month) |
| 32 | + date := time.Date(year, monthName, day, 0, 0, 0, 0, time.UTC) |
| 33 | + return date.YearDay() |
| 34 | +} |
| 35 | + |
| 36 | +// DateIsPassedInCurrentYear Проверяем, прошла ли дата в текущем году |
| 37 | +func DateIsPassedInCurrentYear(dt time.Time) bool { |
| 38 | + now := time.Now().Local() |
| 39 | + |
| 40 | + date := time.Date(now.Year(), dt.Month(), dt.Day(), 0, 0, 0, 0, time.Local) |
| 41 | + |
| 42 | + if now.After(date) { |
| 43 | + return true |
| 44 | + } |
| 45 | + return false |
| 46 | +} |
| 47 | + |
| 48 | +// DateYearsSince Amount of years since the given date |
| 49 | +func DateYearsSince(dt time.Time) int { |
| 50 | + now := time.Now() |
| 51 | + |
| 52 | + years := now.Year() - dt.Year() |
| 53 | + |
| 54 | + // If current date is before dt by day or month, then year is not complete |
| 55 | + if now.Month() < dt.Month() || (now.Month() == dt.Month() && now.Day() < dt.Day()) { |
| 56 | + years-- |
| 57 | + } |
| 58 | + |
| 59 | + return years |
| 60 | +} |
| 61 | + |
| 62 | +// NthOrLastWeekdayOfMonth Calculate the nth or last weekday of a given month and year. |
| 63 | +func NthOrLastWeekdayOfMonth(year int, month time.Month, userWeekday int, weekNum int, isFirstDayMon bool) (time.Time, error) { |
| 64 | + if userWeekday < 1 || userWeekday > 7 { |
| 65 | + return time.Time{}, fmt.Errorf("invalid weekday: %d", userWeekday) |
| 66 | + } |
| 67 | + if weekNum < 0 || weekNum > 5 { |
| 68 | + return time.Time{}, fmt.Errorf("invalid week number: %d", weekNum) |
| 69 | + } |
| 70 | + |
| 71 | + targetWeekday := time.Weekday(0) |
| 72 | + if isFirstDayMon { |
| 73 | + targetWeekday = time.Weekday(userWeekday % 7) // Sunday=0 ... Saturday=6 |
| 74 | + } |
| 75 | + |
| 76 | + if weekNum == 0 { |
| 77 | + // Search for the last weekday of the month |
| 78 | + firstOfNextMonth := time.Date(year, month+1, 1, 0, 0, 0, 0, time.UTC) |
| 79 | + lastDayOfMonth := firstOfNextMonth.AddDate(0, 0, -1) |
| 80 | + |
| 81 | + for lastDayOfMonth.Weekday() != targetWeekday { |
| 82 | + lastDayOfMonth = lastDayOfMonth.AddDate(0, 0, -1) |
| 83 | + } |
| 84 | + return lastDayOfMonth, nil |
| 85 | + } |
| 86 | + |
| 87 | + // Search for the nth weekday of the month |
| 88 | + firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC) |
| 89 | + offset := (int(targetWeekday) - int(firstOfMonth.Weekday()) + 7) % 7 |
| 90 | + day := 1 + offset + (weekNum-1)*7 |
| 91 | + |
| 92 | + // Check if the date is valid |
| 93 | + if day > 31 || time.Date(year, month, day, 0, 0, 0, 0, time.UTC).Month() != month { |
| 94 | + return time.Time{}, fmt.Errorf("no such weekday occurrence in this month") |
| 95 | + } |
| 96 | + |
| 97 | + return time.Date(year, month, day, 0, 0, 0, 0, time.UTC), nil |
| 98 | +} |
0 commit comments