github.com/trim21/go-phpserialize@v0.0.22-0.20240301204449-2fca0319b3f0/internal/encoder/time.go (about)

     1  package encoder
     2  
     3  import (
     4  	"time"
     5  	"unsafe"
     6  
     7  	"github.com/trim21/go-phpserialize/internal/runtime"
     8  )
     9  
    10  func compileTime(rt *runtime.Type) (encoder, error) {
    11  	if rt != timeType {
    12  		panic("try to compile type encoder for non-`time.Time` type")
    13  	}
    14  
    15  	return encodeTime, nil
    16  }
    17  
    18  func encodeTime(ctx *Ctx, b []byte, p uintptr) ([]byte, error) {
    19  	t := **(**time.Time)(unsafe.Pointer(&p))
    20  	buf := t.AppendFormat(ctx.smallBuffer[:0], time.RFC3339Nano)
    21  	b = appendBytesAsPhpStringVariable(b, buf)
    22  	ctx.smallBuffer = buf
    23  	return b, nil
    24  }