1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package timeutil
- import (
- "errors"
- "fmt"
- "strings"
- "time"
- )
- func Parse(timeStrInput string) (res time.Time, err error) {
- if len(timeStrInput) > 0 {
- timeStr := strings.ReplaceAll(timeStrInput, "/", "")
- timeStr = strings.ReplaceAll(timeStr, "-", "")
- if len(timeStr) == 8 {
- res, err = time.Parse("20060102", timeStr)
- } else if len(timeStr) == 11 {
- res, err = time.Parse("20060102 15", timeStr)
- } else if len(timeStr) == 14 {
- res, err = time.Parse("20060102 15:04", timeStr)
- } else if len(timeStr) == 17 {
- res, err = time.Parse("20060102 15:04:05", timeStr)
- } else if len(timeStr) == 21 {
- res, err = time.Parse("20060102 15:04:05.000", timeStr)
- } else {
- err = errors.New("bad value for timeStr")
- }
- if err != nil {
- timeStr = strings.ReplaceAll(timeStrInput, "-", "/")
- if len(timeStr) == 8 {
- res, err = time.Parse("2006/1/2", timeStr)
- } else if len(timeStr) == 9 {
- res, err = time.Parse("2006/01/2", timeStr)
- if err != nil {
- res, err = time.Parse("2006/1/02", timeStr)
- }
- } else if len(timeStr) == 11 {
- res, err = time.Parse("2006/1/2 15", timeStr)
- } else if len(timeStr) == 12 {
- res, err = time.Parse("2006/01/2 15", timeStr)
- if err != nil {
- res, err = time.Parse("2006/1/02 15", timeStr)
- }
- } else if len(timeStr) == 14 {
- res, err = time.Parse("2006/1/2 15:04", timeStr)
- } else if len(timeStr) == 15 {
- res, err = time.Parse("2006/1/02 15:04", timeStr)
- if err != nil {
- res, err = time.Parse("2006/01/2 15:04", timeStr)
- }
- } else if len(timeStr) == 17 {
- res, err = time.Parse("2006/1/2 15:04:05", timeStr)
- } else if len(timeStr) == 18 {
- res, err = time.Parse("2006/1/02 15:04:05", timeStr)
- if err != nil {
- res, err = time.Parse("2006/01/2 15:04:05", timeStr)
- }
- }
- }
- }
- return res, err
- }
- func SecondsToTime(seconds int) string {
- if seconds <= 0 {
- return "00:00:00"
- }
- hours := seconds / 3600
- seconds = seconds % 3600
- minutes := seconds / 60
- seconds = seconds % 60
- return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
- }
|