解析带有日期时间的股票代码并删除经过的时间。

huangapple go评论96阅读模式
英文:

Parsing ticker with datetime and dropping time elapsed

问题

我想从一个以 ticker.C 格式的字符串(通过网络)中获取一个日期时间,并将其解析为一个 Time 对象。ticker.C 的格式可能是 2023-01-03 17:24:13.986722973 +0100 CET m=+1.002332450。可能需要去掉 m=+1.002332450 的经过时间,因为我没有找到将其保留在 Time 对象中的方法。

另外,有没有办法从一个 Time 对象中获取一个格式字符串?类似于 mytime.GetFormat()

英文:

I would like to get a datetime from a ticker.C formatted string (over the network) and parse it into a Time object. ticker.C would look like 2023-01-03 17:24:13.986722973 +0100 CET m=+1.002332450. It would probably have to drop the m=+1.002332450 elapsed time as I don't see a way of keeping that in a Time object.

Also, is there a way to get a format string out of a Time object? Something like mytime.GetFormat()

答案1

得分: 1

Time的Stringer格式在这里有文档记录:https://pkg.go.dev/time@go1.19.4#Time.String

String使用格式字符串返回格式化的时间

"2006-01-02 15:04:05.999999999 -0700 MST"

如果时间具有单调时钟读数,则返回的字符串包括最后一个字段“m=±”,其中value是以秒为单位格式化的单调时钟读数的十进制数。

返回的字符串用于调试;对于稳定的序列化表示,请使用t.MarshalText、t.MarshalBinary或t.Format与显式格式字符串。

这表明你不应该尝试使用该值,而是依赖于正确编组(或格式化)的字符串。

没有提到/建议,time.MarshalJSON是一个选项:

MarshalJSON实现了json.Marshaler接口。时间是一个带有RFC 3339格式的带引号的字符串,如果存在,则添加了亚秒精度。

发送方和接收方不需要做任何特殊工作来在JSON中编码和解码time.Time值:

type wireTick struct {
	Tick time.Time `json:"tick"`
}

这是一个使用该结构在网络上编码和解码ticker的小例子,https://go.dev/play/p/Fx73q8-kVFa,它产生如下输出:

Sent JSON-encoded tick on wire:	{"tick":"2009-11-10T23:00:01Z"}
Received tick from wire:		{2009-11-10 23:00:01 +0000 UTC}
Sent JSON-encoded tick on wire:	{"tick":"2009-11-10T23:00:02Z"}
Received tick from wire:		{2009-11-10 23:00:02 +0000 UTC}
...

你能修改发送到网络上的值,或者请别人修改它,使其正确吗?

如果不能,这个应该可以工作:

const stringerLayout = "2006-01-02 15:04:05.999999999 -0700 MST"

timeStr := "2009-11-10 23:00:10 +0000 UTC m=+10.000000001"
tickStr := timeStr[:strings.Index(timeStr, "m=")-1]
tick, _ := time.Parse(stringerLayout, tickStr)

fmt.Printf("Received from wire: \t %q\n", timeStr)
fmt.Printf("Chopped off monotonic: \t %q\n", tickStr)
fmt.Printf("Tick is: \t\t %v\n", tick)
Received from wire: 	 "2009-11-10 23:00:10 +0000 UTC m=+10.000000001"
Chopped off monotonic: 	 "2009-11-10 23:00:10 +0000 UTC"
Tick is: 		         2009-11-10 23:00:10 +0000 UTC
英文:

The Stringer format of Time is documented here, <https://pkg.go.dev/time@go1.19.4#Time.String>:

> String returns the time formatted using the format string
>
> none
&gt; &quot;2006-01-02 15:04:05.999999999 -0700 MST&quot;
&gt;

>
> If the time has a monotonic clock reading, the returned string includes a final field "m=±&lt;value&gt;", where value is the monotonic clock reading formatted as a decimal number of seconds.
>
> The returned string is meant for debugging; for a stable serialized representation, use t.MarshalText, t.MarshalBinary, or t.Format with an explicit format string.

Which suggests you should not try to consume that value and instead depend on a properly marshalled (or formatted) string.

Not mentioned/suggested, time.MarshalJSON is an option:

> MarshalJSON implements the json.Marshaler interface. The time is a quoted string in RFC 3339 format, with sub-second precision added if present.

The sender and receiver don't have to do any special work to encode the time.Time value in JSON and then decode it again:

type wireTick struct {
	Tick time.Time `json:&quot;tick&quot;`
}

Here's a small example of encoding and decoding the ticker on the wire with that struct, <https://go.dev/play/p/Fx73q8-kVFa>, which produces output like:

Sent JSON-encoded tick on wire:	{&quot;tick&quot;:&quot;2009-11-10T23:00:01Z&quot;}
Received tick from wire:		{2009-11-10 23:00:01 +0000 UTC}
Sent JSON-encoded tick on wire:	{&quot;tick&quot;:&quot;2009-11-10T23:00:02Z&quot;}
Received tick from wire:		{2009-11-10 23:00:02 +0000 UTC}
...

Can you modify the value being sent on the wire, or ask someone else to modify it so that it's proper?

If not, this should work:

const stringerLayout = &quot;2006-01-02 15:04:05.999999999 -0700 MST&quot;

timeStr := &quot;2009-11-10 23:00:10 +0000 UTC m=+10.000000001&quot;
tickStr := timeStr[:strings.Index(timeStr, &quot;m=&quot;)-1]
tick, _ := time.Parse(stringerLayout, tickStr)

fmt.Printf(&quot;Received from wire: \t %q\n&quot;, timeStr)
fmt.Printf(&quot;Chopped off monotonic: \t %q\n&quot;, tickStr)
fmt.Printf(&quot;Tick is: \t\t %v\n&quot;, tick)
Received from wire: 	 &quot;2009-11-10 23:00:10 +0000 UTC m=+10.000000001&quot;
Chopped off monotonic: 	 &quot;2009-11-10 23:00:10 +0000 UTC&quot;
Tick is: 		         2009-11-10 23:00:10 +0000 UTC

huangapple
  • 本文由 发表于 2023年1月4日 00:40:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/74996164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定