func (xpath *XPath) SetDeadline(deadline *time.Time) { if deadline == nil { C.xmlXPathContextSetDeadline(xpath.ContextPtr, C.time_t(0)) } else { t := deadline.Unix() C.xmlXPathContextSetDeadline(xpath.ContextPtr, C.time_t(t)) } }
// Fetch retrieves the values represented by an RRD file. func Fetch(filename string, cf int, startTime int64, endTime int64, step uint64) (dsCount uint64, dsNames []string, data []map[int64]float64, err error) { cfilename := C.CString(filename) defer C.free(unsafe.Pointer(cfilename)) // Newer version requires indices cf_idx := CfToString(cf) if cf_idx == "" { err = errors.New(fmt.Sprintf("Unable to convert cf : %d", cf)) return } ccf := C.CString(cf_idx) var cdsCount C.ulong cstep := C.ulong(step) var cdsNames **C.char var cdata *C.rrd_value_t cst := C.time_t(startTime) cet := C.time_t(endTime) ret := C.rrd_fetch_r(cfilename, ccf, &cst, &cet, &cstep, &cdsCount, &cdsNames, &cdata) if int(ret) != 0 { err = errors.New(getError()) return } // Figure out count dsCount = uint64(cdsCount) // Decode names of data sources dsNames = make([]string, dsCount) data = make([]map[int64]float64, dsCount) nptr := *cdsNames for iter := 0; iter < int(dsCount); iter++ { dsNames[iter] = C.GoString(nptr) data[iter] = make(map[int64]float64) nptr = (*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(cdsNames)) + uintptr(iter))) } vptr := cdata for ti := startTime + int64(step); ti <= endTime; ti += int64(step) { k := int64(ti) for ii := 0; ii < int(dsCount); ii++ { v := float64(*vptr) // Add to appropriate map data[ii][k] = v vptr = (*C.rrd_value_t)(unsafe.Pointer(uintptr(unsafe.Pointer(vptr)) + 1)) } } return }
// Export data from RRD file(s) func (e *Exporter) xport(start, end time.Time, step time.Duration) (XportResult, error) { cStart := C.time_t(start.Unix()) cEnd := C.time_t(end.Unix()) cStep := C.ulong(step.Seconds()) args := e.makeArgs(start, end, step) mutex.Lock() defer mutex.Unlock() var ( ret C.int cXSize C.int cColCnt C.ulong cLegends **C.char cData *C.double ) err := makeError(C.rrdXport( &ret, C.int(len(args)), &args[0], &cXSize, &cStart, &cEnd, &cStep, &cColCnt, &cLegends, &cData, )) if err != nil { return XportResult{start, end, step, nil, 0, nil}, err } start = time.Unix(int64(cStart), 0) end = time.Unix(int64(cEnd), 0) step = time.Duration(cStep) * time.Second colCnt := int(cColCnt) legends := make([]string, colCnt) for i := 0; i < colCnt; i++ { legend := C.arrayGetCString(cLegends, C.int(i)) legends[i] = C.GoString(legend) C.free(unsafe.Pointer(legend)) } C.free(unsafe.Pointer(cLegends)) rowCnt := (int(cEnd)-int(cStart))/int(cStep) + 1 valuesLen := colCnt * rowCnt values := make([]float64, valuesLen) sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&values))) sliceHeader.Cap = valuesLen sliceHeader.Len = valuesLen sliceHeader.Data = uintptr(unsafe.Pointer(cData)) return XportResult{start, end, step, legends, rowCnt, values}, nil }
func (self *memcached) Delete(key string, expiration time.Duration) error { cs_key, key_len := cString(key) defer C.free(unsafe.Pointer(cs_key)) return self.checkError( C.memcached_delete( self.mc, cs_key, key_len, C.time_t(expiration.Seconds()))) }
func strftime(format string, t time.Time) (s string) { if format == "" { return } fmt := C.CString(format) defer C.free(unsafe.Pointer(fmt)) // pass timezone to strftime(3) through TZ environment var. // XXX: this is not threadsafe; someone may set TZ to a different value // between when we get and reset it. we could check that it's unchanged // right before setting it back, but that would leave a race between // testing and setting, and also a worse scenario where another thread sets // TZ to the same value of `zone` as this one (which can't be detected), // only to have us unhelpfully reset it to a now-stale value. // // since a runtime environment where different threads are stomping on TZ // is inherently unsafe, don't waste time trying. zone, _ := t.Zone() oldZone := os.Getenv("TZ") if oldZone != zone { defer os.Setenv("TZ", oldZone) os.Setenv("TZ", zone) } timep := C.time_t(t.Unix()) var tm C.struct_tm C.localtime_r(&timep, &tm) for size := initialBufSize; ; size *= 2 { buf := (*C.char)(C.malloc(C.size_t(size))) // can panic defer C.free(unsafe.Pointer(buf)) n := C.strftime(buf, C.size_t(size), fmt, &tm) if n == 0 { // strftime(3), unhelpfully: "Note that the return value 0 does not // necessarily indicate an error; for example, in many locales %p // yields an empty string." This leaves no definite way to // distinguish between the cases where the value doesn't fit and // where it does because the string is empty. In the latter case, // allocating increasingly larger buffers will never change the // result, so we need some heuristic for bailing out. // // Since a single 2-byte conversion sequence should not produce an // output longer than about 24 bytes, we conservatively allow the // buffer size to grow up to 20 times larger than the format string // before giving up. if size > 20*len(format) { return } } else if int(n) < size { s = C.GoStringN(buf, C.int(n)) return } } return }
// Fetch retrieves data from RRD file. func Fetch(filename, cf string, start, end time.Time, step time.Duration) (FetchResult, error) { var null **C.char fn := C.CString(filename) defer freeCString(fn) cCf := C.CString(cf) defer freeCString(cCf) cStart := C.time_t(start.Unix()) cEnd := C.time_t(end.Unix()) cStep := C.ulong(step.Seconds()) var ( ret C.int cDsCnt C.ulong cDsNames **C.char cData *C.double ) err := makeError(C.rrdFetch(&ret, fn, cCf, &cStart, &cEnd, &cStep, &cDsCnt, &cDsNames, &cData)) if err != nil { return FetchResult{filename, cf, start, end, step, nil, 0, nil}, err } if cDsNames == null { return FetchResult{filename, cf, start, end, step, nil, 0, nil}, Error("malloc error") } start = time.Unix(int64(cStart), 0) end = time.Unix(int64(cEnd), 0) step = time.Duration(cStep) * time.Second dsCnt := int(cDsCnt) dsNames := make([]string, dsCnt) for i := 0; i < dsCnt; i++ { dsName := C.arrayGetCString(cDsNames, C.int(i)) dsNames[i] = C.GoString(dsName) C.free(unsafe.Pointer(dsName)) } C.free(unsafe.Pointer(cDsNames)) rowCnt := (int(cEnd)-int(cStart))/int(cStep) + 1 valuesLen := dsCnt * rowCnt var values []float64 sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&values))) sliceHeader.Cap = valuesLen sliceHeader.Len = valuesLen sliceHeader.Data = uintptr(unsafe.Pointer(cData)) return FetchResult{filename, cf, start, end, step, dsNames, rowCnt, values}, nil }
// createPPDCacheEntry creates an instance of ppdCache with the name field set, // all else empty. The caller must free the name and buffer fields with // ppdCacheEntry.free() func createPPDCacheEntry(name string) (*ppdCacheEntry, error) { pce := &ppdCacheEntry{ printername: C.CString(name), modtime: C.time_t(0), } return pce, nil }
func NewFile(id uint32, parent uint32, storage_id uint32, filename string, size uint64, mtime time.Time, fileType int) *File { f := C.LIBMTP_new_file_t() f.item_id = C.uint32_t(id) f.parent_id = C.uint32_t(parent) f.storage_id = C.uint32_t(storage_id) f.filename = C.CString(filename) f.filesize = C.uint64_t(size) f.modificationdate = C.time_t(mtime.Unix()) f.filetype = C.LIBMTP_filetype_t(fileType) return (*File)(f) }
func NewBitcask(dirpath string, depth, pos int, before int64) (b *Bitcask, err error) { b = new(Bitcask) b.path = dirpath cpath := C.CString(dirpath) defer C.free(unsafe.Pointer(cpath)) b.bc = C.bc_open(cpath, C.int(depth), C.int(pos), C.time_t(before)) if b.bc == nil { return nil, errors.New("open bitcask failed" + dirpath) } return }
func (self *memcached) Set(key string, value interface{}, expiration time.Duration) (err error) { buffer, flag, err := self.encode(value) if err != nil { return } cs_key, key_len := cString(key) defer C.free(unsafe.Pointer(cs_key)) cs_value, value_len := cString(string(buffer)) defer C.free(unsafe.Pointer(cs_value)) return self.checkError( C.memcached_set( self.mc, cs_key, key_len, cs_value, value_len, C.time_t(expiration.Seconds()), C.uint32_t(flag))) }
func (c *Creator) create() error { filename := C.CString(c.filename) defer freeCString(filename) args := makeArgs(c.args) defer freeArgs(args) e := C.rrdCreate( filename, C.ulong(c.step), C.time_t(c.start.Unix()), C.int(len(args)), &args[0], ) return makeError(e) }
// createPPDCacheEntry creates an instance of ppdCache with the name field set, // all else empty. The caller must free the name and buffer fields with // ppdCacheEntry.free() func createPPDCacheEntry(name string) (*ppdCacheEntry, error) { file, err := ioutil.TempFile("", "cups-connector-ppd-") if err != nil { return nil, fmt.Errorf("Failed to create PPD cache entry file: %s", err) } defer file.Close() filename := file.Name() pce := &ppdCacheEntry{ printername: C.CString(name), modtime: C.time_t(0), filename: filename, } return pce, nil }
// The Create function lets you set up new Round Robin Database (RRD) files. // The file is created at its final, full size and filled with *UNKNOWN* data. // // filename:: // The name of the RRD you want to create. RRD files should end with the // extension .rrd. However, it accept any filename. // step:: // Specifies the base interval in seconds with which data will be // fed into the RRD. // start_time:: // Specifies the time in seconds since 1970-01-01 UTC when the first // value should be added to the RRD. It will not accept any data timed // before or at the time specified. // values:: // A list of strings identifying datasources (in format "DS:ds-name:DST:dst arguments") // and round robin archives - RRA (in format "RRA:CF:cf arguments"). // There should be at least one DS and RRA. // // See http://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html for detauls. // func Create(filename string, step, start_time int64, values []string) (err os.Error) { cfilename := C.CString(filename) defer C.free(unsafe.Pointer(cfilename)) cvalues := makeCStringArray(values) defer freeCStringArray(cvalues) clearError() ret := C.rrd_create_r(cfilename, C.ulong(step), C.time_t(start_time), C.int(len(values)), getCStringArrayPointer(cvalues)) if int(ret) != 0 { err = os.NewError(error()) } return }
func (r *cRepository) Update(pkg, destination string, lifetime time.Duration) error { cPkg := C.CString(pkg) defer C.free(unsafe.Pointer(cPkg)) cDst := C.CString(destination) defer C.free(unsafe.Pointer(cDst)) cErr := C.malloc(errSize) defer C.free(cErr) cRet := C.bunsan_pm_repository_update(r.repo, cPkg, cDst, C.time_t(lifetime.Seconds()+0.5 /*round non-negative*/), (*C.char)(cErr), errSize) if cRet != 0 { return &cError{C.GoString((*C.char)(cErr))} } return nil }
func (wo *WriteOperation) Operate(object *Object, modifiedTime *time.Time, flags ...LibradosOperation) error { oid := C.CString(object.name) defer freeString(oid) var mtime C.time_t if modifiedTime != nil { mtime = C.time_t(modifiedTime.Unix()) } var f C.int = 0 for _, flag := range flags { f |= C.int(flag) } ret := C.rados_write_op_operate(wo.opContext, wo.ioContext, oid, &mtime, f) if err := toRadosError(ret); err != nil { err.Message = "Unable to perform write operation." return err } return nil }
func unix2C(sec int64, nsec int64) (C.time_t, C.long) { return C.time_t(sec), C.long(nsec) }
func (f *File) SetMtime(t time.Time) { f.modificationdate = C.time_t(t.Unix()) }
// Use C wrapper function to avoid issues with different typedef names on different systems. func toCTime(o *C.struct_timespec, i time.Time) { C.fill_timespec(o, C.time_t(i.Unix()), C.ulong(i.Nanosecond())) }
// int nurs_timer_add(struct nurs_timer *timer, time_t sc); func nursTimerAdd(timer *Timer, sc uint) error { _, err := C.nurs_timer_add((*C.struct_nurs_timer)(timer), C.time_t(sc)) return err }
// int nurs_itimer_add(struct nurs_timer *timer, time_t ini, time_t per); func nursItimerAdd(timer *Timer, ini, per uint) error { _, err := C.nurs_itimer_add((*C.struct_nurs_timer)(timer), C.time_t(ini), C.time_t(per)) return err }
func (self *memcached) Flush(expiration time.Duration) error { return self.checkError(C.memcached_flush(self.mc, C.time_t(expiration.Seconds()))) }
func (t object_map) Run(w http.ResponseWriter, r *http.Request, fn func()) { var req map[string]*json.RawMessage err := json.NewDecoder(r.Body).Decode(&req) if err != nil { http.Error(w, "Bad request", 400) return } for key, value := range req { dst, ok := t[key] if !ok { http.Error(w, "Unknown key: "+key, 400) return } var err error switch dst.Type { case "*main._Ctype_char": var s string err = json.Unmarshal(*value, &s) tmp := C.CString(s) *(**C.char)(dst.Offset) = tmp defer C.free(unsafe.Pointer(tmp)) case "main._Ctype_uint32_t": var i uint32 err = json.Unmarshal(*value, &i) *(*C.uint32_t)(dst.Offset) = C.uint32_t(i) case "main._Ctype_uint16_t": var i uint16 err = json.Unmarshal(*value, &i) *(*C.uint16_t)(dst.Offset) = C.uint16_t(i) case "main._Ctype_uint8_t": var i uint8 err = json.Unmarshal(*value, &i) *(*C.uint8_t)(dst.Offset) = C.uint8_t(i) case "main._Ctype_int32_t": var i int32 err = json.Unmarshal(*value, &i) *(*C.int32_t)(dst.Offset) = C.int32_t(i) case "main._Ctype_int16_t": var i int16 err = json.Unmarshal(*value, &i) *(*C.int16_t)(dst.Offset) = C.int16_t(i) case "main._Ctype_int8_t": var i int8 err = json.Unmarshal(*value, &i) *(*C.int8_t)(dst.Offset) = C.int8_t(i) case "main._Ctype_time_t": var i uint64 err = json.Unmarshal(*value, &i) *(*C.time_t)(dst.Offset) = C.time_t(i) case "*main._Ctype_uint32_t": var ai []uint32 err = json.Unmarshal(*value, &ai) tmp := C.sluw_alloc_uint32_t(C.int(len(ai))) defer C.free(unsafe.Pointer(tmp)) for i := 0; i < len(ai); i++ { C.sluw_set_uint32_t(tmp, C.uint32_t(ai[i]), C.int(i)) } *(**C.uint32_t)(dst.Offset) = tmp case "*main._Ctype_int32_t": var ai []int32 err = json.Unmarshal(*value, &ai) tmp := C.sluw_alloc_int32_t(C.int(len(ai))) defer C.free(unsafe.Pointer(tmp)) for i := 0; i < len(ai); i++ { C.sluw_set_int32_t(tmp, C.int32_t(ai[i]), C.int(i)) } *(**C.int32_t)(dst.Offset) = tmp case "**main._Ctype_char": var as []string err = json.Unmarshal(*value, &as) tmp := C.sluw_alloc_chars(C.int(len(as))) defer C.free(unsafe.Pointer(tmp)) for i := 0; i < len(as); i++ { tmp2 := C.CString(as[i]) defer C.free(unsafe.Pointer(tmp2)) C.sluw_set_chars(tmp, tmp2, C.int(i)) } *(***C.char)(dst.Offset) = (**C.char)(tmp) default: log.Println(key, reflect.TypeOf(dst), "not supported") } if err != nil { http.Error(w, "Bad value for key: "+key, 400) return } } fn() }
func (t *track) SetTimeAdded(val time.Time) { t.t.time_added = C.time_t(val.Unix()) }