10 examples of 'golang time parse' in Go

Every line of 'golang time parse' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
18func ParseTime(lit string) (time.Time, error) {
19 if !strings.Contains(lit, "T") {
20 // This is a date.
21 return time.Parse("2006-01-02", lit)
22 }
23 // todo(jsternberg): need to also parse when there is no time offset.
24 return time.Parse(time.RFC3339Nano, lit)
25}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

209func parseTime(date string) time.Time {
210 if date == "now" {
211 return time.Now()
212 }
213 t, err := time.ParseInLocation(medtronic.UserTimeLayout, date, time.Local)
214 if err != nil {
215 cmdError("setclock", "YYYY-MM-DD HH:MM:SS (or \"now\")", err)
216 }
217 return t
218}
167func parse(format string, t time.Time) string {
168 result := ""
169 for _, s := range format {
170 result += recognize(string(s), t)
171 }
172 return result
173}
45func parseTime(date string) time.Time {
46 const layout = "1/2/2006 15:04:05"
47 t, err := time.ParseInLocation(layout, date, time.Local)
48 if err != nil {
49 log.Fatalf("Cannot parse %s: %v", date, err)
50 }
51 return t
52}
432func (dbf *DBF) parseDate(raw []byte) (time.Time, error) {
433 if string(raw) == strings.Repeat(" ", 8) {
434 return time.Time{}, nil
435 }
436 return time.Parse("20060102", string(raw))
437}
215func parseDate(s string) (time.Time, error) {
216 t, err := time.Parse(time.RFC3339, s)
217 if err == nil {
218 return t, nil
219 }
220 t, err = time.Parse("2006-01-02", s)
221 if err == nil {
222 return t, nil
223 }
224 // TODO: more formats?
225 return time.Now(), err
226}
8func main() {
9 timeAsString := "2012-01-22"
10 fmt.Println(time.Parse("2006-01-01_this-does-not-compile", timeAsString))
11}
258func inlineParseModeledTime(format, v string) string {
259 const formatTimeTmpl = `func() *time.Time{
260 v, err := protocol.ParseTime("%s", "%s")
261 if err != nil {
262 panic(err)
263 }
264 return &v
265 }()`
266
267 return fmt.Sprintf(formatTimeTmpl, format, v)
268}
697func parseTime(dateTime string) (time.Time, error) {
698 return time.Parse(bitstampTimeLayout, dateTime)
699}
153func ParseDate(L *lua.LState) int {
154 // Get date string
155 date := L.Get(2)
156
157 // Check valid duration
158 if date.Type() != lua.LTString {
159 L.ArgError(1, "Invalid time date string. Expected string")
160 return 0
161 }
162
163 // Get date layout
164 layout := L.Get(3)
165
166 // Check valid layout
167 if layout.Type() != lua.LTString {
168 L.ArgError(2, "Invalid time date layout. Expected string")
169 return 0
170 }
171
172 // Parse date string
173 s, err := time.Parse(layout.String(), date.String())
174
175 if err != nil {
176 L.RaiseError("Cannot parse date: %v", err)
177 return 0
178 }
179
180 // Push timestamp
181 L.Push(lua.LNumber(s.Unix()))
182
183 return 1
184}

Related snippets