go - Parsing a time with the format HHMMSS00 -




i'm working data multiple sources , 1 of these sources sage erp system.

i trying reference 2 files in sage in particular, audit date , audit time (audtdate , audttime).

i need parse , store datetime in microsoft sql server database.

currently, trying figure out best way parse this.

an example of data might below:

+----------+----------+ | audtdate | audttime | +----------+----------+ | 20170228 |  5013756 | +----------+----------+ 

audtdate yyyymmdd format , audttime hhmmss00.

so tried below test:

func main() {     value := "20170228 5013756"     layout := "20060102 15040500"      t, _ := time.parse(layout, value)     fmt.println(t) } 

this doesn't work, returns 0001-01-01 00:00:00 +0000 utc when run.

if change time 050137 , layout 150405 works fine:

func main() {     value := "20170228 050137"     layout := "20060102 150405"      t, _ := time.parse(layout, value)     fmt.println(t) } 

one way can think of deal strip milliseconds off end , check length , add 0 beginning if needs one.

this seems pretty ugly solution , involve doing this:

func main() {     date := "20170228"     timestring := "5013756"     value := date + preparetime(timestring)     layout := "20060102150405"      t, _ := time.parse(layout, value)     fmt.println(t) }  func preparetime(time string) string {     if len(time) == 7 {         time = "0" + time     }     return time[:6] } 

is there way without going through above? perhaps natively time package?

assuming you're pulling 2 separate values db, can use fmt.sprintf 0 pad timestring. combining date string, can use following:

value := fmt.sprintf("%s %08s", date, timestring)[:15] 

in code:

func main() {     date := "20170228"     timestring := "5013756"     value := fmt.sprintf("%s %08s", date, timestring)[:15]     layout := "20060102 150405"      t, _ := time.parse(layout, value)     fmt.println(t) } 

results:

2017-02-28 05:01:37 +0000 utc 

this approach useful because correctly pad shorter value of time, e.g. 13756 converted 00013756.

the fmt.sprintf function useful format arguments string using formatting desire specified format string , list of arguments (...interface{}). format string tells function how render arguments.

this format string uses 2 items of note:

  • string verb (%s): format string uses variety of verbs used string substitutions. %s render string or slice. other popular verbs include %d base 10 integer , %f float complete list in docs. %v verb useful can used here render argument's default value.
  • 0 left padding: 0 left pad argument, use 0 followed length number in verb after %. prepended argument maximum number of 0s specified in length number. example, %08s render string 8 prepended zeros. means string "" "00000000" while string "1234567" result in "01234567". if string longer length, nothing prepended.

from documentation:

%s  uninterpreted bytes of string or slice  0   pad leading zeros rather spaces;     numbers, moves padding after sign 

more detailed available in documentation: https://golang.org/pkg/fmt/





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -