github.com/gotranspile/cxgo@v0.3.8-0.20240118201721-29871598a6a2/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  		}
    70  	})
    71  }