初始化项目
This commit is contained in:
87
trunk/goutil/timeUtil/check.go
Normal file
87
trunk/goutil/timeUtil/check.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package timeUtil
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 检查一个日期的时分秒 是否在指定的时间范围内
|
||||
// checkTime:待检查的时间
|
||||
// timeSpan1:时间范围1
|
||||
// addSecond1:需要加上的时间偏差值
|
||||
// timeSpan2:时间范围2
|
||||
// addSecond2:需要加上的时间偏差值
|
||||
func CheckIfInRange(checkTime time.Time, timeSpan1 string, addSecond1 int, timeSpan2 string, addSecond2 int) bool {
|
||||
var (
|
||||
hour1, minute1, second1 int
|
||||
hour2, minute2, second2 int
|
||||
)
|
||||
|
||||
// 取出字符串的时分秒
|
||||
_, hour1, minute1, second1 = ParseTimeString(timeSpan1)
|
||||
_, hour2, minute2, second2 = ParseTimeString(timeSpan2)
|
||||
|
||||
// 取出当前时间的时分秒
|
||||
checkTime = checkTime.Local()
|
||||
hour := checkTime.Hour()
|
||||
minute := checkTime.Minute()
|
||||
second := checkTime.Second()
|
||||
|
||||
// 转换成时间值
|
||||
val := int64(time.Hour)*int64(hour) + int64(time.Minute)*int64(minute) + int64(time.Second)*int64(second)
|
||||
val1 := int64(time.Hour)*int64(hour1) + int64(time.Minute)*int64(minute1) + int64(time.Second)*int64((second1+addSecond1))
|
||||
val2 := int64(time.Hour)*int64(hour2) + int64(time.Minute)*int64(minute2) + int64(time.Second)*int64((second2+addSecond2))
|
||||
|
||||
if val1 <= val && val <= val2 {
|
||||
return true
|
||||
}
|
||||
|
||||
if val2 <= val && val <= val1 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 检查一个日期的时分秒 是否在指定的时间范围内
|
||||
// checkTime:待检查的时间
|
||||
// timeSpan1:时间范围1
|
||||
// timeSpan2:时间范围2
|
||||
func CheckIfInRange2(checkTime time.Time, timeSpan1 string, timeSpan2 string) bool {
|
||||
return CheckIfInRange(checkTime, timeSpan1, 0, timeSpan2, 0)
|
||||
}
|
||||
|
||||
// 检查两个日期是否在同一天
|
||||
// time1:时间1
|
||||
// time2:时间2
|
||||
// 返回值:
|
||||
// bool:true:在同一天 false:不在同一天
|
||||
func CheckIfInSameDate(time1, time2 time.Time) bool {
|
||||
y1, m1, d1 := time1.Date()
|
||||
y2, m2, d2 := time2.Date()
|
||||
|
||||
return y1 == y2 && m1 == m2 && d1 == d2
|
||||
}
|
||||
|
||||
/*
|
||||
下面的时间判断函数,是为了解决time.Time的比较问题
|
||||
可以去掉单调时钟的影响
|
||||
see: https://cloud.tencent.com/developer/article/2129493
|
||||
*/
|
||||
|
||||
// After 比较两个时间,t1是否在t2之后,如果是,返回true,否则返回false
|
||||
// 封装这个方法的原因是,可以去掉单调时钟的影响
|
||||
func After(t1, t2 time.Time) bool {
|
||||
return t1.Local().After(t2.Local())
|
||||
}
|
||||
|
||||
// Before 比较两个时间,t1是否在t2之前,如果是,返回true,否则返回false
|
||||
// 封装这个方法的原因是,可以去掉单调时钟的影响
|
||||
func Before(t1, t2 time.Time) bool {
|
||||
return t1.Local().Before(t2.Local())
|
||||
}
|
||||
|
||||
// Equal 比较两个时间,t1是否等于t2,如果是,返回true,否则返回false
|
||||
// 封装这个方法的原因是,可以去掉单调时钟的影响
|
||||
func Equal(t1, t2 time.Time) bool {
|
||||
return t1.Local().Equal(t2.Local())
|
||||
}
|
||||
227
trunk/goutil/timeUtil/check_test.go
Normal file
227
trunk/goutil/timeUtil/check_test.go
Normal file
@@ -0,0 +1,227 @@
|
||||
package timeUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 检查是否在范围内的常规测试
|
||||
func TestCheckIfInRange(t *testing.T) {
|
||||
// Check with correct data
|
||||
timeSpan1 := "13:00:00"
|
||||
timeSpan2 := "21:00:00"
|
||||
|
||||
t1, err := ToDateTime("2017-09-04 15:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange(t1, timeSpan1, 0, timeSpan2, 0) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange(t1, timeSpan2, 0, timeSpan1, 0) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange(t1, timeSpan1, 0, timeSpan2, 5) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange(t1, timeSpan2, 0, timeSpan1, 5) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
// Check with incorrect data
|
||||
t1, err = ToDateTime("2017-09-04 22:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange(t1, timeSpan1, 0, timeSpan2, 0) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange(t1, timeSpan2, 0, timeSpan1, 0) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckIfInRange2(t *testing.T) {
|
||||
timeSpan1 := "13:00:00"
|
||||
timeSpan2 := "21:00:00"
|
||||
|
||||
t1, err := ToDateTime("2017-09-04 15:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange2(t1, timeSpan1, timeSpan2) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange2(t1, timeSpan2, timeSpan1) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange2(t1, timeSpan1, timeSpan2) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange2(t1, timeSpan2, timeSpan1) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
t1, err = ToDateTime("2017-09-04 22:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange2(t1, timeSpan1, timeSpan2) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInRange2(t1, timeSpan2, timeSpan1) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckIfInSameDate(t *testing.T) {
|
||||
// Check with the same date time
|
||||
t1, err := ToDateTime("2017-09-04 00:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t2, err := ToDateTime("2017-09-04 15:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInSameDate(t1, t2) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
// Check with different date time
|
||||
t2, err = ToDateTime("2019-12-26 15:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if CheckIfInSameDate(t1, t2) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestAfter(t *testing.T) {
|
||||
// Check with the same date time
|
||||
t1, err := ToDateTime("2017-09-04 02:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t1 = t1.In(time.UTC)
|
||||
|
||||
t2, err := ToDateTime("2017-09-04 01:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t2 = t2.In(time.Local)
|
||||
|
||||
if After(t2, t1) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if After(t1, t2) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestBefore(t *testing.T) {
|
||||
// Check with the same date time
|
||||
t1, err := ToDateTime("2017-09-04 02:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t1 = t1.In(time.UTC)
|
||||
|
||||
t2, err := ToDateTime("2017-09-04 01:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t2 = t2.In(time.Local)
|
||||
|
||||
if Before(t2, t1) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if Before(t1, t2) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestEqual(t *testing.T) {
|
||||
// Check with the same date time
|
||||
t1, err := ToDateTime("2017-09-04 02:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t1 = t1.In(time.UTC)
|
||||
|
||||
t2, err := ToDateTime("2017-09-04 01:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t2 = t2.In(time.Local)
|
||||
|
||||
t3, err := ToDateTime("2017-09-04 01:00:00")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if Equal(t2, t1) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if Equal(t1, t2) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if Equal(t2, t3) == false {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
102
trunk/goutil/timeUtil/convert.go
Normal file
102
trunk/goutil/timeUtil/convert.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package timeUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 将字符串转换为标准的时间格式
|
||||
// str:输入,格式为:2015-10-25T17:07:30
|
||||
// 返回值:
|
||||
// 标准时间格式对象
|
||||
// 错误对象
|
||||
func ConverToStandardFormat(str string) (result time.Time, err error) {
|
||||
newStr := strings.Replace(str, "T", ":", -1)
|
||||
newStr = strings.Replace(newStr, "-", ":", -1)
|
||||
newStr = strings.Replace(newStr, ".", ":", -1)
|
||||
|
||||
slice := strings.Split(newStr, ":")
|
||||
slice = slice[:6] // 只取前6位(表示年-月-日 时:分:秒)
|
||||
|
||||
intSlice := make([]int, len(slice))
|
||||
for index, item := range slice {
|
||||
if intItem, err1 := strconv.Atoi(item); err1 != nil {
|
||||
err = fmt.Errorf("输入字符串的格式错误:%s,转换后的格式为:%s", str, newStr)
|
||||
return
|
||||
} else {
|
||||
intSlice[index] = intItem
|
||||
}
|
||||
}
|
||||
|
||||
result = time.Date(intSlice[0], time.Month(intSlice[1]), intSlice[2], intSlice[3], intSlice[4], intSlice[5], 0, time.Local)
|
||||
return
|
||||
}
|
||||
|
||||
// 将时间转换为int类型(20160120,共8位)
|
||||
// t:时间
|
||||
// 返回值:
|
||||
// int类型的数字
|
||||
func ConvertToInt(t time.Time) int {
|
||||
year := int(t.Year())
|
||||
month := int(t.Month())
|
||||
day := int(t.Day())
|
||||
|
||||
return year*10e3 + month*10e1 + day
|
||||
}
|
||||
|
||||
// 计算两个时间的日期差值
|
||||
func SubDay(time1, time2 time.Time) int {
|
||||
// 当前时间距离00:00:00的秒数
|
||||
awayFromZero := func(val time.Time) int64 {
|
||||
hour := val.Hour()
|
||||
minute := val.Minute()
|
||||
second := val.Second()
|
||||
return int64(hour*3600 + minute*60 + second)
|
||||
}
|
||||
|
||||
// 每天对应的秒数
|
||||
var eachDaySecond int64 = 24 * 3600
|
||||
|
||||
// 计算出两个时间对应的00:00:00时的时间戳
|
||||
unix1 := time1.Unix() - awayFromZero(time1)
|
||||
unix2 := time2.Unix() - awayFromZero(time2)
|
||||
|
||||
if unix1 < unix2 {
|
||||
return int((unix2 - unix1) / eachDaySecond)
|
||||
} else {
|
||||
return int((unix1 - unix2) / eachDaySecond)
|
||||
}
|
||||
}
|
||||
|
||||
// 解析时间字符串,要求时间格式形式为:12:59:59 这种形式
|
||||
// timeStr:时间字符串
|
||||
// 返回值:
|
||||
// err:错误信息
|
||||
// hour:小时值
|
||||
// minute:分钟值
|
||||
// second:秒数
|
||||
func ParseTimeString(timeStr string) (err error, hour int, minute int, second int) {
|
||||
timeSlice := strings.Split(timeStr, ":")
|
||||
if len(timeSlice) != 3 {
|
||||
err = fmt.Errorf("时间字符串格式不正确:%v", timeStr)
|
||||
return
|
||||
}
|
||||
|
||||
hour, _ = strconv.Atoi(timeSlice[0])
|
||||
minute, _ = strconv.Atoi(timeSlice[1])
|
||||
second, _ = strconv.Atoi(timeSlice[2])
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 获取时间的日期值
|
||||
// timeVal:时间值
|
||||
// 返回值:
|
||||
// time.Time:日期值
|
||||
func GetDate(timeVal time.Time) time.Time {
|
||||
year, month, day := timeVal.Date()
|
||||
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, timeVal.Location())
|
||||
}
|
||||
57
trunk/goutil/timeUtil/convert_test.go
Normal file
57
trunk/goutil/timeUtil/convert_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package timeUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestConverToStandardFormat(t *testing.T) {
|
||||
str := "2018-10-10T10:10:10"
|
||||
expected := time.Date(2018, 10, 10, 10, 10, 10, 0, time.Local)
|
||||
|
||||
got, err := ConverToStandardFormat(str)
|
||||
if err != nil {
|
||||
t.Errorf("发生错误,错误信息为:%s", err)
|
||||
}
|
||||
|
||||
if got != expected {
|
||||
t.Errorf("转换不正确,期待:%s, 实际:%s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertToInt(t *testing.T) {
|
||||
date := time.Date(2018, 10, 10, 10, 10, 10, 0, time.Local)
|
||||
finalInt := ConvertToInt(date)
|
||||
expecteInt := 20181010
|
||||
|
||||
if finalInt != expecteInt {
|
||||
t.Errorf("转换不正确,期待:%d, 实际:%d", expecteInt, finalInt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubDay(t *testing.T) {
|
||||
time1 := time.Now().AddDate(0, 0, 5)
|
||||
time2 := time.Now()
|
||||
expected := 5
|
||||
|
||||
got := SubDay(time1, time2)
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but now got %d.", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTimeString(t *testing.T) {
|
||||
val := "12:13:14"
|
||||
expectedHour := 12
|
||||
expectedMinute := 13
|
||||
expectedSecond := 14
|
||||
|
||||
err, hour, miniute, second := ParseTimeString(val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if expectedHour != hour || expectedMinute != miniute || expectedSecond != second {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
4
trunk/goutil/timeUtil/doc.go
Normal file
4
trunk/goutil/timeUtil/doc.go
Normal file
@@ -0,0 +1,4 @@
|
||||
/*
|
||||
提供时间相关的一些助手方法
|
||||
*/
|
||||
package timeUtil
|
||||
259
trunk/goutil/timeUtil/format.go
Normal file
259
trunk/goutil/timeUtil/format.go
Normal file
@@ -0,0 +1,259 @@
|
||||
package timeUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"goutil/stringUtil"
|
||||
)
|
||||
|
||||
// format time like java, such as: yyyy-MM-dd HH:mm:ss
|
||||
// t:时间
|
||||
// format:格式化字符串
|
||||
// 返回值:
|
||||
// 格式化后的字符串
|
||||
func Format(t time.Time, format string) string {
|
||||
//year
|
||||
if strings.ContainsAny(format, "y") {
|
||||
year := strconv.Itoa(t.Year())
|
||||
|
||||
if strings.Count(format, "yy") == 1 && strings.Count(format, "y") == 2 {
|
||||
format = strings.Replace(format, "yy", year[2:], 1)
|
||||
} else if strings.Count(format, "yyyy") == 1 && strings.Count(format, "y") == 4 {
|
||||
format = strings.Replace(format, "yyyy", year, 1)
|
||||
} else {
|
||||
panic("format year error! please 'yyyy' or 'yy'")
|
||||
}
|
||||
}
|
||||
|
||||
//month
|
||||
if strings.ContainsAny(format, "M") {
|
||||
var month string
|
||||
|
||||
if int(t.Month()) < 10 {
|
||||
month = "0" + strconv.Itoa(int(t.Month()))
|
||||
} else {
|
||||
month = strconv.Itoa(int(t.Month()))
|
||||
}
|
||||
|
||||
if strings.Count(format, "MM") == 1 && strings.Count(format, "M") == 2 {
|
||||
format = strings.Replace(format, "MM", month, 1)
|
||||
} else {
|
||||
panic("format month error! please 'MM'")
|
||||
}
|
||||
}
|
||||
|
||||
//day
|
||||
if strings.ContainsAny(format, "d") {
|
||||
var day string
|
||||
|
||||
if t.Day() < 10 {
|
||||
day = "0" + strconv.Itoa(t.Day())
|
||||
} else {
|
||||
day = strconv.Itoa(t.Day())
|
||||
}
|
||||
|
||||
if strings.Count(format, "dd") == 1 && strings.Count(format, "d") == 2 {
|
||||
format = strings.Replace(format, "dd", day, 1)
|
||||
} else {
|
||||
panic("format day error! please 'dd'")
|
||||
}
|
||||
}
|
||||
|
||||
//hour
|
||||
if strings.ContainsAny(format, "H") {
|
||||
var hour string
|
||||
|
||||
if t.Hour() < 10 {
|
||||
hour = "0" + strconv.Itoa(t.Hour())
|
||||
} else {
|
||||
hour = strconv.Itoa(t.Hour())
|
||||
}
|
||||
|
||||
if strings.Count(format, "HH") == 1 && strings.Count(format, "H") == 2 {
|
||||
format = strings.Replace(format, "HH", hour, 1)
|
||||
} else {
|
||||
panic("format hour error! please 'HH'")
|
||||
}
|
||||
}
|
||||
|
||||
//minute
|
||||
if strings.ContainsAny(format, "m") {
|
||||
var minute string
|
||||
|
||||
if t.Minute() < 10 {
|
||||
minute = "0" + strconv.Itoa(t.Minute())
|
||||
} else {
|
||||
minute = strconv.Itoa(t.Minute())
|
||||
}
|
||||
if strings.Count(format, "mm") == 1 && strings.Count(format, "m") == 2 {
|
||||
format = strings.Replace(format, "mm", minute, 1)
|
||||
} else {
|
||||
panic("format minute error! please 'mm'")
|
||||
}
|
||||
}
|
||||
|
||||
//second
|
||||
if strings.ContainsAny(format, "s") {
|
||||
var second string
|
||||
|
||||
if t.Second() < 10 {
|
||||
second = "0" + strconv.Itoa(t.Second())
|
||||
} else {
|
||||
second = strconv.Itoa(t.Second())
|
||||
}
|
||||
|
||||
if strings.Count(format, "ss") == 1 && strings.Count(format, "s") == 2 {
|
||||
format = strings.Replace(format, "ss", second, 1)
|
||||
} else {
|
||||
panic("format second error! please 'ss'")
|
||||
}
|
||||
}
|
||||
|
||||
return format
|
||||
}
|
||||
|
||||
// 转换成日期字符串
|
||||
// timeVal:待转换的时间
|
||||
// 返回值:
|
||||
// string:格式形如:2016-10-10
|
||||
/*
|
||||
前面是含义,后面是 go 的表示值,多种表示,逗号","分割
|
||||
月份 1,01,Jan,January
|
||||
日 2,02,_2
|
||||
时 3,03,15,PM,pm,AM,am
|
||||
分 4,04
|
||||
秒 5,05
|
||||
年 06,2006
|
||||
时区 -07,-0700,Z0700,Z07:00,-07:00,MST
|
||||
周几 Mon,Monday
|
||||
*/
|
||||
func ToDateString(timeVal time.Time) string {
|
||||
return timeVal.Local().Format("2006-01-02")
|
||||
}
|
||||
|
||||
// 忽略时区,转换成日期字符串
|
||||
// timeVal:待转换的时间
|
||||
// 返回值:
|
||||
// string:格式形如:2016-10-10
|
||||
/*
|
||||
前面是含义,后面是 go 的表示值,多种表示,逗号","分割
|
||||
月份 1,01,Jan,January
|
||||
日 2,02,_2
|
||||
时 3,03,15,PM,pm,AM,am
|
||||
分 4,04
|
||||
秒 5,05
|
||||
年 06,2006
|
||||
时区 -07,-0700,Z0700,Z07:00,-07:00,MST
|
||||
周几 Mon,Monday
|
||||
*/
|
||||
func ToDateString2(timeVal time.Time) string {
|
||||
return timeVal.Format("2006-01-02")
|
||||
}
|
||||
|
||||
// 以本地时区为准,转换成时间字符串
|
||||
// timeVal:待转换的时间
|
||||
// 返回值:
|
||||
// string:格式形如:2016-10-10 10:10:10
|
||||
/*
|
||||
前面是含义,后面是 go 的表示值,多种表示,逗号","分割
|
||||
月份 1,01,Jan,January
|
||||
日 2,02,_2
|
||||
时 3,03,15,PM,pm,AM,am
|
||||
分 4,04
|
||||
秒 5,05
|
||||
年 06,2006
|
||||
时区 -07,-0700,Z0700,Z07:00,-07:00,MST
|
||||
周几 Mon,Monday
|
||||
*/
|
||||
func ToDateTimeString(timeVal time.Time) string {
|
||||
return ToDateTimeStringEx(timeVal, false)
|
||||
}
|
||||
|
||||
func ToDateTimeStringEx(timeVal time.Time, flagT bool) string {
|
||||
if flagT {
|
||||
val := timeVal.Local().Format("2006-01-02 15:04:05")
|
||||
return strings.Replace(val, " ", "T", -1)
|
||||
}
|
||||
|
||||
return timeVal.Local().Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
// 忽略时区,转换成时间字符串
|
||||
// timeVal:待转换的时间
|
||||
// 返回值:
|
||||
// string:格式形如:2016-10-10 10:10:10
|
||||
/*
|
||||
前面是含义,后面是 go 的表示值,多种表示,逗号","分割
|
||||
月份 1,01,Jan,January
|
||||
日 2,02,_2
|
||||
时 3,03,15,PM,pm,AM,am
|
||||
分 4,04
|
||||
秒 5,05
|
||||
年 06,2006
|
||||
时区 -07,-0700,Z0700,Z07:00,-07:00,MST
|
||||
周几 Mon,Monday
|
||||
*/
|
||||
func ToDateTimeString2(timeVal time.Time) string {
|
||||
return ToDateTimeStringEx2(timeVal, false)
|
||||
}
|
||||
|
||||
// 日期和时间中间带T方式
|
||||
func ToDateTimeStringEx2(timeVal time.Time, flagT bool) string {
|
||||
if flagT {
|
||||
val := timeVal.Format("2006-01-02 15:04:05")
|
||||
return strings.Replace(val, " ", "T", -1)
|
||||
}
|
||||
|
||||
return timeVal.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
// 转换成日期格式
|
||||
func ToDateTime(timeVal string) (time.Time, error) {
|
||||
if stringUtil.IsEmpty(timeVal) {
|
||||
return time.Time{}, fmt.Errorf("timeval is empty")
|
||||
}
|
||||
|
||||
return time.ParseInLocation("2006-01-02 15:04:05", timeVal, time.Local)
|
||||
}
|
||||
|
||||
// 以指定时区,转换成日期格式
|
||||
func ToDateTime2(timeVal string, location *time.Location) (time.Time, error) {
|
||||
if stringUtil.IsEmpty(timeVal) {
|
||||
return time.Time{}, fmt.Errorf("timeval is empty")
|
||||
}
|
||||
|
||||
return time.ParseInLocation("2006-01-02 15:04:05", timeVal, location)
|
||||
}
|
||||
|
||||
// 转换成时间格式
|
||||
func ToDate(timeVal string) (time.Time, error) {
|
||||
if stringUtil.IsEmpty(timeVal) {
|
||||
return time.Time{}, fmt.Errorf("timeval is empty")
|
||||
}
|
||||
|
||||
return time.ParseInLocation("2006-01-02", timeVal, time.Local)
|
||||
}
|
||||
|
||||
// 转换成时间格式
|
||||
func ToDate2(timeVal string, location *time.Location) (time.Time, error) {
|
||||
if stringUtil.IsEmpty(timeVal) {
|
||||
return time.Time{}, fmt.Errorf("timeval is empty")
|
||||
}
|
||||
|
||||
return time.ParseInLocation("2006-01-02", timeVal, location)
|
||||
}
|
||||
|
||||
// 转换成yyyyMMddHHmmssms的格式
|
||||
func ToInt64(timeVal time.Time) int64 {
|
||||
year := timeVal.Year()
|
||||
month := int(timeVal.Month())
|
||||
day := timeVal.Day()
|
||||
hour := timeVal.Hour()
|
||||
minute := timeVal.Minute()
|
||||
second := timeVal.Second()
|
||||
|
||||
return int64(int64(year)*1e10) + int64(month*1e8) + int64(day*1e6) + int64(hour*1e4) + int64(minute*1e2) + int64(second)
|
||||
}
|
||||
29
trunk/goutil/timeUtil/format_test.go
Normal file
29
trunk/goutil/timeUtil/format_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package timeUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFormat(t *testing.T) {
|
||||
now := time.Date(2015, 9, 11, 10, 10, 10, 0, time.Local)
|
||||
expectedString := "2015/09/11"
|
||||
result := Format(now, "yyyy/MM/dd")
|
||||
if result != expectedString {
|
||||
t.Errorf("Format Error, expected %s, Got %s", expectedString, result)
|
||||
}
|
||||
|
||||
expectedString = "2015-09-11 %d:%s:%d"
|
||||
minutes := ""
|
||||
if now.Minute() >= 10 {
|
||||
minutes = fmt.Sprintf("%d", now.Minute())
|
||||
} else {
|
||||
minutes = fmt.Sprintf("0%d", now.Minute())
|
||||
}
|
||||
expectedString = fmt.Sprintf(expectedString, now.Hour(), minutes, now.Second())
|
||||
result = Format(now, "yyyy-MM-dd HH:mm:ss")
|
||||
if result != expectedString {
|
||||
t.Errorf("Format Error, expected %s, Got %s", expectedString, result)
|
||||
}
|
||||
}
|
||||
64
trunk/goutil/timeUtil/timeZone.go
Normal file
64
trunk/goutil/timeUtil/timeZone.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package timeUtil
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 把时区转换到UTC时区,但时间值不变(去掉时区的影响)
|
||||
func GetUTCTime(timeVal time.Time) time.Time {
|
||||
_, offset := timeVal.Zone()
|
||||
timeVal = timeVal.Add(time.Duration(offset) * time.Second)
|
||||
timeVal = timeVal.In(time.UTC)
|
||||
|
||||
return timeVal
|
||||
}
|
||||
|
||||
// 把时间转换成本地时区,但时间值不变(去掉时区的影响)
|
||||
func GetLocalTime(timeVal time.Time) time.Time {
|
||||
// 获取本地时区的时间偏移
|
||||
tmpVal := time.Now()
|
||||
_, localOffset := tmpVal.Zone()
|
||||
|
||||
// 获取指定时间值的时区偏移
|
||||
_, timeOffset := timeVal.Zone()
|
||||
timeVal = timeVal.Add(time.Duration(timeOffset-localOffset) * time.Second)
|
||||
timeVal = timeVal.In(time.Local)
|
||||
|
||||
return timeVal
|
||||
}
|
||||
|
||||
// 增加本地时区的值到指定时间上
|
||||
func AddLocalTimeZone(timeVal int64) int64 {
|
||||
// 获取本地时区的时间偏移
|
||||
tmpVal := time.Now()
|
||||
_, localOffset := tmpVal.Zone()
|
||||
|
||||
return timeVal + int64(localOffset)
|
||||
}
|
||||
|
||||
// 增加本地时区的值到指定时间上
|
||||
func AddLocalTimeZone2(timeVal time.Time) time.Time {
|
||||
// 获取本地时区的时间偏移
|
||||
tmpVal := time.Now()
|
||||
_, localOffset := tmpVal.Zone()
|
||||
|
||||
return timeVal.Add(time.Duration(localOffset) * time.Second)
|
||||
}
|
||||
|
||||
// 减去本地时区到指定时间上
|
||||
func SubLocalTimeZone(timeVal int64) int64 {
|
||||
// 获取本地时区的时间偏移
|
||||
tmpVal := time.Now()
|
||||
_, localOffset := tmpVal.Zone()
|
||||
|
||||
return timeVal + -1*int64(localOffset)
|
||||
}
|
||||
|
||||
// 减去本地时区到指定时间上
|
||||
func SubLocalTimeZone2(timeVal time.Time) time.Time {
|
||||
// 获取本地时区的时间偏移
|
||||
tmpVal := time.Now()
|
||||
_, localOffset := tmpVal.Zone()
|
||||
|
||||
return timeVal.Add(-1 * time.Duration(localOffset) * time.Second)
|
||||
}
|
||||
36
trunk/goutil/timeUtil/timeZone_test.go
Normal file
36
trunk/goutil/timeUtil/timeZone_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package timeUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGetTime(t *testing.T) {
|
||||
timeVal := time.Date(2018, 4, 25, 9, 36, 1, 0, time.Local)
|
||||
timeStr1 := ToDateTimeString2(timeVal)
|
||||
|
||||
utcTime := GetUTCTime(timeVal)
|
||||
timeStr2 := ToDateTimeString2(utcTime)
|
||||
|
||||
if timeStr1 != timeStr2 {
|
||||
t.Errorf("获取UTC时间出错,两个时间不对等")
|
||||
}
|
||||
|
||||
utcTime2 := GetUTCTime(utcTime)
|
||||
timeStr3 := ToDateTimeString2(utcTime2)
|
||||
if timeStr1 != timeStr3 {
|
||||
t.Errorf("两次的UTC时间不对等")
|
||||
}
|
||||
|
||||
utcTime4 := GetLocalTime(utcTime)
|
||||
timeStr4 := ToDateTimeString2(utcTime4)
|
||||
if timeStr4 != timeStr1 {
|
||||
t.Errorf("local变更了时间 time1:%v time4:%v", timeStr1, timeStr4)
|
||||
}
|
||||
|
||||
utcTime5 := GetLocalTime(utcTime)
|
||||
timeStr5 := ToDateTimeString2(utcTime5)
|
||||
if timeStr4 != timeStr5 {
|
||||
t.Errorf("两次的local时间不对等")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user