time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gabriel Rossetti

    time

    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

    #2
    Re: time

    On Oct 7, 10:05 am, Gabriel Rossetti <gabriel.rosse. ..@arimaz.com>
    wrote:
    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

    Comment

    • Richard Brodie

      #3
      Re: time


      "Gabriel Rossetti" <gabriel.rosset ti@arimaz.comwr ote in message
      news:mailman.21 24.1223392354.3 487.python-list@python.org ...
      ..
      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.


      Comment

      • Mike Driscoll

        #4
        Re: time

        On Oct 7, 11:11 am, "Richard Brodie" <R.Bro...@rl.ac .ukwrote:
        "Gabriel Rossetti" <gabriel.rosse. ..@arimaz.comwr ote in message
        >
        news:mailman.21 24.1223392354.3 487.python-list@python.org ...
        .
        >
        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

        Comment

        • Lawrence D'Oliveiro

          #5
          Re: time

          In message <mailman.2124.1 223392354.3487. python-list@python.org >, Gabriel
          Rossetti wrote:
          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.

          Comment

          Working...