TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Maya – Python Datetimes for Humans

350 点作者 kenneth_reitz超过 8 年前

33 条评论

BoppreH超过 8 年前
Awesome! Timekeeping is hard and I&#x27;m glad we now have one more tool do deal with it.<p>One thing that is bothering me is that when you ask for `maya.when(&#x27;tomorrow&#x27;)`, or give only a date, you get back a timestamp with millisecond precision, representing 00:00 of that day. I understand this simplifies the implementation, but shouldn&#x27;t `tomorrow` be a <i>range</i>, from 00:00 to 23:59?<p>Treating imprecise dates as ranges would allow for stuff like<p><pre><code> # Did this event happen &quot;yesterday&quot; according to the US&#x2F;Eastern time zone? timestamp in maya.when(&#x27;yesterday&#x27;, to_timezone=&#x27;US&#x2F;Eastern&#x27;) # Get me all events that happened last Friday (UTC) [event for event in events if event.time in maya.when(&#x27;2016-12-16&#x27;)] </code></pre> Maybe I&#x27;m being naive, and there&#x27;s a reason why this won&#x27;t work, but this seems the way most humans deal with time.<p>PS: it failed to install on Windows, so I opened an issue at <a href="https:&#x2F;&#x2F;github.com&#x2F;kennethreitz&#x2F;maya&#x2F;issues&#x2F;10" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kennethreitz&#x2F;maya&#x2F;issues&#x2F;10</a>
评论 #13205904 未加载
评论 #13207606 未加载
评论 #13210980 未加载
评论 #13206276 未加载
评论 #13205877 未加载
评论 #13206503 未加载
sametmax超过 8 年前
I wish Kenneth would have contributed to an existed project for once.<p>Arrow and pendulum (my current favorite) have a very decent API. The later one is especially well tested for the numerous corner cases of date handling, which I doubt Kenneth got right on first try.<p>For request, a full rewrite made sense because urllib sucked so much and we had no good alternatives. But for date time, alternative exists and they are good. Do not split the open source effort, join forces!
评论 #13205572 未加载
评论 #13205626 未加载
评论 #13206013 未加载
评论 #13206199 未加载
评论 #13205178 未加载
ianamartin超过 8 年前
I think its fascinating that the community has no consensus about a datetime library.<p>A lot of Python is really solved. We don&#x27;t argue about using requests (a not-coincidental example). If you&#x27;re using Python, and you need to deal with http, you use requests. Everyone knows this.<p>There are basically 3 platforms for web frameworks. Flask, Pyramid, and Django. Maybe we&#x27;re a little more dissolute than C# or Ruby folks, but that&#x27;s pretty impressive considering how much we Python people like to roll our own.<p>The fact that there is real disagreement about this among ourselves about this particular issue says to me that this is more about the difficulty of the problem than it is anything else.
评论 #13206380 未加载
jMyles超过 8 年前
What&#x27;s up buddy?<p>My only real question:<p>&gt; rand_day = maya.when(&#x27;2011-02-07&#x27;, timezone=&#x27;US&#x2F;Eastern&#x27;)<p>This returning an object representing a DateTime on the 6th (in UTC time) strikes me as perhaps &quot;not for humans.&quot;<p>If I just see that line casually, I think I expect to get a Date and for it to be the 7th.<p>It looks like, in order to get this (arguably expected) object, I need to take the resulting MayaDT epoch and run its `datetime` method, passing naive=True?<p>And I also see that _tz can only ever be pytz.timezone(&#x27;UTC&#x27;) - is this the result of some belief that timezones are illusions or something? :-)<p>For a while, I have kinda thought that timezones foment a confused mental model of time and teamwork. I prefer to think in terms of the astronomy - it&#x27;s not actually a <i>different time</i> anywhere else, it&#x27;s just that the sun is at a different position relative to the rest of the earth (and thus, ones faraway teammates and loved ones).<p>Anyway, thanks for yet another set of interesting ideas. Hope you are well.
评论 #13207110 未加载
krautsourced超过 8 年前
I wonder if the naming isn&#x27;t a bit unfortunate, seeing that Maya is one of the major 3d packages out there and googling for Maya and Python will almost always lead there (also, not sure whether Autodesk might object...)
评论 #13205222 未加载
评论 #13205245 未加载
评论 #13206606 未加载
评论 #13206379 未加载
etanol超过 8 年前
Not to undermine its merit, but most of the dirty work and heavy lifting is done by its dependencies:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;kennethreitz&#x2F;maya&#x2F;blob&#x2F;d57a78c6bc6b5295f7d7207be0ee7cf0d62f063f&#x2F;setup.py#L14-L20" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kennethreitz&#x2F;maya&#x2F;blob&#x2F;d57a78c6bc6b5295f7...</a><p>And i18n support in humanize is a bit lacking, as it only translates to French, Korean and Russian. Given that most of the translations needed to render human dates can be found in the CLDR database, maintaining their own looks like a bit of a wasted effort.<p>Reference:<p><a href="http:&#x2F;&#x2F;cldr.unicode.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;cldr.unicode.org&#x2F;</a>
评论 #13205548 未加载
Goopplesoft超过 8 年前
Kenneth Reitz is a testament to how important good interfaces are in the developer community. There are literally 0 interesting things in the code (all dependency driven, <a href="https:&#x2F;&#x2F;git.io&#x2F;v15i3" rel="nofollow">https:&#x2F;&#x2F;git.io&#x2F;v15i3</a>). It does have a nice interface and because of this it will probably become one of the more popular python datetime libs.
评论 #13206010 未加载
bndr超过 8 年前
How does it differ from Arrow which was made several years ago? [1]<p>[1] <a href="http:&#x2F;&#x2F;crsmithdev.com&#x2F;arrow&#x2F;" rel="nofollow">http:&#x2F;&#x2F;crsmithdev.com&#x2F;arrow&#x2F;</a>
评论 #13205188 未加载
评论 #13205046 未加载
评论 #13205204 未加载
Drdrdrq超过 8 年前
I have found that no matter what language&#x2F;platform I use, the one thing that is always supported is UNIX timestamp. That makes date+time operations much easier:<p><pre><code> 1) Whenever dealing with users, use local tz. 2) Always save and manipulate in utc.</code></pre>
评论 #13205561 未加载
评论 #13207033 未加载
评论 #13205615 未加载
dom0超过 8 年前
&gt; Datetimes are a headache to deal with in Python, especially when dealing with timezones, especially when dealing with different machines with different locales.<p>Anything with date&#x2F;time calculations is always a pain, probably doesn&#x27;t really have much to do with the library&#x2F;language itself, but that the abstraction level that&#x27;s used (and typically used in other libraries) means that the complexities of calendar and time systems are sprinkled all over application code.<p>I do have to notice here that always using UTC is <i>not</i> always the right thing to do. For example, evaluating rrules in UTC is rather error-prone (DST).
评论 #13206801 未加载
ak217超过 8 年前
I like Kenneth&#x27;s work, but &quot;I wrote a new datetime library&quot; is a cliche now. We have datetime, dateutil, pytz, babel, arrow, pendulum, delorean, a bunch of lesser known stuff, and now this. I have yet to see the need for anything but the first four.
评论 #13208770 未加载
foxhop超过 8 年前
My first commit to ago.py was &#x27;Fri Jun 29 19:25:55 2012&#x27;.<p><pre><code> &gt;&gt;&gt; import ago &gt;&gt;&gt; import dateutil &gt;&gt;&gt; ago.human(dateutil.parser.parse(&#x27;Fri Jun 29 19:25:55 2012&#x27;)) &#x27;4 years, 172 days ago&#x27; The current implementation is 66 lines of code including docstrings: </code></pre> * <a href="https:&#x2F;&#x2F;bitbucket.org&#x2F;russellballestrini&#x2F;ago&#x2F;src&#x2F;tip&#x2F;ago.py" rel="nofollow">https:&#x2F;&#x2F;bitbucket.org&#x2F;russellballestrini&#x2F;ago&#x2F;src&#x2F;tip&#x2F;ago.py</a><p>* <a href="https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;ago" rel="nofollow">https:&#x2F;&#x2F;pypi.python.org&#x2F;pypi&#x2F;ago</a>
japhyr超过 8 年前
I&#x27;m competent with strptime(), but this looks really nice:<p><pre><code> # Automatically parse datetime strings and generate naive datetimes. &gt;&gt;&gt; scraped = &#x27;2016-12-16 18:23:45.423992+00:00&#x27; &gt;&gt;&gt; maya.parse(scraped).datetime(to_timezone=&#x27;US&#x2F;Eastern&#x27;, naive=True) datetime.datetime(2016, 12, 16, 13, 23, 45, 423992) </code></pre> I&#x27;m happy not to have to write formatting arguments to strptime() anymore. Do the other datetime libraries have similar parsing functions?
评论 #13205954 未加载
charlex815超过 8 年前
I suppose this beats breaking out time delta, but I think it&#x27;ll be hard for me to see an actual use in my projects that I couldn&#x27;t accomplish with maybe just a couple extra lines
评论 #13205047 未加载
meltingwax超过 8 年前
This is designed for programmers of user facing applications. For science and engineering, it does not address the problems, eg lack of leap seconds.
评论 #13205553 未加载
评论 #13205776 未加载
评论 #13205252 未加载
diyseguy超过 8 年前
But does it get DST correct. I haven&#x27;t found any python time libraries that do.
评论 #13206952 未加载
评论 #13207834 未加载
Animats超过 8 年前
Bikeshedding. The Python &quot;datetime&quot; module does almost all of this. (Although I did try to get ISO8660 parsing put in, after finding eight libraries for it, all of which were broken in some way.)
ben_jones超过 8 年前
People are asking why this is needed compared to Arrow and a few other libraries. Personally I was never happy using the python time libraries, it always felt like my use cases were slightly different then the library, and I always found myself getting frustrated over little things here and there. I think there are a LOT of use cases for time in python applications and there is plenty of room for small libraries to satisfy these conditions, versus one monolithic time library that attempts to solve all.
djoser超过 8 年前
I understand the link with the maya believes but for a &quot;digital&quot; feature, this sound a lot like an Autodesk product... That said, nice API!
RubyPinch超过 8 年前
<p><pre><code> &lt;MayaDT epoch=1481850660.9&gt; </code></pre> not the most human readable is it?<p>I guess it would help with like, &quot;X happened before Y&quot; situations, but I don&#x27;t think I&#x27;d trust my eyes for that!<p>I think I would of preferred the &quot;this happened in X month&quot; case instead, I find it easier to trust my eyes for that, instead of trying to look for differing digits!<p>- - -<p>UTC default is a godsend though
评论 #13205118 未加载
partycoder超过 8 年前
[offtopic] I do not like the &quot;for humans&quot; thing. What could it be for instead? for kangaroos or giraffes?
评论 #13205777 未加载
krick超过 8 年前
Uh, Arrow? I&#x27;m not a big fan of it, really, as I&#x27;ve seen it to break silently on some edge cases API is not totally awesome, but this one doesn&#x27;t seem to be any better. Still not sure if I should use this or that or write one of my own.
sebastibe超过 8 年前
Hopefully we will have a &quot;API for Humans&quot; for each of the standard library modules.
niftich超过 8 年前
<i>The link to its github was posted on HN when it was brand new, and I left a comment [6], which I&#x27;m reproducing here -- since most of my points still apply.</i><p>(...) take my very early comments with a grain of salt -- they refer to the progress as of this commit [1].<p>I love Requests -- its API design is fantastic, and manages to distill down most of a complex problem domain to a clean, dare-I-say, elegant API. So I can eagerly anticipate this design applied to datetimes. But the progress being shown so far is definitely not it.<p><i>&gt;&gt;&gt; tomorrow = maya.when(&#x27;tomorrow&#x27;)</i><p><i>&lt;MayaDT epoch=1481919067.23&gt;</i><p>Why is &quot;tomorrow&quot; a precise-to-centisecond, infinitesimally small point on a giant cosmic timeline? I&#x27;m reasonably sure it&#x27;s an abstract concept that describes the calendar day that starts after the current calendar day ends.<p>At least, Pendulum normalizes tomorrow() and its ilk to represent midnight on the given day [2], while Delorean&#x27;s natural language methods [3] like next_day() advance the day while leaving the time-of-day unchanged, but the method name makes this fairly clear.<p>Even Arrow, which is heavily inspired by Moment.js to the point of conflating every single datetime idea into the same class, opts for mutators that are still more clear [4].<p><i>&gt; Timezones fit in here somewhere...</i><p>Yeah, this needs more work.<p>Java 8 &#x2F; Threeten, and its predecessor Joda-Time took the approach of clearly modeling each and every concept that humans actually use to describe time; even if you take issue with their API, the designers have clearly done their homework, and their data model is solid.<p>Formats like TOML wrestled with datetimes and realized [5] that datetimes aren&#x27;t just woefully underspecified in most other specs and APIs, but that they&#x27;re frequently mis-modeled, so they adopted large portions of Threeten&#x27;s data model. Cases like this should merit strong consideration from anyone trying to propose new datetime APIs today.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;kennethreitz&#x2F;maya&#x2F;commit&#x2F;ecd0166ba215c1a5.." rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kennethreitz&#x2F;maya&#x2F;commit&#x2F;ecd0166ba215c1a5...</a>. [2] <a href="https:&#x2F;&#x2F;pendulum.eustace.io&#x2F;docs&#x2F;#instantiation" rel="nofollow">https:&#x2F;&#x2F;pendulum.eustace.io&#x2F;docs&#x2F;#instantiation</a> [3] <a href="http:&#x2F;&#x2F;delorean.readthedocs.io&#x2F;en&#x2F;latest&#x2F;quickstart.html#nat.." rel="nofollow">http:&#x2F;&#x2F;delorean.readthedocs.io&#x2F;en&#x2F;latest&#x2F;quickstart.html#nat...</a>. [4] <a href="http:&#x2F;&#x2F;crsmithdev.com&#x2F;arrow&#x2F;#replace-shift" rel="nofollow">http:&#x2F;&#x2F;crsmithdev.com&#x2F;arrow&#x2F;#replace-shift</a> [5] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12364805" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12364805</a> [6] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13190314#13190657" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13190314#13190657</a>
aivosha超过 8 年前
i dont trust any piece of software that has all these made-up, unrelated, non-reflecting names. I mean come on, arrow, maya ? the main challenge about software engineering is correct naming. If you fail there you pretty much fail in the rest. Same goes for actually builtin name, &quot;datetime&quot;. There is no such thing in real life as datetime. There is date and there is time. They are very separate notions and the root of the problem IMO is in trying to pile them together.
d0m超过 8 年前
Yeah.. datetime is one of the rare part of the Python API that I really hate using. It&#x27;s just badly designed, e.g. simple things are hard and confusing.
smilekzs超过 8 年前
Slightly off topic:<p>&gt; Maya never panics, and always carrys a towel.<p>Nice reference!
jchassoul超过 8 年前
I don&#x27;t understand the author&#x27;s effort on solving this &quot;problem&quot;... I wonder if he considered at some point make a contribution to stable open-source existing libraries like arrow that even claim inspiration on one of his projects for the API. What&#x27;s not for humans on the arrow API? what are the arguments for basically start yet another time library from scratch?, why is making a contribution not an option? probably the author of arrow will love your contributions and will love to hear the arguments on the changes you propose.
评论 #13206160 未加载
评论 #13206173 未加载
batbomb超过 8 年前
Kenneth: It would be really cool if you could put in support for TAI and MJD.
aibottle超过 8 年前
Great job, Mr. Reitz. I think I will use this in every project from now on. Thanks!
Walkman超过 8 年前
This is a joke. The whole &quot;library&quot; is 200 lines, has 9 tests, Kenneth probably wrote it in a couple of hours. It is totally not necessary, because, there are a gazillion datetime libraries and still, this is on the front page?<p>Also:<p><pre><code> &gt;&gt;&gt; dt = maya.now() &gt;&gt;&gt; dt.datetime() datetime.datetime(2016, 12, 18, 19, 24, 50, 212663, tzinfo=&lt;UTC&gt;) &gt;&gt;&gt; dt.datetime(&#x27;Europe&#x2F;Budapest&#x27;) datetime.datetime(2016, 12, 18, 20, 24, 50, 212663, tzinfo=&lt;UTC&gt;) </code></pre> I would not use it...
评论 #13207363 未加载
评论 #13207281 未加载
评论 #13207232 未加载
joaoqalves超过 8 年前
Yet another library to handle dates in Python. Oh, boy... <a href="https:&#x2F;&#x2F;xkcd.com&#x2F;927&#x2F;" rel="nofollow">https:&#x2F;&#x2F;xkcd.com&#x2F;927&#x2F;</a>
fnord123超过 8 年前
Python datetimes used with timezones, even UTC, are ridiculously slow and bloated. It&#x27;s puzzling why this doesn&#x27;t wrap np.datetime64 instead. Or wrap boost.datetime. There are many good options to claw some performance back so it&#x27;s really head-scratch inducing that someone would recognize that stdlib datetime is a dog and then wrap it instead of scrapping it.
评论 #13205390 未加载
评论 #13205597 未加载
评论 #13207854 未加载
评论 #13208295 未加载
评论 #13205480 未加载