From c7de414dd041b6c73635d2f1f7a03e05151c99a4 Mon Sep 17 00:00:00 2001 From: jingyuexing <19589872+jingyuexing@users.noreply.github.com> Date: Tue, 11 Jun 2024 00:06:19 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20buffer=20=E2=9C=85=20?= =?UTF-8?q?test:=20update=20datetime=20testing=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- buffer.go | 39 +++++++++++++++++++++++++++++++++++++++ utils_test.go | 10 ++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 buffer.go diff --git a/buffer.go b/buffer.go new file mode 100644 index 0000000..b1e5d57 --- /dev/null +++ b/buffer.go @@ -0,0 +1,39 @@ +package utils + +import "strings" + + +const ( + hextable = "0123456789abcdef" +) + +type Buffer []byte + +func NewBuffer() Buffer { + return make([]byte,0) +} + +func (b Buffer) Length() int{ + return len(b) // the unint is byte +} + +func (b Buffer) String() string { + str := make([]byte,b.Length() << 1) + j := 0 + for _,item := range b { + str[j] = hextable[item>>4] + str[j+1] = hextable[item&0x0f] + j += 2 + } + return string(str) +} + +func (b Buffer) Load(data string) Buffer{ + for i:= 0;i < len(data);i+=2 { + cache := 0x00 + cache = strings.Index(hextable,string(data[i])) << 4 + cache = cache | strings.Index(hextable,string(data[i+1])) + b = append(b, byte(cache)) + } + return b +} diff --git a/utils_test.go b/utils_test.go index d9fa886..93738ee 100644 --- a/utils_test.go +++ b/utils_test.go @@ -326,6 +326,14 @@ func TestDateTime(t *testing.T) { t.Error("caculate has wrong") } + datetime2 := utils.NewDateTime() + + datetime2 = *datetime2.Parse("2024/06/06/23:10:40","YYYY/MM/DD/HH:mm:ss") + + if datetime2.Year != 2024 { + t.Error("paser has wrong") + } + fmt.Printf("解析后时间 %s",datetime2.String()) } func TestNumberConver(t *testing.T) { @@ -441,8 +449,6 @@ func TestReferenceString(t *testing.T){ func TestBuffer(t *testing.T){ bf := utils.NewBuffer() bf = bf.Load("320c3a83c202880e83fa0814320c3a83c202880e83fa10") - fmt.Printf("%#v\n",bf) - if bf[0] != 0x32 { t.Error(fmt.Sprintf("TestBuffer expect: %d,but got %d",0x32,bf[0])) }