| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package timex
- import (
- "speechAnalysis/constvar"
- "time"
- )
- func StringToTime(timeStr string) (time.Time, error) {
- return time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
- }
- func StringToTime2(timeStr string) (time.Time, error) {
- return time.ParseInLocation("2006-01-02", timeStr, time.Local)
- }
- func StringToTime3(timeStr string) (time.Time, error) {
- return time.ParseInLocation("2006-01-02 15:04", timeStr, time.Local)
- }
- func StringToClock(timeStr string) (time.Time, error) {
- return time.ParseInLocation("15:04", timeStr, time.Local)
- }
- func TimeToString(time time.Time) string {
- return time.Format("2006-01-02 15:04:05")
- }
- func TimeToString2(time time.Time) string {
- return time.Format("2006-01-02")
- }
- func TimeToString3(time time.Time) string {
- return time.Format("15:04")
- }
- func UnixTimeToString(_t int64) string {
- return time.Unix(_t, 0).In(time.Local).Format("2006-01-02 15:04:05")
- }
- func UnixTimeToDate(_t int64) string {
- return time.Unix(_t, 0).In(time.Local).Format("2006-01-02")
- }
- func DayStartTime(_t int64) int64 {
- l := time.Unix(_t, 0).In(time.Local)
- k := time.Date(l.Year(), l.Month(), l.Day(), 0, 0, 0, 0, time.Local).Unix()
- return k
- }
- func DayStartTimeDateStr(_t int64) string {
- l := time.Unix(_t, 0).In(time.Local)
- k := time.Date(l.Year(), l.Month(), l.Day(), 0, 0, 0, 0, time.Local)
- return TimeToString2(k)
- }
- const timeLayout = "2006-01-02 15:04:05"
- func GetCurrentTime() string {
- return time.Now().Format(timeLayout)
- }
- func NextDateTimestamp(base time.Time, unit constvar.InspectCycleUnit, cycle int) time.Time {
- var t time.Time
- switch unit {
- case constvar.InspectCycleUnitWeek:
- t = base.AddDate(0, 0, cycle*7)
- case constvar.InspectCycleUnitMonth:
- t = base.AddDate(0, cycle, 0)
- case constvar.InspectCycleUnitDay:
- t = base.AddDate(0, 0, cycle)
- }
- return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
- }
- // Cycle2Seconds 周期换算成秒数
- func Cycle2Seconds(unit constvar.InspectCycleUnit, cycle int) int {
- var s int
- switch unit {
- case constvar.InspectCycleUnitWeek:
- s = cycle * 86400 * 7
- case constvar.InspectCycleUnitMonth:
- s = cycle * 86400 * 30
- case constvar.InspectCycleUnitDay:
- s = cycle * 86400
- }
- return s
- }
|