Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Milliseconds are not parsed correctly #30

Closed
lsxliron opened this issue Apr 21, 2018 · 0 comments
Closed

Milliseconds are not parsed correctly #30

lsxliron opened this issue Apr 21, 2018 · 0 comments

Comments

@lsxliron
Copy link

When parsing a timestamp with which contains milliseconds, now.Parse will always display them as zeros.

Consider the following program which compares golang time library parsing to now:

package main

import (
  "fmt"
  "time"
  "github.com/jinzhu/now"
)

func main(){
  // Get current location
  loc, err := time.LoadLocation("America/Indianapolis")
  if err!=nil{
    fmt.Println("Could not get location")
    panic(err)
  }

  timeString := "04/20/2017 23:01:54.364"
  layout := "01/02/2006 15:04:05.000"

  // Parse using golang parse function and convert to UTC
  t1, err := time.ParseInLocation(layout, timeString, loc)

  if err!=nil{
    fmt.Printf("Could not parse t1, %s", err)
  }
  t1UTC := t1.UTC().Format("01/02/2006 03:04:05.000-0700")

  // Using Now library

  // Step 1: add layout to now.Timestamps as first argument to ensure that the now
  // library will use that one
  now.TimeFormats = append([]string{layout}, now.TimeFormats...)

  // Step 2: parse using now.Parse
  t2, err := now.Parse(timeString)
  if err!=nil{
    fmt.Printf("Could not parse t2, %s", err)
  }

  // Step 3: conver to UTC
  t2UTC := t2.UTC().Format("01/02/2006 03:04:05.000-0700")

  fmt.Println("Original time (EST):\t", timeString)
  fmt.Println("Parsed time using Golang (UTC):\t", t1UTC)
  fmt.Println("Parsed time using Now (UTC):\t", t2UTC)
}

Output:

Original time (EST):	         04/20/2017 23:01:54.364
Parsed time using Golang (UTC):	 04/21/2017 03:01:54.364+0000
Parsed time using Now (UTC):	 04/21/2017 03:01:54.000+0000

The last line of the output (parsed using now) shows an incorrect timestemp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants