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,
The bottom line is that
time_tis not defined in the standard. It’s up to the OS and compiler. So your definition oftime_tin your Python script depends on the DLL/so you are interacting with.