473,787 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using struct timeval in python

Hi,
I am using a library (pcapy) that returns a timeval object T=
(seconds,micros econds) 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 3753
On 9/8/07, sapsi <sa************ @gmail.comwrote :
Hi,
I am using a library (pcapy) that returns a timeval object T=
(seconds,micros econds) 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,micros econds) 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2755
by: Kai Wu | last post by:
#include <string.h> #include <fstream> #include <time.h> typedef unsigned char BYTE; struct Dex { BYTE status; struct timeval timestamp; }; int main(){
2
20405
by: Tim Quon | last post by:
Hi I have a function which needs a struct timeVal as timeout parameter. How can I initialize this struct to a timeout of 3 seconds? Thanks Tim
10
2816
by: Martin Holm Pedersen | last post by:
Hey All.. Im having a bit of a problem with my program that i wrote for linux in c. I use select() to monitor if the user has pressed a key and reads the key with read(). It works fine om my IBM laptop but once i move the program to my dell laptop it seems like it doesn't even recognize the select-function. That is, it doesn't use the timeout assigned at all. I don't get any errors when i compile on either computer. I run debian/testing on...
5
5994
by: John Dumais | last post by:
Hello, I have been trying to figure out how to write an array of doubles (in this specific case) to a binary stream without using a loop. What I have been doing is... foreach(double d in TraceData) { instanceOfBinaryWriter.Write(d); }
8
5919
by: Tony Houghton | last post by:
I'm writing a python program which reads input device events so it needs to know sizeof(struct timeval). By using the struct module I should be able to work out sizeof(long) from python, but I can't think of a way to measure non-fundamental types without including a little bit of C, which I'd rather avoid. How safe would I be assuming that sizeof(struct timeval) == 2 * sizeof(long)
1
5316
by: xoinki | last post by:
hi experts, I need a little help in debugging this code.. would u pleeze kindly help me? here this program sends a datagram every 10 seconds and on reception it cheks whether the source IP is already written in file or not.. if it is not already entered into the file then a file is opened and that IP is entered. the problem is with this part.. if the filewrite() function is tested independently it is working but in this program...
3
3212
by: Sudheer Gupta | last post by:
Hi, I am having trouble using C struct in python. Hope anyone can help me out ... Say, I have my C struct as typedef struct call { struct call *next;
0
3610
by: timekeer9 | last post by:
Hi, I would like to poll stdin for input so I can get user input from the command line without blocking. Looking through the archives, it appears that the best way to do this is using a combination of select and cin.rdbuf()->in_avail(). However, I can't seem to get it to work properly. My select statement seems to always return true, and the cin.rdbuf()->in_avail() always returns false, regardless of if the user has typed or not. Here is...
7
19007
by: PawelCarqowski | last post by:
Hallo group members, Do You know any conversion specification for this. I imagine: struct timeval time; printf("%T\n", time); regards, Pawel
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.