TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Time in Go

128 pointsby laexover 9 years ago

13 comments

mitchellhover 9 years ago
More languages should copy Go&#x27;s time library.<p>As someone who has written a lot of Go for years, I am always very quick to say that &quot;time&quot; is my favorite Go standard library. I love it.<p>The &quot;time&quot; library is to Go what the &quot;requests&quot; [non-standard] library is to Python, in terms of a great API and simplicity. For some reason in every other language I&#x27;ve used (important, I don&#x27;t know every time library! :P) the time library has always been adequate, but not much else.<p>I think Python&#x27;s &quot;datetime&quot; is a great example of a not great time library. At one point I was writing Python every day for a couple years. During that time, I could never ever remember the API for datetime, despite it being able to do what I needed to do well enough. I always had to open the docs. I think this is an example of an adequate library that just doesn&#x27;t have a great API.<p>Or, take the more generic problem of: I have a time, I have a duration, how do I tell if that duration has passed? Most developers I know (including myself) stumble on this for some period of time (see what I did there?), remembering what do I add to what and is it a greater than or a less than or do I subtract, etc. It is an easy problem, but always seems to require a little bit of thinking.<p>Go&#x27;s &quot;time&quot; library is nothing like this.<p>Go&#x27;s &quot;time&quot; library you will remember the API, it is intuitive. You can even guess it after writing Go for some period of time. It is that easy (ignoring the constants and non-standard printf-like function in it).<p>Go&#x27;s &quot;time&quot; comparison&#x2F;arithmetic is incredible. Comparing times? Determining expiration? You&#x27;ll almost never get this wrong the first time. (But write tests anyways)<p>What I&#x27;m trying to say is: even if you don&#x27;t like Go, take a look at how Go&#x27;s &quot;time&quot; library works. I think it would be valuable for other languages. Other languages can probably even do it better (imagining better type systems around units), but I&#x27;ve never seen an API that feels as right for manipulating time as Go&#x27;s time stdlib.
评论 #10196467 未加载
评论 #10196254 未加载
评论 #10196302 未加载
评论 #10196624 未加载
neoyagamiover 9 years ago
Golang to me has the best time manipulation I ever seen, wannabe change the timezone, easy as eat pie, wanna add seconds? No prob, wanna compare dates? No biggie
评论 #10196228 未加载
评论 #10196237 未加载
paraditeover 9 years ago
So I copied the example from <a href="http:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;time&#x2F;#Parse" rel="nofollow">http:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;time&#x2F;#Parse</a><p>and it did NOT work as expected: <a href="http:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;Xd9oEeSffd" rel="nofollow">http:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;Xd9oEeSffd</a><p>(timezone offset is zero)
评论 #10197667 未加载
评论 #10196406 未加载
polskibusover 9 years ago
Slightly offtopic but I always thought that Mike Bostock&#x27;s bl.ocks.org was created to support D3 visualization sharing. This is the first time I see it used for another language, somewhat contrary to it&#x27;s designed use.
panicover 9 years ago
<p><pre><code> fmt.Printf(&quot;\n... and %v days after that ...\n&quot;, days) t2 := t1.Add(time.Duration(days) * time.Hour * 24) printTime(t2) </code></pre> How is that supposed to work with daylight saving time? Won&#x27;t <i>t2</i> end up one hour off from <i>t1</i> if there is a daylight saving shift?
评论 #10196445 未加载
riobardover 9 years ago
Has anybody ever wondered how Go&#x27;s date formatting would work if the language is not English?
chmikeover 9 years ago
I&#x27;m surprised about the method to define a time format by example. There could be an ambiguity between month and day if the example is badly chosen.<p>I prefer the C way with the struct and format.<p>How do I get the time offset to UTC time at a given date and location ?
评论 #10196286 未加载
评论 #10196425 未加载
JonnieCacheover 9 years ago
My favorite is time.Ticker: It returns a channel of Time objects, dispensed at intervals of your choice. It even auto adjusts the interval based on how long your receiver takes to run.
p1mrxover 9 years ago
It&#x27;s unfortunate that Go&#x27;s time library cannot represent an infinite duration, or timestamps in the infinite past or infinite future. This makes it difficult to represent things like &quot;this cache entry never expires&quot;, without relying on auxiliary data.<p>Also, the minimum&#x2F;maximum timestamp and overflow behavior seem to be poorly-defined.
评论 #10197553 未加载
评论 #10196505 未加载
评论 #10196449 未加载
评论 #10196471 未加载
telover 9 years ago
Time libraries like this are a basket of inscrutable bugs waiting to happen. A simply API deceiving its users into thinking they&#x27;re dealing with a simple subject. Time is anything but.<p>There are a few notions of time, each wildly different from the other, which we as humans transparently conflate but which will throw computers into wild undecipherable loops:<p>* Dates, specifically delimited by days, which are logical items of a calendar, not absolute points in time. They&#x27;re never even <i>points</i>, for that matter, but explicit intervals. * Which calendar? In modern, western times you&#x27;re okay with just one, but historical, international, and especially historical international dates you will fail.<p>* Wall times or local times, e.g. what a human being might think the local time is. This is what we tend to mean when something ought to happen &quot;at 3pm&quot; but you should realize that there&#x27;s no way to treat this uniformly: it holds <i>on a particular day</i>, <i>in a particular location</i>. Even simple ideas like &quot;3pm will occur once a day&quot; may not genuinely be true.<p>* Universal times, of which there are a few variations each dealing with leap seconds differently, UTC, UT0&#x2F;1&#x2F;1R&#x2F;2, TAI (<a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Universal_Time" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Universal_Time</a>)<p>* Intervals of universal time, like &quot;10 seconds&quot; which are the only things which behave sanely from a physical point of view. This is why CPU time or Epoch time is nice. Adding two universal time intervals produces an interval twice as long. Adding a universal time interval to a universal time point produces a new universal time point which, subject to leap seconds, may or may not appear to be the proper number of seconds away. Adding a universal time interval to anything else is nonsense.<p>* Intervals of wall-times like &quot;half a day&quot; which can be added meaningfully to combinations of wall-time and dates in a given calendar, useful for setting up human-interpretable re-occurrences.<p>* Intervals of dates which can be added to dates within a calendar<p>Other caveats apply, mostly having to do with the need to have an accurate geopolitical rundown of time disputes (the &quot;Olson&quot; database is probably sufficient) and a reasonably exact notion of where someone is in space that they are interpreting times (go look up Indiana&#x27;s time zone and then throw away your standard US 4-tz notion).<p>Generally, the idea is that when dealing with humans you want to think of time as being arranged into approximately 24-hour chunks (possibly longer or shorter and then overlapping or with weird gaps which should be smoothed out) assigned to each &quot;day&quot; of some assumed calendar. Then you can, given that person&#x27;s exact point in space, convert points in this notion of time into a universal one using the Olson database. Converting intervals is harder and must be done by converting both ends and then subtracting in universal time, handling leap seconds if you care.<p>The only time library I&#x27;ve ever used in anger which handles all of this is Haskell&#x27;s `time` library.<p><pre><code> https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;time http:&#x2F;&#x2F;two-wrongs.com&#x2F;haskell-time-library-tutorial</code></pre>
NKCSSover 9 years ago
Looking at the docs it looks fine; only thing I can remark is that it&#x27;s too bad that all sorts of constants are in the same namespace; I&#x27;d prefer to have all the formatting formats in a separate namespace for intance to make it more clear.
评论 #10197865 未加载
alblueover 9 years ago
Java is an excellent example of how not to do date&#x2F;time APIs (pre Java 8 anyway). My answer (from years ago):<p><a href="http:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;1969651" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;1969651</a>
MichaelGGover 9 years ago
&gt; time.Duration(mins) * time.Minute<p>Seems a bit clunky. Why not time.FromMinutes(mins) or something to that effect? time.Duration is just &quot;casting&quot; an int64 to a Duration type?
评论 #10199620 未加载