time

  • Thread starter Gabriel Rossetti
  • Start date
G

Gabriel Rossetti

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
 
M

Mike Driscoll

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
 
R

Richard Brodie

..
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.
 
M

Mike Driscoll

.


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
 
A

Alan Franzoni

Gabriel Rossetti was kind enough to say:
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?

I don't see your IP since I'm reading this through gmane and you appear to
have posted via the python ml, but if your utc+1 means "Central Europe" ,
you're proably missing the daylight saving. 17.00 CEST is 15.00 UTC during
summer, when daylight saving is employed.

--
Alan Franzoni <[email protected]>
-
Remove .xyz from my email in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
 
L

Lawrence D'Oliveiro

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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top