libfaketime icon indicating copy to clipboard operation
libfaketime copied to clipboard

How to faketime Go Lang time.now() calls

Open sivachaitanya opened this issue 11 months ago • 3 comments

Thanks for an awesome tool, I have followed all the documentation instructions and was able to create /etc/ld.so.preload and /etc/faketimerc files assuming to fake all system wide calls. Inside faketimerc I have text -300d, and i have set the below env variables

export FAKETIME_XRESET=1 export FAKETIME_NO_CACHE=1 export LD_PRELOAD=/usr/local/src/libfaketime/src/libfaketime.so.1

before setting LD_PRELOAD date was current host time and after setting LD_PRELOAD it shows the current date - 300d. My env is currently Kubernetes pod on AKS and i dont have root privileges, I am trying to run a Golang program to see if the date changes are happening with time.now() but still it shows current date, can you let me know how to have golang time.now() take faketimerc date changes ?

golang time.now() code -

package main
 
import (
    "fmt"
    "time"
)
 
func main() {
 
    
    var d  = time.Now()
 
    fmt.Println(d)
 

}

golanf time.now() source - https://github.com/golang/go/blob/master/src/time/time.go

sivachaitanya avatar Dec 04 '24 14:12 sivachaitanya

@sivachaitanya try using this watchmaker

ghost avatar Jan 16 '25 04:01 ghost

@xmapst I had an attempt to use it on arm64 linux but faced a few issues and wasn't able to make it work - https://github.com/xmapst/watchmaker/pull/1

(going to try on amd64 as well)

defanator avatar May 01 '25 12:05 defanator

@xmapst works on amd64 with the same test go code from the above link:

Image

Pasting code here for the reference:

package main

import "fmt"
import "time"

func main() {
    for {
        dt := time.Now()
        fmt.Println("Current date and time is: ", dt.String())
        time.Sleep(1*time.Second)
    }
}

defanator avatar May 01 '25 12:05 defanator