-
Notifications
You must be signed in to change notification settings - Fork 6
/
time.go
35 lines (30 loc) · 862 Bytes
/
time.go
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
package strtime
import (
"time"
"unsafe"
"github.com/pkg/errors"
)
// #include <stdlib.h>
// #include "bsdshim.h"
// extern int bsd_strptime(const char *s, const char *format, struct mytm *tm);
import "C"
func Strptime(value string, layout string) (time.Time, error) {
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
cLayout := C.CString(layout)
defer C.free(unsafe.Pointer(cLayout))
var cTime C.struct_mytm
if r, err := C.bsd_strptime(cValue, cLayout, &cTime); r == 0 || err != nil {
return time.Time{}, errors.Wrapf(err, "could not parse %s as %s", value, layout)
}
return time.Date(
int(cTime.tm_year)+1900,
time.Month(cTime.tm_mon+1),
int(cTime.tm_mday),
int(cTime.tm_hour),
int(cTime.tm_min),
int(cTime.tm_sec),
int(cTime.tm_nsec),
time.FixedZone(C.GoString(cTime.tm_zone), int(cTime.tm_gmtoff)),
), nil
}