Skip to content

Commit

Permalink
Supports timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
canhlinh committed Dec 10, 2018
1 parent 39185dd commit 4697e0e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
20 changes: 13 additions & 7 deletions zksocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 8 additions & 6 deletions zksocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -21,33 +22,34 @@ 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()

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()
require.NoError(t, err)
}

func BenchmarkSocketGetAttendances(b *testing.B) {
socket := NewZkSocket(testZkHost, testZkPort, 0)
socket := NewZkSocket(testZkHost, testZkPort, 0, testTimezone)
require.NoError(b, socket.Connect())
defer socket.Destroy()

Expand Down

0 comments on commit 4697e0e

Please sign in to comment.