time convert

Discussion in 'Python' started by Benoit BESSE, Jul 1, 2003.

  1. Benoit BESSE

    Benoit BESSE Guest

    Hi, I try to write a fonction which take a date and time and convert it into
    a NTP time.
    I have to use mktime but I did not work at all.
    Here is my code dans the exécution.
    Please help.

    Thanks
    def ToNTPTime(d,h):
    "Retuen a NTP time"
    year,month,day=d.split("/")
    print "year,month,day=>",year,month,day
    hour,minute=h.split(":")
    print "hour,minute=>",hour,minute

    timeTuple=(int(year),int(month),int(day),int(hour),int(minute),int(0),int(0)
    ,int(0),int(0))
    print "Tuple for mktime=>"timeTuple,type(timeTuple)
    return "%d" % (int(time.mktime((timeTuple))) + 2208988800L)
    >>> d

    '2003/04/25'
    >>> h

    '14:42'
    >>> z=ToNTPTime(d,h)

    year,month,day=> 2003 04 25
    hour,minute=> 14 42
    Tuple for mktime=> (2003, 4, 25, 14, 42, 0, 0, 0, 0) <type 'tuple'>
    Traceback (most recent call last):
    File "<pyshell#42>", line 1, in ?
    z=NetworkTimeProtocolConvert.ToNTPTime(d,h)
    File "C:\Python21\_projet\NetworkTimeProtocolConvert.py", line 38, in
    ToNTPTime
    return "%d" % (int(time.mktime((timeTuple))) + 2208988800L)
    AttributeError: mktime
    >>>


    PS : sorry for my english
    Benoit BESSE, Jul 1, 2003
    #1
    1. Advertising

  2. Benoit BESSE

    Egor Bolonev Guest

    Hello, Benoit!
    You wrote on Tue, 1 Jul 2003 01:08:41 +0200:

    BB> Thanks
    BB> def ToNTPTime(d,h):
    BB> "Retuen a NTP time"
    BB> year,month,day=d.split("/")
    BB> print "year,month,day=>",year,month,day
    BB> hour,minute=h.split(":")
    BB> print "hour,minute=>",hour,minute

    BB> timeTuple=(int(year),int(month),int(day),int(hour),int(minute),int(0),i
    BB> nt(0) ,int(0),int(0))
    BB> print "Tuple for mktime=>"timeTuple,type(timeTuple)
    BB> return "%d" % (int(time.mktime((timeTuple))) + 2208988800L)
    ??>>>> d
    BB> '2003/04/25'
    ??>>>> h
    BB> '14:42'
    ??>>>> z=ToNTPTime(d,h)
    BB> year,month,day=> 2003 04 25
    BB> hour,minute=> 14 42
    BB> Tuple for mktime=> (2003, 4, 25, 14, 42, 0, 0, 0, 0) <type 'tuple'>
    BB> Traceback (most recent call last):
    BB> File "<pyshell#42>", line 1, in ?
    BB> z=NetworkTimeProtocolConvert.ToNTPTime(d,h)
    BB> File "C:\Python21\_projet\NetworkTimeProtocolConvert.py", line 38, in
    BB> ToNTPTime
    BB> return "%d" % (int(time.mktime((timeTuple))) + 2208988800L)
    BB> AttributeError: mktime
    ??>>>>
    BB> PS : sorry for my english

    ========================================
    This one work with no errors
    ========================================
    import time

    def ToNTPTime(d,h):
    "Retuen a NTP time"
    year,month,day=d.split("/")
    print "year,month,day=>",year,month,day
    hour,minute=h.split(":")
    print "hour,minute=>",hour,minute

    timeTuple=(int(year),int(month),int(day),int(hour),int(minute),int(0),int(0)
    ,int(0),int(0))
    print "Tuple for mktime=>",timeTuple,type(timeTuple)
    return "%d" % (int(time.mktime((timeTuple))) + 2208988800L)

    print ToNTPTime('2003/04/25','14:42')
    ========================================

    With best regards, Egor Bolonev. E-mail:
    Egor Bolonev, Jul 1, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Salim Afsar

    convert local time to another country time

    Salim Afsar, Mar 16, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    724
    Juan T. Llibre
    Mar 16, 2006
  2. flamesrock
    Replies:
    8
    Views:
    434
    Hendrik van Rooyen
    Nov 24, 2006
  3. Replies:
    1
    Views:
    773
    Paul Boddie
    Mar 11, 2007
  4. deepak
    Replies:
    1
    Views:
    30,508
    Kevin Spencer
    Feb 6, 2008
  5. Ruwan Budha
    Replies:
    4
    Views:
    1,067
    Kirk Haines
    Mar 9, 2011
Loading...

Share This Page