1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package cobol
import (
"errors"
"strconv"
"strings"
"time"
)
// --------------------------------------------------------------------------------
// | Your String Component | Example | Go Reference Component | Layout String Part |
// --------------------------------------------------------------------------------
// | 4-digit year | 2025 | 2006 | 2006 |
// | Separator | / | (literal) | / |
// | 2-digit month | 10 | 01 | 01 |
// | Separator | / | (literal) | / |
// | 2-digit day | 14 | 02 | 02 |
// | Space separator | | (literal) | |
// | 2-digit hour (24h) | 12 | 15 | 15 |
// | Separator | : | (literal) | : |
// | 2-digit minute | 13 | 04 | 04 |
// | Separator | : | (literal) | : |
// | 2-digit second | 12 | 05 | 05 |
// --------------------------------------------------------------------------------
func doTimeString(BUILDTIME string) (*time.Time, error) {
parts := strings.Split(BUILDTIME, " ")
if len(parts) == 1 {
return doTimeSingleString(parts[0])
}
if len(parts) >= 2 {
if len(parts[0]) == 10 && len(parts[1]) == 8 {
// fmt.Println("GOT HERE also", BUILDTIME)
const layout = "2006/01/02 15:04:05"
t, err := time.ParseInLocation(layout, parts[0]+" "+parts[1], time.UTC)
if err == nil {
return &t, nil
}
}
}
// try lots of common stuff
const layout = "2006-01-02 15:04:05 -0500 CDT"
t, err := time.ParseInLocation(layout, parts[0]+" "+parts[1], time.UTC)
if err == nil {
return &t, nil
}
return nil, NewFeature
}
// string is a single string with no spaces
func doTimeSingleString(BUILDTIME string) (*time.Time, error) {
// The input epoch seconds
// epochSeconds := int64(1758646486)
num, err := strconv.Atoi(BUILDTIME)
if err == nil {
epochSeconds := int64(num)
// Convert the epoch seconds to a time.Time object.
// time.Unix() creates the time in the UTC timezone by default.
t := time.Unix(epochSeconds, 0)
return &t, errors.New("treated string as seconds")
}
{
const layout = "2006.01.02"
t, err := time.ParseInLocation(layout, BUILDTIME, time.UTC)
if err == nil {
return &t, nil
}
}
{
const layout = "2006-01-02"
t, err := time.ParseInLocation(layout, BUILDTIME, time.UTC)
if err == nil {
return &t, nil
}
}
const layout = "2006/01/02"
t, err := time.ParseInLocation(layout, BUILDTIME, time.UTC)
if err == nil {
return &t, nil
}
return nil, err
}
|