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

     1  package libc
     2  
     3  import (
     4  	"time"
     5  	"unsafe"
     6  )
     7  
     8  const CLOCK_REALTIME = 0
     9  const CLOCK_MONOTONIC = 1
    10  
    11  const CLOCKS_PER_SEC = 1000000 // us
    12  
    13  type Time int32
    14  
    15  func (t Time) GoTime() time.Time {
    16  	return time.Unix(int64(t), 0)
    17  }
    18  
    19  type Timer uint32
    20  type Clock int32
    21  type ClockID uint32
    22  
    23  var clockStart = time.Now()
    24  
    25  func ClockTicks() Clock {
    26  	return Clock(time.Since(clockStart).Microseconds())
    27  }
    28  
    29  func GetTime(dst *Time) Time {
    30  	t := Time(time.Now().Unix())
    31  	if dst != nil {
    32  		*dst = t
    33  	}
    34  	return t
    35  }
    36  
    37  func MakeTime(src *TimeInfo) Time {
    38  	if src == nil {
    39  		return Time(time.Now().Unix())
    40  	}
    41  	return Time(time.Date(1900+int(src.Year), time.Month(src.Month), int(src.Day), int(src.Hour), int(src.Min), int(src.Sec), 0, time.Local).Unix())
    42  }
    43  
    44  func LocalTime(src *Time) *TimeInfo {
    45  	t := src.GoTime()
    46  	return &TimeInfo{
    47  		Sec:     int32(t.Second()),
    48  		Min:     int32(t.Minute()),
    49  		Hour:    int32(t.Hour()),
    50  		Day:     int32(t.Day()),
    51  		Month:   int32(t.Month()),
    52  		Year:    int32(t.Year()) - 1900,
    53  		WeekDay: int32(t.Weekday()),
    54  		YearDay: int32(t.YearDay()),
    55  		// TODO
    56  		IsDst:    0,
    57  		Timezone: nil,
    58  		GMTOffs:  0,
    59  	}
    60  }
    61  
    62  func AscTime(tm *TimeInfo) *byte {
    63  	t := tm.GoTime()
    64  	s := t.Format("Mon Jan _2 15:04:05 2006")
    65  	return CString(s)
    66  }
    67  
    68  type TimeVal struct {
    69  	Sec  Time
    70  	USec int64
    71  }
    72  
    73  type TimeSpec struct {
    74  	Sec  Time
    75  	NSec int64
    76  }
    77  
    78  type TimeInfo struct {
    79  	Sec      int32
    80  	Min      int32
    81  	Hour     int32
    82  	Day      int32
    83  	Month    int32
    84  	Year     int32
    85  	WeekDay  int32
    86  	YearDay  int32
    87  	IsDst    int32
    88  	Timezone unsafe.Pointer
    89  	GMTOffs  int32
    90  }
    91  
    92  func (tm *TimeInfo) GoTime() time.Time {
    93  	return MakeTime(tm).GoTime()
    94  }
    95  
    96  func ClockGetRes(c Clock, ts *TimeSpec) int32 {
    97  	panic("TODO")
    98  }
    99  
   100  func ClockSetTime(c Clock, ts *TimeSpec) int32 {
   101  	panic("TODO")
   102  }
   103  
   104  func ClockGetTime(c Clock, ts *TimeSpec) int32 {
   105  	switch c {
   106  	case CLOCK_MONOTONIC:
   107  		d := time.Since(clockStart)
   108  		*ts = TimeSpec{Sec: Time(d / time.Second), NSec: int64(d % time.Second)}
   109  		return 0
   110  	}
   111  	panic("TODO")
   112  }