On Oct 7, 10:05*am, Gabriel Rossetti <gabriel.rosse...@arimaz.com>
wrote:
Quote:
Hello everyone!
>
I trying to work with time and I a bit confused... If I look at my
clock, it's 16:59 (4:59pm), if I type "date" in a terminal, it says the
same thing. I'm wanting to write a simple NTP-type server/client (it's
not NTP at all actually, but does the same thing). The idea is that a
client sends it it's UTC offset and it returns the current time, so the
server side checks the time and adds the UTC offset given by the client.
I'm a UTC/GMT +1, I tried obtaining the UTC time, it says it's 2 hours
earlier than the current time (14:59). I tried various other methods, I
still get the wrong time. Does anyone have an idea with what is wrong?
>
Thanks,
Gabriel
Take a look at the time module. It has the functions you'll need.
Here's a proof of concept:
<code>
import time
def getCurrentTime(offset):
"""
Assumes offset is in hours and must convert
the offset to seconds
"""
offset = offset * 60 * 60
now = time.time() # returns seconds since the epoch
# gmtime creates a time_struct instance to allow formatting
now_struct = time.gmtime(now)
print time.strftime("Current time: %H:%M", now_struct)
now += offset
now_struct = time.gmtime(now)
print time.strftime("Current time: %H:%M", now_struct)
return now
if __name__ == "__main__":
getCurrentTime(-2)
</code>
See the docs for more info:
http://www.python.org/doc/2.5.2/lib/module-time.html
Mike