Skip to content

Commit b080c52

Browse files
Handle timezone information during DateTime parsing (#46) (#49)
* Handle timezone information during DateTime parsing (#46) * adjusting asserts to be UTC * updating TibiadataDatetimeV3 with correct format * adding loading of Europe/Berlin timezone and changing time parsing to include that location * cleaning Co-authored-by: kamilon <kennedybushnell@kamilon.com>
1 parent afdf2d5 commit b080c52

File tree

6 files changed

+66
-56
lines changed

6 files changed

+66
-56
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ require (
2626
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2727
github.com/modern-go/reflect2 v1.0.2 // indirect
2828
github.com/pmezard/go-difflib v1.0.0 // indirect
29-
github.com/stretchr/objx v0.3.0 // indirect
3029
github.com/ugorji/go/codec v1.2.6 // indirect
3130
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
3231
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
6161
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
6262
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
6363
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
64-
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
6564
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
66-
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
67-
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
6865
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
6966
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
7067
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

src/TibiaCharactersCharacterV3_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestNumber1(t *testing.T) {
3434
assert.Nil(characterJson.Characters.Character.Houses)
3535
assert.Equal("Jokerz", characterJson.Characters.Character.Guild.GuildName)
3636
assert.Equal("Trial of the ", characterJson.Characters.Character.Guild.Rank)
37-
assert.Equal("2022-01-05T22:23:32Z", characterJson.Characters.Character.LastLogin)
37+
assert.Equal("2022-01-05T21:23:32Z", characterJson.Characters.Character.LastLogin)
3838
assert.Equal("Premium Account", characterJson.Characters.Character.AccountStatus)
3939
assert.Empty(characterJson.Characters.Character.Comment)
4040
}
@@ -69,7 +69,7 @@ func TestNumber2(t *testing.T) {
6969
assert.Equal("2022-01-16", characterJson.Characters.Character.Houses[0].Paid)
7070
assert.Equal("Magnus Magister of the ", characterJson.Characters.Character.Guild.Rank)
7171
assert.Equal("Lionheart Society", characterJson.Characters.Character.Guild.GuildName)
72-
assert.Equal("2022-01-06T22:38:44Z", characterJson.Characters.Character.LastLogin)
72+
assert.Equal("2022-01-06T21:38:44Z", characterJson.Characters.Character.LastLogin)
7373
assert.Equal("Testa de Ferro do Lejonhjartat ;)", characterJson.Characters.Character.Comment)
7474
assert.Equal("Premium Account", characterJson.Characters.Character.AccountStatus)
7575

@@ -104,7 +104,7 @@ func TestNumber3(t *testing.T) {
104104
assert := assert.New(t)
105105

106106
assert.Equal("Borttagna Gubben", characterJson.Characters.Character.Name)
107-
assert.Equal("2022-03-08T01:09:13Z", characterJson.Characters.Character.DeletionDate)
107+
assert.Equal("2022-03-08T00:09:13Z", characterJson.Characters.Character.DeletionDate)
108108
assert.Equal("", characterJson.Characters.Character.LastLogin)
109109
assert.Equal("Free Account", characterJson.Characters.Character.AccountStatus)
110110
}

src/TibiaDataUtils.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"time"
6+
7+
"golang.org/x/text/unicode/norm"
8+
)
9+
10+
// TibiadataDatetimeV3 func
11+
func TibiadataDatetimeV3(date string) string {
12+
//TODO: Normalization needs to happen above this layer
13+
date = norm.NFKC.String(date)
14+
15+
var (
16+
returnDate time.Time
17+
err error
18+
)
19+
20+
// If statement to determine if date string is filled or empty
21+
if date == "" {
22+
// The string that should be returned is the current timestamp
23+
returnDate = time.Now()
24+
} else {
25+
// timezone use in html: CET/CEST
26+
loc, _ := time.LoadLocation("Europe/Berlin")
27+
28+
// format used in datetime on html: Jan 02 2007, 19:20:30 CET
29+
formatting := "Jan 02 2006, 15:04:05 MST"
30+
31+
// parsing html in time with location set in loc
32+
returnDate, err = time.ParseInLocation(formatting, date, loc)
33+
34+
// parsing html in tiem without loc
35+
//returnDate, err = time.Parse("Jan 02 2006, 15:04:05 MST", date)
36+
37+
if err != nil {
38+
log.Println(err)
39+
}
40+
}
41+
42+
// Return of formatted date and time string to functions..
43+
return returnDate.UTC().Format(time.RFC3339)
44+
}

src/TibiaDataUtils_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestTibiaCETDateFormat(t *testing.T) {
10+
assert.Equal(t, "2021-12-24T08:52:16Z", TibiadataDatetimeV3("Dec 24 2021, 09:52:16 CET"))
11+
}
12+
13+
func TestTibiaCESTDateFormat(t *testing.T) {
14+
assert.Equal(t, "2021-12-24T07:52:16Z", TibiadataDatetimeV3("Dec 24 2021, 09:52:16 CEST"))
15+
}
16+
17+
func TestTibiaUTCDateFormat(t *testing.T) {
18+
assert.Equal(t, "2021-12-24T09:52:16Z", TibiadataDatetimeV3("Dec 24 2021, 09:52:16 UTC"))
19+
}

src/webserver.go

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -417,55 +417,6 @@ func TibiadataQueryEscapeStringV3(data string) string {
417417
return url.QueryEscape(data)
418418
}
419419

420-
var datetimeRegex = regexp.MustCompile(`(.*).([0-9][0-9]).([0-9][0-9][0-9][0-9]),.([0-9][0-9]:[0-9][0-9]:[0-9][0-9]).(.*)`)
421-
422-
// TibiadataDatetimeV3 func
423-
func TibiadataDatetimeV3(date string) string {
424-
var returnDate string
425-
426-
// If statement to determine if date string is filled or empty
427-
if date == "" {
428-
// The string that should be returned is the current timestamp
429-
returnDate = time.Now().UTC().Format(time.RFC3339)
430-
} else {
431-
// Converting: Jan 02 2007, 19:20:30 CET -> RFC1123 -> RFC3339
432-
433-
// regex to exact values..
434-
subma1 := datetimeRegex.FindAllStringSubmatch(date, -1)
435-
436-
if len(subma1) > 0 {
437-
// Adding fake-Sun for valid RFC1123 convertion..
438-
dateDate, err := time.Parse(time.RFC1123, "Sun, "+subma1[0][2]+" "+subma1[0][1]+" "+subma1[0][3]+" "+subma1[0][4]+" "+subma1[0][5])
439-
if err != nil {
440-
// log.Fatal(err)
441-
log.Println(err)
442-
}
443-
444-
// Set data to return
445-
returnDate = dateDate.UTC().Format(time.RFC3339)
446-
447-
} else {
448-
// Format not defined yet..
449-
log.Println("Incoming date: " + date)
450-
log.Println("UNKNOWN FORMAT YET!")
451-
452-
// Parse the given string to be formatted correct later
453-
dateDate, err := time.Parse(time.RFC3339, string(date))
454-
if err != nil {
455-
log.Fatal(err)
456-
}
457-
458-
// Set data to return
459-
returnDate = dateDate.UTC().Format(time.RFC3339)
460-
461-
}
462-
}
463-
464-
// Return of formatted date and time string to functions..
465-
return returnDate
466-
467-
}
468-
469420
var dateRegex = regexp.MustCompile(`([a-zA-Z]{3}).*([0-9]{2}).*([0-9]{4})`)
470421

471422
// TibiadataDateV3 func

0 commit comments

Comments
 (0)