Skip to content

Commit 74c0208

Browse files
committed
Fixed time utils and moved into their own package
Signed-off-by: Cristian Maglie <[email protected]>
1 parent 045c820 commit 74c0208

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

src/arduino.cc/builder/setup_build_properties.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"arduino.cc/builder/types"
4040
"arduino.cc/builder/utils"
4141
"arduino.cc/properties"
42+
"arduino.cc/timeutils"
4243
)
4344

4445
type SetupBuildProperties struct{}
@@ -108,9 +109,9 @@ func (s *SetupBuildProperties) Run(ctx *types.Context) error {
108109

109110
now := time.Now()
110111
buildProperties[constants.BUILD_PROPERTIES_EXTRA_TIME_UTC] = strconv.FormatInt(now.Unix(), 10)
111-
buildProperties[constants.BUILD_PROPERTIES_EXTRA_TIME_LOCAL] = strconv.FormatInt(utils.LocalUnix(now), 10)
112-
buildProperties[constants.BUILD_PROPERTIES_EXTRA_TIME_ZONE] = strconv.Itoa(utils.TimezoneOffset())
113-
buildProperties[constants.BUILD_PROPERTIES_EXTRA_TIME_DST] = strconv.Itoa(utils.DaylightSavingsOffset(now))
112+
buildProperties[constants.BUILD_PROPERTIES_EXTRA_TIME_LOCAL] = strconv.FormatInt(timeutils.LocalUnix(now), 10)
113+
buildProperties[constants.BUILD_PROPERTIES_EXTRA_TIME_ZONE] = strconv.Itoa(timeutils.TimezoneOffsetNoDST(now))
114+
buildProperties[constants.BUILD_PROPERTIES_EXTRA_TIME_DST] = strconv.Itoa(timeutils.DaylightSavingsOffset(now))
114115

115116
ctx.BuildProperties = buildProperties
116117

src/arduino.cc/builder/utils/time.go renamed to src/arduino.cc/timeutils/time.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
* This file is part of Arduino Builder.
33
*
4+
* Copyright 2016 Arduino LLC (http://www.arduino.cc/)
5+
*
46
* Arduino Builder is free software; you can redistribute it and/or modify
57
* it under the terms of the GNU General Public License as published by
68
* the Free Software Foundation; either version 2 of the License, or
@@ -23,24 +25,31 @@
2325
* the GNU General Public License. This exception does not however
2426
* invalidate any other reasons why the executable file might be covered by
2527
* the GNU General Public License.
26-
*
27-
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
2828
*/
2929

30-
package utils
30+
package timeutils
3131

3232
import "time"
3333

34-
func TimezoneOffset() int {
35-
_, offset := time.Unix(0, 0).Zone()
36-
return offset
34+
// TimezoneOffsetNoDST returns the timezone offset without the DST component
35+
func TimezoneOffsetNoDST(t time.Time) int {
36+
_, winterOffset := time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location()).Zone()
37+
_, summerOffset := time.Date(t.Year(), 7, 1, 0, 0, 0, 0, t.Location()).Zone()
38+
if winterOffset > summerOffset {
39+
winterOffset, summerOffset = summerOffset, winterOffset
40+
}
41+
return winterOffset
3742
}
3843

44+
// DaylightSavingsOffset returns the DST offset of the specified time
3945
func DaylightSavingsOffset(t time.Time) int {
4046
_, offset := t.Zone()
41-
return offset - TimezoneOffset()
47+
return offset - TimezoneOffsetNoDST(t)
4248
}
4349

50+
// LocalUnix returns the unix timestamp of the specified time with the
51+
// local timezone offset and DST added
4452
func LocalUnix(t time.Time) int64 {
45-
return t.Unix() + int64(TimezoneOffset()) + int64(DaylightSavingsOffset(t))
53+
_, offset := t.Zone()
54+
return t.Unix() + int64(offset)
4655
}

src/arduino.cc/builder/test/time_test.go renamed to src/arduino.cc/timeutils/time_test.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
* This file is part of Arduino Builder.
33
*
4+
* Copyright 2016 Arduino LLC (http://www.arduino.cc/)
5+
*
46
* Arduino Builder is free software; you can redistribute it and/or modify
57
* it under the terms of the GNU General Public License as published by
68
* the Free Software Foundation; either version 2 of the License, or
@@ -23,31 +25,30 @@
2325
* the GNU General Public License. This exception does not however
2426
* invalidate any other reasons why the executable file might be covered by
2527
* the GNU General Public License.
26-
*
27-
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
2828
*/
2929

30-
package test
30+
package timeutils
3131

3232
import (
3333
"testing"
3434
"time"
3535

36-
"arduino.cc/builder/utils"
3736
"github.com/stretchr/testify/require"
3837
)
3938

4039
func TestTime(t *testing.T) {
4140
loc, err := time.LoadLocation("CET")
42-
NoError(t, err)
41+
require.NoError(t, err)
4342

4443
firstJanuary2015CET := time.Date(2015, 1, 1, 0, 0, 0, 0, loc)
4544
require.Equal(t, int64(1420066800), firstJanuary2015CET.Unix())
46-
require.Equal(t, int64(1420066800+3600), utils.LocalUnix(firstJanuary2015CET))
47-
require.Equal(t, 0, utils.DaylightSavingsOffset(firstJanuary2015CET))
45+
require.Equal(t, int64(1420066800+3600), LocalUnix(firstJanuary2015CET))
46+
require.Equal(t, 3600, TimezoneOffsetNoDST(firstJanuary2015CET))
47+
require.Equal(t, 0, DaylightSavingsOffset(firstJanuary2015CET))
4848

4949
fall2015CET := time.Date(2015, 9, 23, 0, 0, 0, 0, loc)
5050
require.Equal(t, int64(1442959200), fall2015CET.Unix())
51-
require.Equal(t, int64(1442959200+3600+3600), utils.LocalUnix(fall2015CET))
52-
require.Equal(t, 3600, utils.DaylightSavingsOffset(fall2015CET))
51+
require.Equal(t, int64(1442959200+3600+3600), LocalUnix(fall2015CET))
52+
require.Equal(t, 3600, TimezoneOffsetNoDST(fall2015CET))
53+
require.Equal(t, 3600, DaylightSavingsOffset(fall2015CET))
5354
}

0 commit comments

Comments
 (0)