Velocity Reviews > time convert

# time convert

Benoit BESSE
Guest
Posts: n/a

 06-30-2003
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.

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

Egor Bolonev
Guest
Posts: n/a

 07-01-2003
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: http://www.velocityreviews.com/forums/(E-Mail Removed)