Commit 5d0856f6f343bc58afc148d86672a64e0f871690

Authored by Marko Tikvić
1 parent 07daaf4e56
Exists in master

when parsing time make sure to include the date as well... because reasons

Showing 1 changed file with 56 additions and 57 deletions   Show diff stats
... ... @@ -335,6 +335,30 @@ func (nt *NullDateTime) CastToSQL() NullDateTime {
335 335 return *nt
336 336 }
337 337  
  338 +func parseSQLDateTime(str string, loc *time.Location) (t time.Time, err error) {
  339 + base := "0000-00-00 00:00:00.0000000"
  340 + timeFormat := "2006-01-02 15:04:05.999999"
  341 + switch len(str) {
  342 + case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
  343 + if str == base[:len(str)] {
  344 + return
  345 + }
  346 + t, err = time.Parse(timeFormat[:len(str)], str)
  347 + default:
  348 + err = fmt.Errorf("invalid time string: %s", str)
  349 + return
  350 + }
  351 +
  352 + // Adjust location
  353 + if err == nil && loc != time.UTC {
  354 + y, mo, d := t.Date()
  355 + h, mi, s := t.Clock()
  356 + t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
  357 + }
  358 +
  359 + return
  360 +}
  361 +
338 362 // NullDate ...
339 363 type NullDate struct {
340 364 Time time.Time
... ... @@ -402,8 +426,7 @@ func (nt *NullDate) UnmarshalJSON(b []byte) error {
402 426 }
403 427  
404 428 if temp != nil {
405   - nt.Valid = true
406   - nt.Time = *temp
  429 + nt.Scan(t1)
407 430 } else {
408 431 nt.Valid = false
409 432 }
... ... @@ -414,6 +437,30 @@ func (nt *NullDate) CastToSQL() NullDate {
414 437 return *nt
415 438 }
416 439  
  440 +func parseSQLDate(str string, loc *time.Location) (t time.Time, err error) {
  441 + base := "0000-00-00"
  442 + timeFormat := "2006-01-02"
  443 + switch len(str) {
  444 + case 10:
  445 + if str == base[:len(str)] {
  446 + return
  447 + }
  448 + t, err = time.Parse(timeFormat[:len(str)], str)
  449 + default:
  450 + err = fmt.Errorf("invalid time string: %s", str)
  451 + return
  452 + }
  453 +
  454 + // Adjust location
  455 + if err == nil && loc != time.UTC {
  456 + y, mo, d := t.Date()
  457 + h, mi, s := t.Clock()
  458 + t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
  459 + }
  460 +
  461 + return
  462 +}
  463 +
417 464 // NullTime ...
418 465 type NullTime struct {
419 466 Time time.Time
... ... @@ -473,7 +520,7 @@ func (nt *NullTime) UnmarshalJSON(b []byte) error {
473 520 if s1 == "null" {
474 521 temp = nil
475 522 } else {
476   - t1, err = time.Parse("15:04:05", s2)
  523 + t1, err = time.Parse("2006-05-04 15:04:05", "1970-01-01 "+s2)
477 524 if err != nil {
478 525 return err
479 526 }
... ... @@ -481,8 +528,7 @@ func (nt *NullTime) UnmarshalJSON(b []byte) error {
481 528 }
482 529  
483 530 if temp != nil {
484   - nt.Valid = true
485   - nt.Time = *temp
  531 + nt.Scan(t1)
486 532 } else {
487 533 nt.Valid = false
488 534 }
... ... @@ -493,63 +539,16 @@ func (nt *NullTime) CastToSQL() NullTime {
493 539 return *nt
494 540 }
495 541  
496   -func parseSQLDateTime(str string, loc *time.Location) (t time.Time, err error) {
497   - base := "0000-00-00 00:00:00.0000000"
498   - timeFormat := "2006-01-02 15:04:05.999999"
499   - switch len(str) {
500   - case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
501   - if str == base[:len(str)] {
502   - return
503   - }
504   - t, err = time.Parse(timeFormat[:len(str)], str)
505   - default:
506   - err = fmt.Errorf("invalid time string: %s", str)
507   - return
508   - }
509   -
510   - // Adjust location
511   - if err == nil && loc != time.UTC {
512   - y, mo, d := t.Date()
513   - h, mi, s := t.Clock()
514   - t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
515   - }
516   -
517   - return
518   -}
519   -
520   -func parseSQLDate(str string, loc *time.Location) (t time.Time, err error) {
521   - base := "0000-00-00"
522   - timeFormat := "2006-01-02"
523   - switch len(str) {
524   - case 10: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
525   - if str == base[:len(str)] {
526   - return
527   - }
528   - t, err = time.Parse(timeFormat[:len(str)], str)
529   - default:
530   - err = fmt.Errorf("invalid time string: %s", str)
531   - return
532   - }
533   -
534   - // Adjust location
535   - if err == nil && loc != time.UTC {
536   - y, mo, d := t.Date()
537   - h, mi, s := t.Clock()
538   - t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
539   - }
540   -
541   - return
542   -}
543   -
  542 +// NOTE(marko): Date must be included because database can't convert it to TIME otherwise.
544 543 func parseSQLTime(str string, loc *time.Location) (t time.Time, err error) {
545   - base := "00:00:00.0000000"
546   - timeFormat := "15:04:05.999999"
  544 + base := "00:00:00"
  545 + timeFormat := "15:04:05"
547 546 switch len(str) {
548   - case 12, 15: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
  547 + case 8:
549 548 if str == base[:len(str)] {
550 549 return
551 550 }
552   - t, err = time.Parse(timeFormat[:len(str)], str)
  551 + t, err = time.Parse("2006-05-04 "+timeFormat[:len(str)], "1970-01-01 "+str)
553 552 default:
554 553 err = fmt.Errorf("invalid time string: %s", str)
555 554 return
... ...