mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
Compare commits
3 Commits
16c2df711b
...
17ff84fa1f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
17ff84fa1f | ||
|
|
bf50baa07d | ||
|
|
7d56da8108 |
@@ -40,6 +40,14 @@ func Md5String(s string) string {
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// Md5String return the md5 string of byte slice.
|
||||
// Play: todo
|
||||
func Md5Byte(data []byte) string {
|
||||
h := md5.New()
|
||||
h.Write(data)
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// Md5File return the md5 value of file.
|
||||
func Md5File(filename string) (string, error) {
|
||||
if fileInfo, err := os.Stat(filename); err != nil {
|
||||
|
||||
@@ -21,6 +21,12 @@ func TestMd5String(t *testing.T) {
|
||||
assert.Equal("5d41402abc4b2a76b9719d911017c592", Md5String("hello"))
|
||||
}
|
||||
|
||||
func TestMd5Byte(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestMd5Byte")
|
||||
data := []byte{'a'}
|
||||
assert.Equal("0cc175b9c0f1b6a831c399e269772661", Md5Byte(data))
|
||||
}
|
||||
|
||||
func TestMd5File(t *testing.T) {
|
||||
fileMd5, err := Md5File("./basic.go")
|
||||
assert := internal.NewAssert(t, "TestMd5File")
|
||||
|
||||
@@ -366,21 +366,22 @@ func ExampleHmacSha512() {
|
||||
}
|
||||
|
||||
func ExampleMd5String() {
|
||||
str := "hello"
|
||||
|
||||
md5Str := Md5String(str)
|
||||
|
||||
md5Str := Md5String("hello")
|
||||
fmt.Println(md5Str)
|
||||
|
||||
// Output:
|
||||
// 5d41402abc4b2a76b9719d911017c592
|
||||
}
|
||||
|
||||
func ExampleMd5Byte() {
|
||||
md5Str := Md5Byte([]byte{'a'})
|
||||
fmt.Println(md5Str)
|
||||
|
||||
// Output:
|
||||
// 0cc175b9c0f1b6a831c399e269772661
|
||||
}
|
||||
func ExampleSha1() {
|
||||
str := "hello"
|
||||
|
||||
result := Sha1(str)
|
||||
|
||||
result := Sha1("hello")
|
||||
fmt.Println(result)
|
||||
|
||||
// Output:
|
||||
@@ -388,10 +389,7 @@ func ExampleSha1() {
|
||||
}
|
||||
|
||||
func ExampleSha256() {
|
||||
str := "hello"
|
||||
|
||||
result := Sha256(str)
|
||||
|
||||
result := Sha256("hello")
|
||||
fmt.Println(result)
|
||||
|
||||
// Output:
|
||||
@@ -399,10 +397,7 @@ func ExampleSha256() {
|
||||
}
|
||||
|
||||
func ExampleSha512() {
|
||||
str := "hello"
|
||||
|
||||
result := Sha512(str)
|
||||
|
||||
result := Sha512("hello")
|
||||
fmt.Println(result)
|
||||
|
||||
// Output:
|
||||
|
||||
@@ -96,6 +96,18 @@ func GetNowDateTime() string {
|
||||
return time.Now().Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
// GetTodayStartTime return the start time of today, format: yyyy-mm-dd 00:00:00.
|
||||
// Play: todo
|
||||
func GetTodayStartTime() string {
|
||||
return time.Now().Format("2006-01-02") + " 00:00:00"
|
||||
}
|
||||
|
||||
// GetTodayEndTime return the end time of today, format: yyyy-mm-dd 23:59:59.
|
||||
// Play: todo
|
||||
func GetTodayEndTime() string {
|
||||
return time.Now().Format("2006-01-02") + " 23:59:59"
|
||||
}
|
||||
|
||||
// GetZeroHourTimestamp return timestamp of zero hour (timestamp of 00:00).
|
||||
// Play: https://go.dev/play/p/QmL2oIaGE3q
|
||||
func GetZeroHourTimestamp() int64 {
|
||||
|
||||
@@ -91,6 +91,18 @@ func TestGetNowDateTime(t *testing.T) {
|
||||
assert.Equal(expected, GetNowDateTime())
|
||||
}
|
||||
|
||||
func TestGetTodayStartTime(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestGetTodayStartTime")
|
||||
expected := time.Now().Format("2006-01-02") + " 00:00:00"
|
||||
assert.Equal(expected, GetTodayStartTime())
|
||||
}
|
||||
|
||||
func TestGetTodayEndTime(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestGetTodayEndTime")
|
||||
expected := time.Now().Format("2006-01-02") + " 23:59:59"
|
||||
assert.Equal(expected, GetTodayEndTime())
|
||||
}
|
||||
|
||||
func TestFormatTimeToStr(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestFormatTimeToStr")
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ import (
|
||||
- [HmacSha256](#HmacSha256)
|
||||
- [HmacSha512](#HmacSha512)
|
||||
- [Md5String](#Md5String)
|
||||
- [Md5Byte](#Md5Byte)
|
||||
- [Md5File](#Md5File)
|
||||
- [Sha1](#Sha1)
|
||||
- [Sha256](#Sha256)
|
||||
@@ -880,6 +881,35 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Md5Byte">Md5Byte</span>
|
||||
|
||||
<p>Return the md5 string of byte slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Md5Byte(data []byte) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/cryptor"
|
||||
)
|
||||
|
||||
func main() {
|
||||
md5Str := cryptor.Md5Byte([]byte{'a'})
|
||||
fmt.Println(md5Str)
|
||||
|
||||
// Output:
|
||||
// 0cc175b9c0f1b6a831c399e269772661
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Md5File">Md5File</span>
|
||||
|
||||
<p>Get the md5 value of file.</p>
|
||||
|
||||
@@ -45,6 +45,7 @@ import (
|
||||
- [HmacSha256](#HmacSha256)
|
||||
- [HmacSha512](#HmacSha512)
|
||||
- [Md5String](#Md5String)
|
||||
- [Md5Byte](#Md5Byte)
|
||||
- [Md5File](#Md5File)
|
||||
- [Sha1](#Sha1)
|
||||
- [Sha256](#Sha256)
|
||||
@@ -879,6 +880,35 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Md5Byte">Md5Byte</span>
|
||||
|
||||
<p>获取byte slice的md5至。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Md5Byte(data []byte) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/cryptor"
|
||||
)
|
||||
|
||||
func main() {
|
||||
md5Str := cryptor.Md5Byte([]byte{'a'})
|
||||
fmt.Println(md5Str)
|
||||
|
||||
// Output:
|
||||
// 0cc175b9c0f1b6a831c399e269772661
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Md5File">Md5File</span>
|
||||
|
||||
<p>获取文件md5值。</p>
|
||||
|
||||
@@ -42,6 +42,8 @@ import (
|
||||
- [GetNowDate](#GetNowDate)
|
||||
- [GetNowTime](#GetNowTime)
|
||||
- [GetNowDateTime](#GetNowDateTime)
|
||||
- [GetTodayStartTime](#GetTodayStartTime)
|
||||
- [GetTodayEndTime](#GetTodayEndTime)
|
||||
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
||||
- [GetNightTimestamp](#GetNightTimestamp)
|
||||
- [FormatTimeToStr](#FormatTimeToStr)
|
||||
@@ -643,14 +645,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
currentDate := datetime.GetNowDate()
|
||||
|
||||
fmt.Println(currentDate)
|
||||
|
||||
// Output:
|
||||
@@ -675,14 +674,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
currentTime := datetime.GetNowTime()
|
||||
|
||||
fmt.Println(currentTime) // 15:57:33
|
||||
|
||||
// Output:
|
||||
@@ -707,14 +703,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
current := datetime.GetNowDateTime()
|
||||
|
||||
fmt.Println(current)
|
||||
|
||||
// Output:
|
||||
@@ -722,6 +715,64 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="GetTodayStartTime">GetTodayStartTime</span>
|
||||
|
||||
<p>Return the start time of today, format: yyyy-mm-dd 00:00:00.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func GetTodayStartTime() string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
startTime := datetime.GetTodayStartTime()
|
||||
fmt.Println(startTime)
|
||||
|
||||
// Output:
|
||||
// 2023-06-29 00:00:00
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="GetTodayEndTime">GetTodayEndTime</span>
|
||||
|
||||
<p>Return the end time of today, format: yyyy-mm-dd 23:59:59.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func GetTodayEndTime() string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
endTime := datetime.GetTodayEndTime()
|
||||
fmt.Println(endTime)
|
||||
|
||||
// Output:
|
||||
// 2023-06-29 23:59:59
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
||||
|
||||
<p>Return timestamp of zero hour (timestamp of 00:00).</p>
|
||||
@@ -739,14 +790,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
zeroTime := datetime.GetZeroHourTimestamp()
|
||||
|
||||
fmt.Println(zeroTime)
|
||||
|
||||
// Output:
|
||||
@@ -771,14 +819,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
nightTime := datetime.GetNightTimestamp()
|
||||
|
||||
fmt.Println(nightTime)
|
||||
|
||||
// Output:
|
||||
|
||||
@@ -41,6 +41,8 @@ import (
|
||||
- [GetNowDate](#GetNowDate)
|
||||
- [GetNowTime](#GetNowTime)
|
||||
- [GetNowDateTime](#GetNowDateTime)
|
||||
- [GetTodayStartTime](#GetTodayStartTime)
|
||||
- [GetTodayEndTime](#GetTodayEndTime)
|
||||
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
||||
- [GetNightTimestamp](#GetNightTimestamp)
|
||||
- [FormatTimeToStr](#FormatTimeToStr)
|
||||
@@ -643,14 +645,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
currentDate := datetime.GetNowDate()
|
||||
|
||||
fmt.Println(currentDate)
|
||||
|
||||
// Output:
|
||||
@@ -675,15 +674,12 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
currentTime := datetime.GetNowTime()
|
||||
|
||||
fmt.Println(currentTime) // 15:57:33
|
||||
fmt.Println(currentTime)
|
||||
|
||||
// Output:
|
||||
// 15:57:33
|
||||
@@ -707,14 +703,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
current := datetime.GetNowDateTime()
|
||||
|
||||
fmt.Println(current)
|
||||
|
||||
// Output:
|
||||
@@ -722,6 +715,64 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="GetTodayStartTime">GetTodayStartTime</span>
|
||||
|
||||
<p>返回当天开始时间, 格式: yyyy-mm-dd 00:00:00.</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func GetTodayStartTime() string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
startTime := datetime.GetTodayStartTime()
|
||||
fmt.Println(startTime)
|
||||
|
||||
// Output:
|
||||
// 2023-06-29 00:00:00
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="GetTodayEndTime">GetTodayEndTime</span>
|
||||
|
||||
<p>返回当天结束时间,格式: yyyy-mm-dd 23:59:59.</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func GetTodayEndTime() string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
endTime := datetime.GetTodayEndTime()
|
||||
fmt.Println(endTime)
|
||||
|
||||
// Output:
|
||||
// 2023-06-29 23:59:59
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
||||
|
||||
<p>获取零点时间戳(timestamp of 00:00)</p>
|
||||
@@ -739,14 +790,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
zeroTime := datetime.GetZeroHourTimestamp()
|
||||
|
||||
fmt.Println(zeroTime)
|
||||
|
||||
// Output:
|
||||
@@ -771,14 +819,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
nightTime := datetime.GetNightTimestamp()
|
||||
|
||||
fmt.Println(nightTime)
|
||||
|
||||
// Output:
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func TestTuples(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestTuples")
|
||||
|
||||
type foo struct {
|
||||
@@ -39,6 +40,7 @@ func TestTuples(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTuple_Unbox(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestTuple_Unbox")
|
||||
|
||||
type foo struct {
|
||||
@@ -125,5 +127,69 @@ func TestTuple_Unbox(t *testing.T) {
|
||||
assert.Equal("c", v8)
|
||||
assert.Equal(map[string]int{"a": 1}, v9)
|
||||
assert.Equal(foo{A: "a"}, v10)
|
||||
}
|
||||
|
||||
func TestZip(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestZip")
|
||||
|
||||
r2 := Zip2(
|
||||
[]int{1, 2},
|
||||
[]string{"a", "b"},
|
||||
)
|
||||
assert.Equal(r2, []Tuple2[int, string]{
|
||||
{FieldA: 1, FieldB: "a"},
|
||||
{FieldA: 2, FieldB: "b"},
|
||||
})
|
||||
|
||||
r3 := Zip3(
|
||||
[]int{1, 2, 3},
|
||||
[]string{"a", "b", "c"},
|
||||
[]float64{0.1, 0.2, 0.3},
|
||||
)
|
||||
assert.Equal(r3, []Tuple3[int, string, float64]{
|
||||
{FieldA: 1, FieldB: "a", FieldC: 0.1},
|
||||
{FieldA: 2, FieldB: "b", FieldC: 0.2},
|
||||
{FieldA: 3, FieldB: "c", FieldC: 0.3},
|
||||
})
|
||||
|
||||
r4 := Zip4(
|
||||
[]int{1, 2, 3, 4},
|
||||
[]string{"a", "b", "c", "d"},
|
||||
[]float64{0.1, 0.2, 0.3, 0.4},
|
||||
[]bool{true, true, true, true},
|
||||
)
|
||||
assert.Equal(r4, []Tuple4[int, string, float64, bool]{
|
||||
{FieldA: 1, FieldB: "a", FieldC: 0.1, FieldD: true},
|
||||
{FieldA: 2, FieldB: "b", FieldC: 0.2, FieldD: true},
|
||||
{FieldA: 3, FieldB: "c", FieldC: 0.3, FieldD: true},
|
||||
{FieldA: 4, FieldB: "d", FieldC: 0.4, FieldD: true},
|
||||
})
|
||||
|
||||
r5 := Zip5(
|
||||
[]int{1, 2, 3, 4, 5},
|
||||
[]string{"a", "b", "c", "d", "e"},
|
||||
[]float64{0.1, 0.2, 0.3, 0.4, 0.5},
|
||||
[]bool{true, true, true, true, true},
|
||||
[]int{6, 7, 8, 9, 10},
|
||||
)
|
||||
assert.Equal(r5, []Tuple5[int, string, float64, bool, int]{
|
||||
{FieldA: 1, FieldB: "a", FieldC: 0.1, FieldD: true, FieldE: 6},
|
||||
{FieldA: 2, FieldB: "b", FieldC: 0.2, FieldD: true, FieldE: 7},
|
||||
{FieldA: 3, FieldB: "c", FieldC: 0.3, FieldD: true, FieldE: 8},
|
||||
{FieldA: 4, FieldB: "d", FieldC: 0.4, FieldD: true, FieldE: 9},
|
||||
{FieldA: 5, FieldB: "e", FieldC: 0.5, FieldD: true, FieldE: 10},
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestUnzip(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestUnzip")
|
||||
|
||||
r1, r2, r3 := Unzip3([]Tuple3[string, int, float64]{{FieldA: "a", FieldB: 1, FieldC: 0.1}, {FieldA: "b", FieldB: 2, FieldC: 0.2}})
|
||||
|
||||
assert.Equal(r1, []string{"a", "b"})
|
||||
assert.Equal(r2, []int{1, 2})
|
||||
assert.Equal(r3, []float64{0.1, 0.2})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user