From 4697e0eb796d44dd455f3f1581fc5673170669f6 Mon Sep 17 00:00:00 2001 From: canhlinh Date: Mon, 10 Dec 2018 18:36:39 +0700 Subject: [PATCH] Supports timezone --- zksocket.go | 20 +++++++++++++------- zksocket_test.go | 14 ++++++++------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/zksocket.go b/zksocket.go index a3a3bc3..876ed8a 100644 --- a/zksocket.go +++ b/zksocket.go @@ -32,17 +32,23 @@ type ZkSocket struct { pin int connected bool lastData []byte + location *time.Location } // NewZkSocket creates a new ZkSocket -func NewZkSocket(host string, port int, pin int) Zk { +func NewZkSocket(host string, port int, pin int, timezone string) Zk { + location, err := time.LoadLocation(timezone) + if err != nil { + panic(err) + } zk := &ZkSocket{ - conn: nil, - bp: &binarypack.BinaryPack{}, - host: host, - port: port, - pin: pin, + conn: nil, + bp: &binarypack.BinaryPack{}, + host: host, + port: port, + pin: pin, + location: location, } if err := zk.createSocket(); err != nil { @@ -699,7 +705,7 @@ func (s *ZkSocket) decodeTime(packet []byte) (time.Time, error) { t = t / 12 year := t + 2000 - return time.Date(year, time.Month(month), day, hour, minute, second, 0, time.UTC), nil + return time.Date(year, time.Month(month), day, hour, minute, second, 0, s.location), nil } // makeCommKey take a password and session_id and scramble them to send to the time clock. diff --git a/zksocket_test.go b/zksocket_test.go index 187540b..7f5d42e 100644 --- a/zksocket_test.go +++ b/zksocket_test.go @@ -10,8 +10,9 @@ import ( // I'm too lazy to work to this project const ( - testZkHost = "192.168.0.201" - testZkPort = 4370 + testZkHost = "27.72.144.131" + testZkPort = 4370 + testTimezone = "Asia/Ho_Chi_Minh" ) func TestSocketcreateHeader(t *testing.T) { @@ -21,14 +22,14 @@ func TestSocketcreateHeader(t *testing.T) { } func TestSocketConnect(t *testing.T) { - socket := NewZkSocket(testZkHost, testZkPort, 0) + socket := NewZkSocket(testZkHost, testZkPort, 0, testTimezone) err := socket.Connect() require.NoError(t, err) defer socket.Destroy() } func TestSocketGetAttendances(t *testing.T) { - socket := NewZkSocket(testZkHost, testZkPort, 0) + socket := NewZkSocket(testZkHost, testZkPort, 0, testTimezone) err := socket.Connect() require.NoError(t, err) defer socket.Destroy() @@ -36,10 +37,11 @@ func TestSocketGetAttendances(t *testing.T) { attendances, err := socket.GetAttendances() require.NoError(t, err) t.Log(len(attendances)) + t.Log(attendances[len(attendances)-1]) } func TestSocketGetUsers(t *testing.T) { - socket := NewZkSocket(testZkHost, testZkPort, 0) + socket := NewZkSocket(testZkHost, testZkPort, 0, testTimezone) require.NoError(t, socket.Connect()) defer socket.Destroy() _, err := socket.GetUsers() @@ -47,7 +49,7 @@ func TestSocketGetUsers(t *testing.T) { } func BenchmarkSocketGetAttendances(b *testing.B) { - socket := NewZkSocket(testZkHost, testZkPort, 0) + socket := NewZkSocket(testZkHost, testZkPort, 0, testTimezone) require.NoError(b, socket.Connect()) defer socket.Destroy()