time convert

B

Benoit BESSE

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)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
 
E

Egor Bolonev

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: (e-mail address removed)
 

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,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top