Connecting Tech Pros Worldwide Help | Site Map

time

Gabriel Rossetti
Guest
 
Posts: n/a
#1: Oct 7 '08
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
Mike Driscoll
Guest
 
Posts: n/a
#2: Oct 7 '08

re: time


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
Richard Brodie
Guest
 
Posts: n/a
#3: Oct 7 '08

re: time



"Gabriel Rossetti" <gabriel.rossetti@arimaz.comwrote in message
news:mailman.2124.1223392354.3487.python-list@python.org...
..
Quote:
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?
It would be helpful to specify a named timezone. 2 hours earlier would be
expected for central Europe, it being summer. Sorry if that's obvious.


Mike Driscoll
Guest
 
Posts: n/a
#4: Oct 7 '08

re: time


On Oct 7, 11:11*am, "Richard Brodie" <R.Bro...@rl.ac.ukwrote:
Quote:
"Gabriel Rossetti" <gabriel.rosse...@arimaz.comwrote in message
>
news:mailman.2124.1223392354.3487.python-list@python.org...
.
>
Quote:
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?
>
It would be helpful to specify a named timezone. 2 hours earlier would be
expected for central Europe, it being summer. Sorry if that's obvious.
There are "tzset" and "tzname" methods to the time module. I've heard
conflicting reports about their effectiveness, but never tried them
myself.

Mike
Lawrence D'Oliveiro
Guest
 
Posts: n/a
#5: Oct 8 '08

re: time


In message <mailman.2124.1223392354.3487.python-list@python.org>, Gabriel
Rossetti wrote:
Quote:
If I look at my clock, it's 16:59 (4:59pm), if I type "date" in a
terminal, it says the same thing.
Try this:

date

gives local time, while

TZ=UTC date

gives UTC time.
Quote:
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.
It's probably easiest if the server doesn't have to know or care what
timezone the client is in. This is how NTP works: timezones are considered
to be a machine-internal matter, and all times exchanged by the protocol
are in UTC. So have your protocol always work in UTC.
Closed Thread