tags:

views:

415

answers:

6

How can I get the current date, month & year online using Python? Thanks!

EDIT-By this I mean, rather than getting it from the computer's date-visit a website & get it, so it doesn't rely on the computer.

A: 

A quick search on Google for "Python" and "NTP" should find you what you seek.

patjak
A: 

here is a python module for hitting NIST online http://freshmeat.net/projects/mxdatetime.

Zaffiro
A: 

Perhaps you mean the NTP protocol? This project may help: http://pypi.python.org/pypi/ntplib/0.1.3

Jarret Hardie
A: 

If you can't use NTP, but rather want to stick with HTTP, you could urllib.urlget("http://developer.yahooapis.com/TimeService/V1/getTime") and parse the results:

<?xml version="1.0" encoding="UTF-8"?>
<Error xmlns="urn:yahoo:api">
     The following errors were detected:
     <Message>Appid missing or other error </Message>
</Error>
<!-- p6.ydn.sp1.yahoo.com uncompressed/chunked Mon May 25 18:42:11 PDT 2009 -->

Note that the datetime (in PDT) is in the final comment (the error message is due to lack of APP ID). There probably are more suitable web services to get the current date and time in HTTP (without requiring registration &c), since e.g. making such a service freely available on Google App Engine would be so trivial, but I don't know of one offhand.

Alex Martelli
This is more what I'm leaning towards, thanks!
So rather than abusing Yahoo's courtesy please try my brand-new GAE app at http://just-the-time.appspot.com/ and let me know what you think - feature requests accepted (took me 10 minutes to code, deploy and test, so a few more minutes won't hurt;-). "Will work for SO upvotes"...?-)
Alex Martelli
+11  A: 

So thinking about the "would be so trivial" part I went ahead and just made a google app engine web app -- when you visit it, it returns a simple response claiming to be HTML but actually just a string such as 2009-05-26 02:01:12 UTC\n. Any feature requests?-)

Alex Martelli
Taking your request at face value...how about seconds since the epoch? :)
Nikhil Chelliah
OK, second version deployed: now takes an optional ?f=... query parameter that lets you specify the strftime format, with an extension that %t expands to seconds-since-the-epoch. So for example, http://just-the-time.appspot.com/?f=%Y%m%d,%20seconds%20since%20the%20epoch:%20%t now returns as the response's body "20090526, seconds since the epoch: 1243305356.12".Other feature requests...?
Alex Martelli
Is there any chance you can let me have the code for that-ideally I'm looking for it in the form May 26, 2009 but I'm sure I can have a play around! Very clever though!
sure, I put all the code up (there ain't much to it!-) a few minutes ago at http://code.google.com/p/just-the-time/ .
Alex Martelli
...so what about accepting this answer, hm...?-)
Alex Martelli
btw, for 'May 26, 2009' just visit http://just-the-time.appspot.com/?f=%b%20%d,%20%Y ...
Alex Martelli
A: 

I would accept the top one but I'm not registered & it's now forgotten me!