Better time.Time

blov · · 692 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello everyone,</p> <p>I have been looking for a better library for date and time values than time.Time. I am storing information in Mongo which has worked great. However, I am running into problems with parsing HTML and input type date&#39;s and vice versa. I could format the date on the html side, but I would rather have a stronger time value that can handle more date formats. Any suggestions would be great. Thanks in advanced.</p> <hr/>**评论:**<br/><br/>icholy: <pre><p>Read this <a href="https://golang.org/pkg/time/#pkg-constants" rel="nofollow">https://golang.org/pkg/time/#pkg-constants</a></p></pre>shovelpost: <pre><blockquote> <p>However, I am running into problems with parsing HTML and input type date&#39;s and vice versa.</p> </blockquote> <p>What exactly are you trying to parse? The <code>time</code> package is actually very flexible and powerful. It just doesn&#39;t parse and format the same way that the other languages do.</p></pre>ptdave: <pre><p>The input dumps out mm/dd/yyyy. This far, I&#39;ve had to modify each back and forth in JavaScript. Just looking to save a step.</p></pre>shovelpost: <pre><p>You can parse that with <code>time.Parse(&#34;01/02/2006&#34;)</code>. I do not understand why you need a 3rd party package for.</p></pre>ptdave: <pre><p>I&#39;m marshalling a struct that contains the time.Time type. I then turn it around after a few checks and update/insert into a mongo database. </p></pre>shovelpost: <pre><blockquote> <p>I&#39;m marshalling a struct that contains the time.Time type. I then turn it around after a few checks and update/insert into a mongo database.</p> </blockquote> <p>That does not answer my question of why you need a 3rd party package for that. But in any case from what I understand using a custom type like <a href="/u/janderssen" rel="nofollow">/u/janderssen</a> suggested fixes your problem.</p> <p>Another solution might be that instead of using <code>time.Time</code> in your struct to use <code>int64</code> and keep a Unix time. You are probably losing the nanoseconds anyways when you convert to/from JSON.</p></pre>ptdave: <pre><p>Looking for third party to avoid reinventing the wheel. But otherwise, yes I would agree with why would I need a third party library.</p></pre>recurrency: <pre><p>what formats are you looking for?</p></pre>ptdave: <pre><p>I&#39;d like it to format to mm/dd/yyyy automatically from Json and back iso for mgo interface</p></pre>metakeule: <pre><p>have a look at <a href="https://github.com/metakeule/fmtdate" rel="nofollow">https://github.com/metakeule/fmtdate</a> </p> <p>its tiny and should do what you want</p></pre>janderssen: <pre><p>In the browser I use Moment, and in go I created a new type based on time.Time as follows (ensuring I use RFC3339)</p> <pre><code>type CustomTime time.Time func (t CustomTime) MarshalJSON() ([]byte, error) { stamp := fmt.Sprintf(&#34;\&#34;%s\&#34;&#34;, time.Time(t).Format(time.RFC3339)) return []byte(stamp), nil } func (t *CustomTime) UnmarshalJSON(data []byte) error { // for legacy, you could probably ignore this. s := strings.Trim(string(data), &#34;\&#34;&#34;) if s == &#34;null&#34; { *t = CustomTime(time.Time{}) return nil } // if we fail to parse the RFC3339, fall back to the Nano version temp, err := time.Parse(time.RFC3339, s) if err != nil { temp, err = time.Parse(time.RFC3339Nano, s) if err != nil { *t = CustomTime(time.Time{}) return nil } } *t = CustomTime(temp) return nil } </code></pre> <p>Not sure if this is the best solution in the world, but works great for me.</p> <p>Cheers</p></pre>ptdave: <pre><p>Thanks. Hate doing repetition unnecessarily. I&#39;ll give this a try</p></pre>ptdave: <pre><p>I&#39;m noticing a lot of people giving me negative post values. I assure you, I wouldn&#39;t be asking if I didn&#39;t need a solution. The raw values out of HTML input are not being parsed by the time.Time type natively when my struct is pulled. </p> <p>So I am not sure why people are being so negative on this.</p></pre>shovelpost: <pre><blockquote> <p>I&#39;m noticing a lot of people giving me negative post values.</p> </blockquote> <p>My best guess is because your post/question lacks details but most importantly you are blaming the <code>time</code> package instead of trying to solve your problem or at least explain it us so we can help.</p> <blockquote> <p>The raw values out of HTML input are not being parsed by the time.Time type natively <strong>when my struct is pulled</strong>.</p> </blockquote> <p>What does that mean? Did you try the code of <a href="https://www.reddit.com/r/golang/comments/6o2ya3/better_timetime/dkelhsj/" rel="nofollow">this</a> comment? Write a simple example on Go playground of what you are trying to do and what doesn&#39;t work so we can talk with some code.</p></pre>ptdave: <pre><p>I was only looking for a package. I didn&#39;t think code was necessary when I&#39;m looking for a few suggestions.</p> <p>I am using him web framework and when I bind the body to a struct, unless I manually preformat it in my angular4 typescript, it will error from the date formatting in HTML. </p></pre>itsmontoya: <pre><p>Why not just work with UNIX timestamps?</p></pre>ptdave: <pre><p>I have data already that I have to keep in a mongo. I could do that, but if I understand correctly, I would have to go through each existing value and rebuild it.</p></pre>nemith: <pre><p>I&#39;ve had great luck with <a href="https://github.com/araddon/dateparse" rel="nofollow">https://github.com/araddon/dateparse</a> to try to parse just about any format.</p></pre>markuspeloquin: <pre><p>I came here hoping to see people hating on the insane format specifiers.</p> <p>I did read through format.go, which is pretty efficient-looking, and am a little curious if pre-constructed formatter objects are any better. Maybe not as much of a benefit as pre-constructing regular expressions.</p> <p><em>Edit</em> the real money is in merging parser patterns so I don&#39;t have to write a parser for each possible number of fractional digits.</p></pre>gott_modus: <pre><blockquote> <p>but I would rather have a stronger time value that can handle more date formats.</p> </blockquote> <p>why not just write in c then, man. </p></pre>ptdave: <pre><p>cause it&#39;s c. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

692 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传