github.com/gotranspile/cxgo@v0.3.7/libs/time.go (about)

     1  package libs
     2  
     3  import (
     4  	"github.com/gotranspile/cxgo/runtime/libc"
     5  	"github.com/gotranspile/cxgo/types"
     6  )
     7  
     8  const (
     9  	timeH = "time.h"
    10  )
    11  
    12  func init() {
    13  	RegisterLibrary(timeH, func(c *Env) *Library {
    14  		gintT := c.Go().Int()
    15  		intT := types.IntT(4)
    16  		longT := types.IntT(8)
    17  		strT := c.C().String()
    18  		timeT := types.NamedTGo("time_t", "libc.Time", types.IntT(4))
    19  		timerT := types.NamedTGo("timer_t", "libc.Timer", types.UintT(4))
    20  		clockT := types.NamedTGo("clock_t", "libc.Clock", types.IntT(4))
    21  		clockIDT := types.NamedTGo("clockid_t", "libc.ClockID", types.UintT(4))
    22  		tvT := types.NamedTGo("timeval", "libc.TimeVal", types.StructT([]*types.Field{
    23  			{Name: types.NewIdentGo("tv_sec", "Sec", timeT)},
    24  			{Name: types.NewIdentGo("tv_usec", "USec", longT)},
    25  		}))
    26  		tsT := types.NamedTGo("timespec", "libc.TimeSpec", types.StructT([]*types.Field{
    27  			{Name: types.NewIdentGo("tv_sec", "Sec", timeT)},
    28  			{Name: types.NewIdentGo("tv_nsec", "NSec", longT)},
    29  		}))
    30  		tmT := types.NamedTGo("tm", "libc.TimeInfo", types.StructT([]*types.Field{
    31  			{Name: types.NewIdentGo("tm_sec", "Sec", intT)},
    32  			{Name: types.NewIdentGo("tm_min", "Min", intT)},
    33  			{Name: types.NewIdentGo("tm_hour", "Hour", intT)},
    34  			{Name: types.NewIdentGo("tm_mday", "Day", intT)},
    35  			{Name: types.NewIdentGo("tm_mon", "Month", intT)},
    36  			{Name: types.NewIdentGo("tm_year", "Year", intT)},
    37  			{Name: types.NewIdentGo("tm_wday", "WeekDay", intT)},
    38  			{Name: types.NewIdentGo("tm_yday", "YearDay", intT)},
    39  			{Name: types.NewIdentGo("tm_isdst", "IsDst", intT)},
    40  			{Name: types.NewIdentGo("tm_zone", "Timezone", c.PtrT(nil))},
    41  			{Name: types.NewIdentGo("tm_gmtoff", "GMTOffs", intT)},
    42  		}))
    43  		return &Library{
    44  			Imports: map[string]string{
    45  				"libc": RuntimeLibc,
    46  			},
    47  			Types: map[string]types.Type{
    48  				"time_t":    timeT,
    49  				"timer_t":   timerT,
    50  				"clock_t":   clockT,
    51  				"clockid_t": clockIDT,
    52  				"tm":        tmT,
    53  				"timeval":   tvT,
    54  				"timespec":  tsT,
    55  			},
    56  			Idents: map[string]*types.Ident{
    57  				"time":            c.NewIdent("time", "libc.GetTime", libc.GetTime, c.FuncTT(timeT, c.PtrT(timeT))),
    58  				"mktime":          c.NewIdent("mktime", "libc.MakeTime", libc.MakeTime, c.FuncTT(timeT, c.PtrT(tmT))),
    59  				"localtime":       c.NewIdent("localtime", "libc.LocalTime", libc.LocalTime, c.FuncTT(c.PtrT(tmT), c.PtrT(timeT))),
    60  				"clock":           c.NewIdent("clock", "libc.ClockTicks", libc.ClockTicks, c.FuncTT(clockT)),
    61  				"clock_getres":    c.NewIdent("clock_getres", "libc.ClockGetRes", libc.ClockGetRes, c.FuncTT(intT, clockT, c.PtrT(tsT))),
    62  				"clock_settime":   c.NewIdent("clock_settime", "libc.ClockSetTime", libc.ClockSetTime, c.FuncTT(intT, clockT, c.PtrT(tsT))),
    63  				"clock_gettime":   c.NewIdent("clock_gettime", "libc.ClockGetTime", libc.ClockGetTime, c.FuncTT(intT, clockT, c.PtrT(tsT))),
    64  				"asctime":         c.NewIdent("asctime", "libc.AscTime", libc.AscTime, c.FuncTT(strT, c.PtrT(tmT))),
    65  				"CLOCK_REALTIME":  c.NewIdent("CLOCK_REALTIME", "libc.CLOCK_REALTIME", libc.CLOCK_REALTIME, gintT),
    66  				"CLOCK_MONOTONIC": c.NewIdent("CLOCK_MONOTONIC", "libc.CLOCK_MONOTONIC", libc.CLOCK_MONOTONIC, gintT),
    67  				"CLOCKS_PER_SEC":  c.NewIdent("CLOCKS_PER_SEC", "libc.CLOCKS_PER_SEC", libc.CLOCKS_PER_SEC, gintT),
    68  			},
    69  			// TODO
    70  			Header: `
    71  #include <` + BuiltinH + `>
    72  #include <` + sysTypesH + `>
    73  
    74  const _cxgo_go_int CLOCK_REALTIME = 0;
    75  const _cxgo_go_int CLOCK_MONOTONIC = 1;
    76  const _cxgo_go_int CLOCKS_PER_SEC = 1000000;
    77  
    78  typedef _cxgo_int32 time_t;
    79  typedef _cxgo_uint32 timer_t;
    80  typedef _cxgo_int32 clock_t;
    81  typedef _cxgo_uint32 clockid_t;
    82  #define suseconds_t _cxgo_int64
    83  
    84  struct tm {
    85      _cxgo_int32    tm_sec;
    86      _cxgo_int32    tm_min;
    87      _cxgo_int32    tm_hour;
    88      _cxgo_int32    tm_mday;
    89      _cxgo_int32    tm_mon;
    90      _cxgo_int32    tm_year;
    91      _cxgo_int32    tm_wday;
    92      _cxgo_int32    tm_yday;
    93      _cxgo_int32    tm_isdst;
    94      void*  tm_zone;
    95      _cxgo_int32    tm_gmtoff;
    96  };
    97  struct timeval {
    98      time_t         tv_sec;
    99      _cxgo_int64    tv_usec;
   100  };
   101  struct timespec {
   102      time_t  tv_sec;
   103      _cxgo_int64    tv_nsec;
   104  };
   105  
   106  char      *asctime(const struct tm *);
   107  char      *asctime_r(const struct tm *, char *);
   108  clock_t    clock(void);
   109  _cxgo_int32        clock_getres(clockid_t, struct timespec *);
   110  _cxgo_int32        clock_gettime(clockid_t, struct timespec *);
   111  _cxgo_int32        clock_settime(clockid_t, const struct timespec *);
   112  char      *ctime(const time_t *);
   113  char      *ctime_r(const time_t *, char *);
   114  double     difftime(time_t, time_t);
   115  struct tm *getdate(const char *);
   116  struct tm *gmtime(const time_t *);
   117  struct tm *gmtime_r(const time_t *, struct tm *);
   118  struct tm *localtime(const time_t *);
   119  struct tm *localtime_r(const time_t *, struct tm *);
   120  time_t     mktime(struct tm *);
   121  int        nanosleep(const struct timespec *, struct timespec *);
   122  size_t     strftime(char *, size_t, const char *, const struct tm *);
   123  char      *strptime(const char *, const char *, struct tm *);
   124  time_t     time(time_t *);
   125  int        timer_create(clockid_t, struct sigevent *, timer_t *);
   126  int        timer_delete(timer_t);
   127  int        timer_gettime(timer_t, struct itimerspec *);
   128  int        timer_getoverrun(timer_t);
   129  int        timer_settime(timer_t, int, const struct itimerspec *, struct itimerspec *);
   130  void       tzset(void);
   131  
   132  
   133  `,
   134  		}
   135  	})
   136  }