diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/apns.go | 37 | ||||
-rw-r--r-- | utils/config.go | 1 | ||||
-rw-r--r-- | utils/time.go | 23 | ||||
-rw-r--r-- | utils/time_test.go | 50 |
4 files changed, 74 insertions, 37 deletions
diff --git a/utils/apns.go b/utils/apns.go deleted file mode 100644 index 06e8ce6ef..000000000 --- a/utils/apns.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. -// See License.txt for license information. - -package utils - -import ( - l4g "code.google.com/p/log4go" - "fmt" - "github.com/anachronistic/apns" - "github.com/mattermost/platform/model" -) - -func SendAppleNotifyAndForget(deviceId string, message string, badge int) { - go func() { - if err := SendAppleNotify(deviceId, message, badge); err != nil { - l4g.Error(fmt.Sprintf("%v %v", err.Message, err.DetailedError)) - } - }() -} - -func SendAppleNotify(deviceId string, message string, badge int) *model.AppError { - payload := apns.NewPayload() - payload.Alert = message - payload.Badge = 1 - - pn := apns.NewPushNotification() - pn.DeviceToken = deviceId - pn.AddPayload(payload) - client := apns.BareClient(Cfg.EmailSettings.ApplePushServer, Cfg.EmailSettings.ApplePushCertPublic, Cfg.EmailSettings.ApplePushCertPrivate) - resp := client.Send(pn) - - if resp.Error != nil { - return model.NewAppError("", "Could not send apple push notification", fmt.Sprintf("id=%v err=%v", deviceId, resp.Error)) - } else { - return nil - } -} diff --git a/utils/config.go b/utils/config.go index 2fd799cd1..0b292a2ca 100644 --- a/utils/config.go +++ b/utils/config.go @@ -24,6 +24,7 @@ const ( ) var Cfg *model.Config = &model.Config{} +var CfgDiagnosticId = "" var CfgLastModified int64 = 0 var CfgFileName string = "" var ClientCfg map[string]string = map[string]string{} diff --git a/utils/time.go b/utils/time.go new file mode 100644 index 000000000..7d5afdf8f --- /dev/null +++ b/utils/time.go @@ -0,0 +1,23 @@ +package utils + +import ( + "time" +) + +func MillisFromTime(t time.Time) int64 { + return t.UnixNano() / int64(time.Millisecond) +} + +func StartOfDay(t time.Time) time.Time { + year, month, day := t.Date() + return time.Date(year, month, day, 0, 0, 0, 0, t.Location()) +} + +func EndOfDay(t time.Time) time.Time { + year, month, day := t.Date() + return time.Date(year, month, day, 23, 59, 59, 999999999, t.Location()) +} + +func Yesterday() time.Time { + return time.Now().AddDate(0, 0, -1) +} diff --git a/utils/time_test.go b/utils/time_test.go new file mode 100644 index 000000000..7d65046bf --- /dev/null +++ b/utils/time_test.go @@ -0,0 +1,50 @@ +// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils + +import ( + "testing" + "time" +) + +var format = "2006-01-02 15:04:05.000000000" + +func TestMillisFromTime(t *testing.T) { + input, _ := time.Parse(format, "2015-01-01 12:34:00.000000000") + actual := MillisFromTime(input) + expected := int64(1420115640000) + + if actual != expected { + t.Fatalf("TestMillisFromTime failed, %v=%v", expected, actual) + } +} + +func TestYesterday(t *testing.T) { + actual := Yesterday() + expected := time.Now().AddDate(0, 0, -1) + + if actual.Year() != expected.Year() || actual.Day() != expected.Day() || actual.Month() != expected.Month() { + t.Fatalf("TestYesterday failed, %v=%v", expected, actual) + } +} + +func TestStartOfDay(t *testing.T) { + input, _ := time.Parse(format, "2015-01-01 12:34:00.000000000") + actual := StartOfDay(input) + expected, _ := time.Parse(format, "2015-01-01 00:00:00.000000000") + + if actual != expected { + t.Fatalf("TestStartOfDay failed, %v=%v", expected, actual) + } +} + +func TestEndOfDay(t *testing.T) { + input, _ := time.Parse(format, "2015-01-01 12:34:00.000000000") + actual := EndOfDay(input) + expected, _ := time.Parse(format, "2015-01-01 23:59:59.999999999") + + if actual != expected { + t.Fatalf("TestEndOfDay failed, %v=%v", expected, actual) + } +} |