编写一个函数来使用 time_t 显示当前日期?
我一直在编写一个时间转换器来获取系统 time_t 并将其转换为人类可读的日期/时间。哦,这是我的第二个 python 脚本。我们将把这个事实放在一边并继续前进。
完整的脚本托管在此处。
编写年份和月份的转换器相当容易,但我在努力让这一天工作时遇到了严重的障碍。正如你所看到的,从 1970 年到今天,我一直在尝试暴力破解。不幸的是,这一天的结果为-105。
有谁知道更好的方法,或者解决我在这里尝试的方法?现在是凌晨 3:30,所以我很可能错过了一些明显的事情。
抱歉,我忘记指出我手动执行此操作是为了学习 python。不幸的是,通过日期函数执行此操作达不到目的。
I've been writing a time converter to take the systems time_t and convert it into human readable date/time. Oh, and this is my second python script ever. We'll leave that fact aside and move on.
The full script is hosted here.
Writing converters for the year and month were fairly easy, but I've hit a serious brick wall trying to get the day working. As you can see, I've been trying to brute-force my way all the way from 1970 to today. Unfortunately, the day comes out as -105.
Does anyone know of a better way to do it, or a way to fix up what I have attempted here? It's currently 3:30 AM, so it's quite possible I'm missing something obvious.
Sorry, I forgot to note that I'm doing this manually in order to learn python. Doing it via date functions defeats the purpose, unfortunately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不使用:
datetime 模块处理所有边缘情况——闰年、闰秒、闰日——以及时区转换(带有可选的第二个参数)
Why not use:
The datetime module handles all the edge cases -- leap years, leap seconds, leap days -- as well as time zone conversion (with optional second argument)
您可以使用
time.strftime
:或使用
datetime.date.strftime
:You could do it either with
time.strftime
:or with
datetime.date.strftime
:(我假设您这样做是为了学习 Python,所以我会指出您代码中的错误)。
不不不不。你不能那样做。有些年份有 31536000 秒,有些年份有 31622400 秒。
您不需要测试真值是否为真。 :-) 做:
相反。
同时更改:
至:
这也将修复你的错误:直到 2009 年之后你才会停止,所以你得到答案 -105,因为一年还剩 105 天。
而且,逐月计算也没有多大意义。年复一年,效果很好。
8 个空格的缩进已经很多了。 4 比较常见。
另外,我会用一种方法计算年份和日期,而不是执行两次。
(I'm assuming you do this to learn Python, so I'll point out the errors in your code).
Nonononono. You can't do that. Some years have 31536000 seconds, others have 31622400 seconds.
You don't need to test if a true value is true. :-) Do:
Instead.
Also change:
To:
That will also fix your error: You don't stop until AFTER 2009, so you get the answer -105, because there is 105 days left of the year.
And also, there's not much point in calculating month by month. Year by year works fine.
And an indent of 8 spaces is a lot. 4 is more common.
Also, I'd calculate year and day of year in one method, instead of doing it twice.
我还将指出您发布的代码中的一些奇怪之处:
1.)为什么
time.time()
会引发 IOError?据我所知,该函数不可能引发错误,它应该始终返回一个值。2.)
Print
应为print
。3.) 即使
time.time
确实引发了IOError
异常,您也会吞下该异常,而您可能不想这样做。下一行需要定义SecondsSinceEpoch,这样只会引发另一个(更令人困惑的)异常。I'll also point out something odd in the code you posted:
1.) Why would
time.time()
raise an IOError? As far as I know it's impossible for that function to raise an error, it should always return a value.2.)
Print
should beprint
.3.) Even if
time.time
did raise anIOError
exception, you are swallowing the exception, which you probably don't want to do. The next line requiresSecondsSinceEpoch
to be defined, so that will just raise another (more confusing) exception.