解析时间为HHMMSS00格式

我正在处理来自多个来源的一些数据,其中之一是Sage ERP系统。</ p >

我正在尝试特别参考Sage中的两个文件,即审核日期和审核时间( AUDTDATE </ code>和 AUDTTIME </ code>)。</ p>

我需要对此进行解析并将其作为DATETIME存储在Microsoft SQL Server数据库中。</ p>

当前,我只是在尝试找出解析的最佳方法 </ p>

下面是数据的示例:</ p>

  + ----------  + ---------- + \ N | 审核日期|  AUDTTIME | 

  • ---------- + ---------- + | 20170228 | 5013756 |
  • ---------- + ---------- +
    </ code> </ pre>

    AUDTDATE </ code >是yyyymmdd格式,而 AUDTTIME </ code>是HHMMSS00。</ p>

    所以我尝试了以下方法作为测试:</ p>

     < 代码> func main(){
    value:=“ 20170228 5013756”
    layout:=“ 20060102 15040500”

t,_:= time.Parse(layout,value)
fmt.Println(t)\ n}
</ code> </ pre>

这不起作用,运行时仅返回 0001-01-01 00:00:00 +0000 UTC </ code> 。</ p>

如果我将时间更改为 050137 </ code>并将布局更改为 150405 </ code>,则此方法可以正常运行:</ p>

  func main(){
值:=“ 20170228 050137”
布局:=“ 20060102 150405”

t,_:= time.Parse(layout,value)
fmt .Println(t)
}
</ code> </ pre>

我想到的一种解决方法是从末尾剥离毫秒数,然后检查长度 并在需要的开头添加零。</ p>

这似乎是一个非常丑陋的解决方案,可能需要这样做 像这样:</ p>

  func main(){
date:=“ 20170228”
timeString:=“ 5013756”
value:= date + prepareTime(timeString)

layout:=“ 20060102150405”

t,_:= time.Parse(布局,值)
fmt.Println(t)
}

func prepareTime(时间字符串)字符串{
如果是len (时间)== 7 {
时间=“ 0” +时间
}
返回时间[:6]
}
</ code> </ pre>

有没有一种方法可以解决上述问题? 也许是本机附带的时间包?</ strong> </ p>
</ div>

展开原文

原文

I'm working with some data from multiple sources and one of these sources is a Sage ERP system.

I am trying to reference two files in Sage in particular, an audit date and audit time (AUDTDATE and AUDTTIME).

I need to parse this and store it as a DATETIME in a Microsoft SQL Server database.

Currently, I am just trying to figure out the best way to parse this.

An example of what the data might look like is below:

+----------+----------+
| AUDTDATE | AUDTTIME |
+----------+----------+
| 20170228 |  5013756 |
+----------+----------+

AUDTDATE is a yyyymmdd format and the AUDTTIME is HHMMSS00.

So I tried the below as a test:

func main() {
    value := "20170228 5013756"
    layout := "20060102 15040500"

    t, _ := time.Parse(layout, value)
    fmt.Println(t)
}

This doesn't work, it just returns 0001-01-01 00:00:00 +0000 UTC when run.

If I change the time to this 050137 and the layout to 150405 then this works fine:

func main() {
    value := "20170228 050137"
    layout := "20060102 150405"

    t, _ := time.Parse(layout, value)
    fmt.Println(t)
}

One way that I can think of to deal with this is to strip the milliseconds off from the end and then check the length and add a zero to the beginning if it needs one.

This seems like a pretty ugly solution and would involve doing something like 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 a way to do this without going through the above? Perhaps natively with the time package?

1个回答

Assuming that you're pulling back 2 separate values from the DB, you can use fmt.Sprintf to 0 pad timeString. Combining it with the date string, you can use the following:

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

In your 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 is useful because it will also correctly pad any shorter value of time, e.g. 13756 will be converted to 00013756.

The fmt.Sprintf function is useful to format arguments into a string using the formatting you desire as specified by a format string and a list of arguments (...interface{}). The format string tells the function how to render the arguments.

This format string uses two items of note:

  • String verb (%s): The format string uses a variety of verbs that are used for string substitutions. %s is specifically to render a string or a slice. Other popular verbs include %d for base 10 integer and %f for float with a complete list in the docs. The %v verb is very useful can also be used here as it will render an argument's default value.
  • 0 left padding: To 0 left pad an argument, use 0 followed by a length number in the verb after the %. This will prepended the argument with a maximum number of 0s specified in the length number. For example, %08s will render a string with up to 8 prepended zeros. This means a string "" will be "00000000" while a string "1234567" will result in "01234567". If the string is longer than the length, nothing will be prepended.

From the documentation:

%s  the uninterpreted bytes of the string or slice

0   pad with leading zeros rather than spaces;
    for numbers, this moves the padding after the sign

More detailed is available in the documentation: https://golang.org/pkg/fmt/

doufu1504
doufu1504 感谢您的详细解释! 很有用。 我刚接触Go,这对您很有帮助。
大约 3 年之前 回复
douou9094747
douou9094747 那是对的。 我已经更新了答案,以包含更多解释。 如前所述,这在这里很有用,因为即使需要多个0,它也会导致正确的时间长度。 %s和%08s都指示值是字符串,%08s指示在字符串前添加最多8个零。 在此特定实例中,也可以使用“%v%08v”,因为%v表示使用参数的默认值。 %v可以用于任何结构,但是可以在这里尽可能多地指定%s,这很高兴。
大约 3 年之前 回复
dongzhaoshi8497
dongzhaoshi8497 这看起来是一个更为优雅的解决方案。 fmt.Sprintf(“%s%08s”,date,timeString)您可以介意多解释一下吗-我只是想多一点地理解“%s%08s”。 我假设%s输出的是传递的日期,而%08s输出的是传递的时间,其前导零最多为8个字符。 那是对的吗?
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问