Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting type/size of `time_t` using ctypes

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),
    ]
like image 568
GaretJax Avatar asked Jun 20 '11 22:06

GaretJax


1 Answers

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.

like image 166
Santa Avatar answered Oct 17 '22 20:10

Santa