| 1 | // This file is part of go-timeutils |
| 2 | // |
| 3 | // SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies |
| 4 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | |
| 6 | package timeutils |
| 7 | |
| 8 | import "time" |
| 9 | |
| 10 | // TimezoneOffsetNoDST returns the timezone offset without the DST component |
| 11 | func TimezoneOffsetNoDST(t time.Time) int { |
| 12 | _, winterOffset := time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location()).Zone() |
| 13 | _, summerOffset := time.Date(t.Year(), 7, 1, 0, 0, 0, 0, t.Location()).Zone() |
| 14 | if winterOffset > summerOffset { |
| 15 | winterOffset, summerOffset = summerOffset, winterOffset |
| 16 | } |
| 17 | return winterOffset |
| 18 | } |
| 19 | |
| 20 | // DaylightSavingsOffset returns the DST offset of the specified time |
| 21 | func DaylightSavingsOffset(t time.Time) int { |
| 22 | _, offset := t.Zone() |
| 23 | return offset - TimezoneOffsetNoDST(t) |
| 24 | } |
| 25 | |
| 26 | // LocalUnix returns the unix timestamp of the specified time with the |
| 27 | // local timezone offset and DST added |
| 28 | func LocalUnix(t time.Time) int64 { |
| 29 | _, offset := t.Zone() |
| 30 | return t.Unix() + int64(offset) |
| 31 | } |