parent
dac1c863d7
commit
eab2cb4793
@ -1,57 +0,0 @@
|
|||||||
package gostring
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGenerateId(t *testing.T) {
|
|
||||||
t.Log(GenerateId(""))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenerateIdAndTime(t *testing.T) {
|
|
||||||
var data = []string{
|
|
||||||
"2022-04-26 15:15:15.244",
|
|
||||||
"2022-04-26 15:21:39.852",
|
|
||||||
"2022-04-26 16:45:12.915",
|
|
||||||
"2022-04-26 16:45:51.664",
|
|
||||||
"2022-04-26 16:46:10.647",
|
|
||||||
"2022-04-26 16:46:35.952",
|
|
||||||
"2022-04-26 16:47:02.211",
|
|
||||||
"2022-04-26 16:48:46.841",
|
|
||||||
"2022-04-26 16:49:05.583",
|
|
||||||
"2022-04-26 16:49:38.909",
|
|
||||||
"2022-04-26 16:50:37.448",
|
|
||||||
"2022-04-26 16:50:58.763",
|
|
||||||
"2022-04-26 16:51:14.672",
|
|
||||||
"2022-04-26 16:52:06.194",
|
|
||||||
"2022-04-26 16:52:33.746",
|
|
||||||
"2022-04-26 16:52:56.281",
|
|
||||||
"2022-04-26 16:53:21.239",
|
|
||||||
"2022-04-26 16:53:50.016",
|
|
||||||
"2022-04-26 16:54:17.313",
|
|
||||||
"2022-04-26 16:54:47.056",
|
|
||||||
"2022-04-26 16:55:12.822",
|
|
||||||
"2022-04-26 16:55:45.031",
|
|
||||||
"2022-04-26 16:56:38.678",
|
|
||||||
"2022-04-26 16:57:00.522",
|
|
||||||
"2022-04-26 16:57:33.240",
|
|
||||||
"2022-04-26 17:28:37.719",
|
|
||||||
}
|
|
||||||
for _, v := range data {
|
|
||||||
t.Log(GenerateIdAndTime("P0", v))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkGenerateId(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
b.Logf("---------%v---------", i)
|
|
||||||
b.Log(GenerateId(""))
|
|
||||||
b.Log(GenerateId("Q"))
|
|
||||||
b.Log(GenerateId("Q1"))
|
|
||||||
b.Log(GenerateId("Q10000"))
|
|
||||||
b.Log(GenerateId("Q172676"))
|
|
||||||
b.Log(GenerateId("Q17267650"))
|
|
||||||
b.Log(GenerateId(strconv.Itoa(i)))
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
go.dtapp.net/gorandom v1.0.1 h1:IWfMClh1ECPvyUjlqD7MwLq4mZdUusD1qAwAdsvEJBs=
|
go.dtapp.net/gorandom v1.0.1 h1:IWfMClh1ECPvyUjlqD7MwLq4mZdUusD1qAwAdsvEJBs=
|
||||||
go.dtapp.net/gorandom v1.0.1/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8=
|
go.dtapp.net/gorandom v1.0.1/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8=
|
||||||
go.dtapp.net/gotime v1.0.4 h1:gOOkGKfhJwX+dsGHvnF2est09Oqvk7cihJkePddJ5a8=
|
go.dtapp.net/gotime v1.0.5 h1:12aNgB2ULpP6QgQHEUkLilZ4ASvhpFxMFQkBwn0par8=
|
||||||
go.dtapp.net/gotime v1.0.4/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY=
|
go.dtapp.net/gotime v1.0.5/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY=
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
package gostring
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GenerateRandomNumber 生成count个[start,end)结束的不重复的随机数
|
||||||
|
func GenerateRandomNumber(start int, end int, count int) []int {
|
||||||
|
|
||||||
|
// 范围检查
|
||||||
|
if end < start || (end-start) < count {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 存放结果的slice
|
||||||
|
nums := make([]int, 0)
|
||||||
|
|
||||||
|
// 随机数生成器,加入时间戳保证每次生成的随机数不一样
|
||||||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
for len(nums) < count {
|
||||||
|
|
||||||
|
// 生成随机数
|
||||||
|
num := r.Intn(end-start) + start
|
||||||
|
|
||||||
|
// 查重
|
||||||
|
exist := false
|
||||||
|
for _, v := range nums {
|
||||||
|
if v == num {
|
||||||
|
exist = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !exist {
|
||||||
|
nums = append(nums, num)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nums
|
||||||
|
}
|
@ -1,66 +0,0 @@
|
|||||||
package gostring
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestName(t *testing.T) {
|
|
||||||
str := "iPhone 11 Pro Max<iPhone12,5>"
|
|
||||||
fmt.Printf("%d\n", strings.LastIndex(str, "<"))
|
|
||||||
fmt.Printf("%d\n", strings.LastIndex(str, "("))
|
|
||||||
fmt.Printf("%d\n", strings.LastIndex("iPad (6th generation, WiFi)<iPad7,5>", "<"))
|
|
||||||
fmt.Printf("%d\n", strings.LastIndex("iPad (6th generation, WiFi)<iPad7,5>", "("))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestToInt64(t *testing.T) {
|
|
||||||
t.Log(ToInt64("120"))
|
|
||||||
t.Log(ToInt64("120.9"))
|
|
||||||
t.Log(strings.Contains("120", ","))
|
|
||||||
t.Log(strings.Contains("120,1", ","))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestString(t *testing.T) {
|
|
||||||
str := "wx6566ef69e8738ad9"
|
|
||||||
fmt.Println(strings.Contains(str, "wx"))
|
|
||||||
myString := "www.5lmh.com"
|
|
||||||
if strings.HasPrefix(myString, "www") {
|
|
||||||
fmt.Println("Hello to you too")
|
|
||||||
} else {
|
|
||||||
fmt.Println("Goodbye")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSplit(t *testing.T) {
|
|
||||||
t.Log(Split("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20", ","))
|
|
||||||
t.Log(Split(",10,", ","))
|
|
||||||
t.Log(len(Split(",10,", ",")))
|
|
||||||
t.Log(Split(",10,", ",")[1 : len(Split(",1,", ","))-1])
|
|
||||||
t.Log(len(Split(",10,", ",")[1 : len(Split(",1,", ","))-1]))
|
|
||||||
t.Log(Contains("", ","))
|
|
||||||
t.Log(len([]string{}))
|
|
||||||
t.Log(Split("/pages/preferential_recharge/goods_details?goods_id=G02022062517457120", "="))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContains(t *testing.T) {
|
|
||||||
t.Log(Contains("1", ","))
|
|
||||||
t.Log(Contains("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20", ","))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestToFloat64(t *testing.T) {
|
|
||||||
t.Log(ToFloat64("120"))
|
|
||||||
t.Log(ToFloat64("120.9"))
|
|
||||||
t.Log(ToFloat64("100.100.100"))
|
|
||||||
if "100.100.100" > "111.111.111" {
|
|
||||||
log.Println("1")
|
|
||||||
} else {
|
|
||||||
log.Println("2")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReplace(t *testing.T) {
|
|
||||||
t.Log(Replace("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20", ",", "|"))
|
|
||||||
t.Log(Replace("/v1/a/{agent_user_id}/d/g", "{agent_user_id}", "A102DFB78FADE96F1E"))
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package gostring
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGetUuId(t *testing.T) {
|
|
||||||
t.Log(GetUuId())
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkGetUuId(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
b.Log(GetUuId())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
kind: pipeline
|
|
||||||
type: docker
|
|
||||||
name: clone
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Test
|
|
||||||
image: golang:1.18
|
|
||||||
commands:
|
|
||||||
- go env -w GO111MODULE=on
|
|
||||||
- go env -w GOPROXY=https://goproxy.cn,direct
|
|
||||||
- go test -v ./...
|
|
||||||
- name: Benchmark
|
|
||||||
image: golang:1.18
|
|
||||||
commands:
|
|
||||||
- go env -w GO111MODULE=on
|
|
||||||
- go env -w GOPROXY=https://goproxy.cn,direct
|
|
||||||
- go test -bench=. -benchmem
|
|
@ -1,9 +0,0 @@
|
|||||||
.env
|
|
||||||
.git
|
|
||||||
.svn
|
|
||||||
.idea
|
|
||||||
.vscode
|
|
||||||
*.log
|
|
||||||
goinit.sh
|
|
||||||
gomod.sh
|
|
||||||
/vendor/
|
|
@ -1,21 +0,0 @@
|
|||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2018 李光春
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
@ -1,17 +0,0 @@
|
|||||||
<h1>
|
|
||||||
<a href="https://www.dtapp.net/">Golang Random</a>
|
|
||||||
</h1>
|
|
||||||
|
|
||||||
📦 Golang Random
|
|
||||||
|
|
||||||
[comment]: <> (go)
|
|
||||||
[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gorandom?status.svg)](https://pkg.go.dev/go.dtapp.net/gorandom)
|
|
||||||
[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/gorandom/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/gorandom)
|
|
||||||
[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/gorandom)](https://goreportcard.com/report/go.dtapp.net/gorandom)
|
|
||||||
[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net%2Fgorandom)
|
|
||||||
|
|
||||||
#### 安装
|
|
||||||
|
|
||||||
```go
|
|
||||||
go get -v -u go.dtapp.net/gorandom
|
|
||||||
```
|
|
@ -1,50 +0,0 @@
|
|||||||
package gorandom
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math/rand"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const numbers string = "0123456789"
|
|
||||||
const letters string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
||||||
const specials = "~!@#$%^*()_+-=[]{}|;:,./<>?"
|
|
||||||
const alphanumerics string = letters + numbers
|
|
||||||
const ascii string = alphanumerics + specials
|
|
||||||
|
|
||||||
func random[T int | int64](n T, chars string) string {
|
|
||||||
if n <= 0 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
bytes := make([]byte, n, n)
|
|
||||||
l := len(chars)
|
|
||||||
var i T = 0
|
|
||||||
for {
|
|
||||||
if i >= n {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
bytes[i] = chars[r.Intn(l)]
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
return string(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Alphanumeric 随机字母数字
|
|
||||||
func Alphanumeric[T int | int64](n T) string {
|
|
||||||
return random(n, alphanumerics)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Alphabetic 随机字母
|
|
||||||
func Alphabetic[T int | int64](n T) string {
|
|
||||||
return random(n, letters)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Numeric 随机数字
|
|
||||||
func Numeric[T int | int64](n T) string {
|
|
||||||
return random(n, numbers)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ascii 随机ASCII
|
|
||||||
func Ascii[T int | int64](n T) string {
|
|
||||||
return random(n, ascii)
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
package gorandom
|
|
||||||
|
|
||||||
const Version = "1.0.1"
|
|
@ -1,17 +0,0 @@
|
|||||||
kind: pipeline
|
|
||||||
type: docker
|
|
||||||
name: clone
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Test
|
|
||||||
image: golang:1.18
|
|
||||||
commands:
|
|
||||||
- go env -w GO111MODULE=on
|
|
||||||
- go env -w GOPROXY=https://goproxy.cn,direct
|
|
||||||
- go test -v ./...
|
|
||||||
- name: Benchmark
|
|
||||||
image: golang:1.18
|
|
||||||
commands:
|
|
||||||
- go env -w GO111MODULE=on
|
|
||||||
- go env -w GOPROXY=https://goproxy.cn,direct
|
|
||||||
- go test -bench=. -benchmem
|
|
@ -1,7 +0,0 @@
|
|||||||
.env
|
|
||||||
.git
|
|
||||||
.svn
|
|
||||||
.idea
|
|
||||||
.vscode
|
|
||||||
.log
|
|
||||||
gomod.sh
|
|
@ -1,21 +0,0 @@
|
|||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2018 李光春
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
@ -1,17 +0,0 @@
|
|||||||
<h1>
|
|
||||||
<a href="https://www.dtapp.net/">Golang Time</a>
|
|
||||||
</h1>
|
|
||||||
|
|
||||||
📦 Golang 时间
|
|
||||||
|
|
||||||
[comment]: <> (go)
|
|
||||||
[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gotime?status.svg)](https://pkg.go.dev/go.dtapp.net/gotime)
|
|
||||||
[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/gotime/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/gotime)
|
|
||||||
[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/gotime)](https://goreportcard.com/report/go.dtapp.net/gotime)
|
|
||||||
[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net%2Fgotime)
|
|
||||||
|
|
||||||
#### 安装
|
|
||||||
|
|
||||||
```go
|
|
||||||
go get -v -u go.dtapp.net/gotime
|
|
||||||
```
|
|
@ -1,65 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
// Gt 是否大于
|
|
||||||
func (p Pro) Gt(t2 time.Time) bool {
|
|
||||||
return p.Time.After(t2)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lt 是否小于
|
|
||||||
func (p Pro) Lt(t2 time.Time) bool {
|
|
||||||
return p.Time.Before(t2)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Eq 是否等于
|
|
||||||
func (p Pro) Eq(t2 time.Time) bool {
|
|
||||||
return p.Time.Equal(t2)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ne 是否不等于
|
|
||||||
func (p Pro) Ne(t2 time.Time) bool {
|
|
||||||
return !p.Eq(t2)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gte 是否大于等于
|
|
||||||
func (p Pro) Gte(t2 time.Time) bool {
|
|
||||||
return p.Gt(t2) || p.Eq(t2)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lte 是否小于等于
|
|
||||||
func (p Pro) Lte(t2 time.Time) bool {
|
|
||||||
return p.Lt(t2) || p.Eq(t2)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Between 是否在两个时间之间(不包括这两个时间)
|
|
||||||
func (p Pro) Between(start time.Time, end time.Time) bool {
|
|
||||||
if p.Gt(start) && p.Lt(end) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// BetweenIncludedStart 是否在两个时间之间(包括开始时间)
|
|
||||||
func (p Pro) BetweenIncludedStart(start time.Time, end time.Time) bool {
|
|
||||||
if p.Gte(start) && p.Lt(end) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// BetweenIncludedEnd 是否在两个时间之间(包括结束时间)
|
|
||||||
func (p Pro) BetweenIncludedEnd(start time.Time, end time.Time) bool {
|
|
||||||
if p.Gt(start) && p.Lte(end) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// BetweenIncludedBoth 是否在两个时间之间(包括这两个时间)
|
|
||||||
func (p Pro) BetweenIncludedBoth(start time.Time, end time.Time) bool {
|
|
||||||
if p.Gte(start) && p.Lte(end) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
@ -1,69 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
// DiffInHour 相差多少小时
|
|
||||||
func (p Pro) DiffInHour(t2 time.Time) (hour int64) {
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff := p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff / 3600
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInHourWithAbs 相差多少小时(绝对值)
|
|
||||||
func (p Pro) DiffInHourWithAbs(t2 time.Time) (hour int64) {
|
|
||||||
p.Time.Before(t2)
|
|
||||||
diff := t2.Unix() - p.Time.Unix()
|
|
||||||
hour = diff / 3600
|
|
||||||
if hour > 0 {
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff = p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff / 3600
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInMinutes 相差多少分钟
|
|
||||||
func (p Pro) DiffInMinutes(t2 time.Time) (hour int64) {
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff := p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff / 60
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInMinutesWithAbs 相差多少分钟(绝对值)
|
|
||||||
func (p Pro) DiffInMinutesWithAbs(t2 time.Time) (hour int64) {
|
|
||||||
p.Time.Before(t2)
|
|
||||||
diff := t2.Unix() - p.Time.Unix()
|
|
||||||
hour = diff / 60
|
|
||||||
if hour > 0 {
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff = p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff / 60
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInSecond 相差多少秒
|
|
||||||
func (p Pro) DiffInSecond(t2 time.Time) (hour int64) {
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff := p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
|
|
||||||
// DiffInSecondWithAbs 相差多少秒(绝对值)
|
|
||||||
func (p Pro) DiffInSecondWithAbs(t2 time.Time) (hour int64) {
|
|
||||||
p.Time.Before(t2)
|
|
||||||
diff := t2.Unix() - p.Time.Unix()
|
|
||||||
hour = diff
|
|
||||||
if hour > 0 {
|
|
||||||
return hour
|
|
||||||
}
|
|
||||||
t2.Before(p.Time)
|
|
||||||
diff = p.Time.Unix() - t2.Unix()
|
|
||||||
hour = diff
|
|
||||||
return hour
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// invalidTimezoneError 无效的时区错误
|
|
||||||
var invalidTimezoneError = func(timezone string) error {
|
|
||||||
return fmt.Errorf("invalid timezone %q, please see the file %q for all valid timezones", timezone, "$GOROOT/lib/time/zoneinfo.zip")
|
|
||||||
}
|
|
@ -1,105 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 时间格式化常量
|
|
||||||
const (
|
|
||||||
RFC3339Format = time.RFC3339
|
|
||||||
Iso8601Format = "2006-01-02T15:04:05-07:00"
|
|
||||||
CookieFormat = "Monday, 02-Jan-2006 15:04:05 MST"
|
|
||||||
RFC1036Format = "Mon, 02 Jan 06 15:04:05 -0700"
|
|
||||||
RFC7231Format = "Mon, 02 Jan 2006 15:04:05 GMT"
|
|
||||||
DayDateTimeFormat = "Mon, Jan 2, 2006 3:04 PM"
|
|
||||||
DateTimeFormat = "2006-01-02 15:04:05"
|
|
||||||
DateFormat = "2006-01-02"
|
|
||||||
TimeFormat = "15:04:05"
|
|
||||||
ShortDateTimeFormat = "20060102150405"
|
|
||||||
ShortDateFormat = "20060102"
|
|
||||||
ShortTimeFormat = "150405"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Pro 结构体
|
|
||||||
type Pro struct {
|
|
||||||
Time time.Time
|
|
||||||
loc *time.Location
|
|
||||||
Error error
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewPro 初始化结构体
|
|
||||||
func NewPro() Pro {
|
|
||||||
return Pro{
|
|
||||||
Time: time.Now(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BeforeSeconds 获取n秒前的时间
|
|
||||||
func (p Pro) BeforeSeconds(seconds int64) Pro {
|
|
||||||
st, _ := time.ParseDuration(fmt.Sprintf("-%ds", seconds))
|
|
||||||
p.Time = p.Time.Add(st)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// AfterSeconds 获取n秒后的时间
|
|
||||||
func (p Pro) AfterSeconds(seconds int64) Pro {
|
|
||||||
st, _ := time.ParseDuration(fmt.Sprintf("+%ds", seconds))
|
|
||||||
p.Time = p.Time.Add(st)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// BeforeMinute 获取n分钟前的时间
|
|
||||||
func (p Pro) BeforeMinute(seconds int64) Pro {
|
|
||||||
st, _ := time.ParseDuration(fmt.Sprintf("-%dm", seconds))
|
|
||||||
p.Time = p.Time.Add(st)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// AfterMinute 获取n分钟后的时间
|
|
||||||
func (p Pro) AfterMinute(seconds int64) Pro {
|
|
||||||
st, _ := time.ParseDuration(fmt.Sprintf("+%dm", seconds))
|
|
||||||
p.Time = p.Time.Add(st)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// BeforeHour 获取n小时前的时间
|
|
||||||
func (p Pro) BeforeHour(hour int64) Pro {
|
|
||||||
st, _ := time.ParseDuration(fmt.Sprintf("-%dh", hour))
|
|
||||||
p.Time = p.Time.Add(st)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// AfterHour 获取n小时后的时间
|
|
||||||
func (p Pro) AfterHour(hour int64) Pro {
|
|
||||||
st, _ := time.ParseDuration(fmt.Sprintf("+%dh", hour))
|
|
||||||
p.Time = p.Time.Add(st)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// BeforeDay 获取n天前的时间
|
|
||||||
func (p Pro) BeforeDay(day int) Pro {
|
|
||||||
p.Time = p.Time.AddDate(0, 0, -day)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// AfterDay 获取n天后的时间
|
|
||||||
func (p Pro) AfterDay(day int) Pro {
|
|
||||||
p.Time = p.Time.AddDate(0, 0, day)
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetFormat 格式化
|
|
||||||
func (p Pro) SetFormat(layout string) string {
|
|
||||||
return p.Time.Format(layout)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Month 获取当前月
|
|
||||||
func (p Pro) Month() int64 {
|
|
||||||
return p.MonthOfYear()
|
|
||||||
}
|
|
||||||
|
|
||||||
// MonthOfYear 获取本年的第几月
|
|
||||||
func (p Pro) MonthOfYear() int64 {
|
|
||||||
return int64(p.Time.In(p.loc).Month())
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 通过时区获取 Location 实例
|
|
||||||
func getLocationByTimezone(timezone string) (*time.Location, error) {
|
|
||||||
loc, err := time.LoadLocation(timezone)
|
|
||||||
if err != nil {
|
|
||||||
err = invalidTimezoneError(timezone)
|
|
||||||
}
|
|
||||||
return loc, err
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
// Bson mongoDB
|
|
||||||
func (p Pro) Bson() string {
|
|
||||||
return p.Now().String()
|
|
||||||
}
|
|
@ -1,124 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
// 数字常量
|
|
||||||
const (
|
|
||||||
YearsPerMillennium = 1000 // 每千年1000年
|
|
||||||
YearsPerCentury = 100 // 每世纪100年
|
|
||||||
YearsPerDecade = 10 // 每十年10年
|
|
||||||
QuartersPerYear = 4 // 每年4季度
|
|
||||||
MonthsPerYear = 12 // 每年12月
|
|
||||||
MonthsPerQuarter = 3 // 每季度3月
|
|
||||||
WeeksPerNormalYear = 52 // 每常规年52周
|
|
||||||
weeksPerLongYear = 53 // 每长年53周
|
|
||||||
WeeksPerMonth = 4 // 每月4周
|
|
||||||
DaysPerLeapYear = 366 // 每闰年366天
|
|
||||||
DaysPerNormalYear = 365 // 每常规年365天
|
|
||||||
DaysPerWeek = 7 // 每周7天
|
|
||||||
HoursPerWeek = 168 // 每周168小时
|
|
||||||
HoursPerDay = 24 // 每天24小时
|
|
||||||
MinutesPerDay = 1440 // 每天1440分钟
|
|
||||||
MinutesPerHour = 60 // 每小时60分钟
|
|
||||||
SecondsPerWeek = 604800 // 每周604800秒
|
|
||||||
SecondsPerDay = 86400 // 每天86400秒
|
|
||||||
SecondsPerHour = 3600 // 每小时3600秒
|
|
||||||
SecondsPerMinute = 60 // 每分钟60秒
|
|
||||||
MillisecondsPerSecond = 1000 // 每秒1000毫秒
|
|
||||||
MicrosecondsPerMillisecond = 1000 // 每毫秒1000微秒
|
|
||||||
MicrosecondsPerSecond = 1000000 // 每秒1000000微秒
|
|
||||||
)
|
|
||||||
|
|
||||||
// StartOfCentury 本世纪开始时间
|
|
||||||
func (p Pro) StartOfCentury() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year()/YearsPerCentury*YearsPerCentury, 1, 1, 0, 0, 0, 0, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// EndOfCentury 本世纪结束时间
|
|
||||||
func (p Pro) EndOfCentury() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year()/YearsPerCentury*YearsPerCentury+99, 12, 31, 23, 59, 59, 999999999, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartOfDecade 本年代开始时间
|
|
||||||
func (p Pro) StartOfDecade() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year()/YearsPerDecade*YearsPerDecade, 1, 1, 0, 0, 0, 0, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// EndOfDecade 本年代结束时间
|
|
||||||
func (p Pro) EndOfDecade() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year()/YearsPerDecade*YearsPerDecade+9, 12, 31, 23, 59, 59, 999999999, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartOfYear 本年开始时间
|
|
||||||
func (p Pro) StartOfYear() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year(), 1, 1, 0, 0, 0, 0, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// EndOfYear 本年结束时间
|
|
||||||
func (p Pro) EndOfYear() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year(), 12, 31, 23, 59, 59, 999999999, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// Quarter 获取当前季度
|
|
||||||
func (p Pro) Quarter() (quarter int) {
|
|
||||||
switch {
|
|
||||||
case p.Time.Month() >= 10:
|
|
||||||
quarter = 4
|
|
||||||
case p.Time.Month() >= 7:
|
|
||||||
quarter = 3
|
|
||||||
case p.Time.Month() >= 4:
|
|
||||||
quarter = 2
|
|
||||||
case p.Time.Month() >= 1:
|
|
||||||
quarter = 1
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartOfQuarter 本季度开始时间
|
|
||||||
func (p Pro) StartOfQuarter() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year(), time.Month(3*p.Quarter()-2), 1, 0, 0, 0, 0, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// EndOfQuarter 本季度结束时间
|
|
||||||
func (p Pro) EndOfQuarter() Pro {
|
|
||||||
quarter, day := p.Quarter(), 30
|
|
||||||
switch quarter {
|
|
||||||
case 1, 4:
|
|
||||||
day = 31
|
|
||||||
case 2, 3:
|
|
||||||
day = 30
|
|
||||||
}
|
|
||||||
p.Time = time.Date(p.Time.Year(), time.Month(3*quarter), day, 23, 59, 59, 999999999, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartOfMonth 本月开始时间
|
|
||||||
func (p Pro) StartOfMonth() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year(), time.Month(p.Month()), 1, 0, 0, 0, 0, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// EndOfMonth 本月结束时间
|
|
||||||
func (p Pro) EndOfMonth() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year(), time.Month(p.Month()), 1, 23, 59, 59, 999999999, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartOfDay 本日开始时间
|
|
||||||
func (p Pro) StartOfDay() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year(), p.Time.Month(), p.Time.Day(), 0, 0, 0, 0, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
// EndOfDay 本日结束时间
|
|
||||||
func (p Pro) EndOfDay() Pro {
|
|
||||||
p.Time = time.Date(p.Time.Year(), p.Time.Month(), p.Time.Day(), 23, 59, 59, 0, p.Time.Location())
|
|
||||||
return p
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
// Tomorrow 明天
|
|
||||||
func Tomorrow() Pro {
|
|
||||||
p := NewPro()
|
|
||||||
location, err := time.LoadLocation("Asia/Shanghai")
|
|
||||||
if err != nil {
|
|
||||||
p.Time = time.Now().Add(time.Hour*8).AddDate(0, 0, +1)
|
|
||||||
} else {
|
|
||||||
p.Time = time.Now().In(location).AddDate(0, 0, +1)
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
const Version = "1.0.4"
|
|
@ -1,17 +0,0 @@
|
|||||||
package gotime
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Yesterday 昨天
|
|
||||||
func Yesterday() Pro {
|
|
||||||
p := NewPro()
|
|
||||||
location, err := time.LoadLocation("Asia/Shanghai")
|
|
||||||
if err != nil {
|
|
||||||
p.Time = time.Now().Add(time.Hour*8).AddDate(0, 0, -1)
|
|
||||||
} else {
|
|
||||||
p.Time = time.Now().In(location).AddDate(0, 0, -1)
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
# go.dtapp.net/gorandom v1.0.1
|
|
||||||
## explicit; go 1.18
|
|
||||||
go.dtapp.net/gorandom
|
|
||||||
# go.dtapp.net/gotime v1.0.4
|
|
||||||
## explicit; go 1.18
|
|
||||||
go.dtapp.net/gotime
|
|
@ -1,3 +0,0 @@
|
|||||||
package gostring
|
|
||||||
|
|
||||||
const Version = "1.0.6"
|
|
@ -1,13 +0,0 @@
|
|||||||
package gostring
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestVersion(t *testing.T) {
|
|
||||||
t.Log(Version)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkVersion(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
b.Log(Version)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue