471,328 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,328 software developers and data experts.

Using struct timeval in python

Hi,
I am using a library (pcapy) that returns a timeval object T=
(seconds,microseconds) where microseconds is always < 1e6.
Is there a Python class that can handle timeval structs? Specifically,
I wish to subtract two T (defined above) objects, taking into account
the large values of T[0] and T[1] (esp T[0])?

At one point, I would have to step in, since T[0] rolls back to zero
(after some time).

Any pointers?
Thanks in advance
Saptarshi

Sep 8 '07 #1
2 3424
On 9/8/07, sapsi <sa************@gmail.comwrote:
Hi,
I am using a library (pcapy) that returns a timeval object T=
(seconds,microseconds) where microseconds is always < 1e6.
Is there a Python class that can handle timeval structs? Specifically,
I wish to subtract two T (defined above) objects, taking into account
the large values of T[0] and T[1] (esp T[0])?

At one point, I would have to step in, since T[0] rolls back to zero
(after some time).
The Pythonic way would be to convert directly to a datetime (or
perhaps timedelta) object as soon as you receive your (seconds,
microseconds) tuple. Then you can subtract etc easily and interoperate
nicely with the rest of the standard python library.

But if you want to keep it in tuple format then you will probably need
to roll your own. eg: subclass tuple and override the __sub__ method.
Just make sure you handle negative values correctly (ie, what
(seconds,microseconds) you should end up with when you subtract a
later timval from an earlier timeval).

Alternatively you could convert to and from datetime as you need to
(perhaps subclassing and adding methods for easier conversion), but
that would be more expensive.
Sep 8 '07 #2
The timedelta module did the job just fine.
Thank you
Sep 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Kai Wu | last post: by
2 posts views Thread by Tim Quon | last post: by
10 posts views Thread by Martin Holm Pedersen | last post: by
8 posts views Thread by Tony Houghton | last post: by
3 posts views Thread by Sudheer Gupta | last post: by
reply views Thread by timekeer9 | last post: by
7 posts views Thread by PawelCarqowski | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.