I'm accessing a C struct which contains some time_t
fields using python ctypes module.
Given its non completely portable nature, I cannot define these fields statically as of c_int
or c_long
type.
How can I define them to make my code portable?
Example C struct definition:
#import <sys/types.h>
#import <time.h>
typedef struct my_struct {
time_t timestap;
uint16_t code;
};
Respective python ctypes structure:
from ctypes import *
c_time = ? # What do I have to put here?
class MyStruct(Structure):
_fields_ = [
('timestamp', c_time),
('code', c_int16),
]
Your best bet is by introspecting the system your script runs on and making a best bet on which integral type to use. Something like,
if sys.platform == 'win32':
time_t = ctypes.c_uint64
# ...
The bottom line is that time_t
is not defined in the standard. It's up to the OS and compiler. So your definition of time_t
in your Python script depends on the DLL/so you are interacting with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With