Adding seconds to a in PostgreSQL

The trick is to create a fixed interval and multiply it with the number of seconds in the column: SELECT start_time, expiration_time_seconds, start_time + expiration_time_seconds * interval ‘1 second’ FROM whatever ORDER BY start_time; start_time | expiration_time_seconds | end_time —————————-|————————-|—————————- 2014-08-05 08:23:32.428452 | 172800 | 2014-08-07 08:23:32.428452 2014-08-10 09:49:51.082456 | 3600 | 2014-08-10 10:49:51.082456 2014-08-13 … Read more

Convert UTC to “local” time in Go

Keep in mind that the playground has the time set to 2009-11-10 23:00:00 +0000 UTC, so it is working. The proper way is to use time.LoadLocation though, here’s an example: var countryTz = map[string]string{ “Hungary”: “Europe/Budapest”, “Egypt”: “Africa/Cairo”, } func timeIn(name string) time.Time { loc, err := time.LoadLocation(countryTz[name]) if err != nil { panic(err) } … Read more

How do you time a function in Go and return its runtime in milliseconds?

Go’s defer makes this trivial. In Go 1.x, define the following functions: func trace(s string) (string, time.Time) { log.Println(“START:”, s) return s, time.Now() } func un(s string, startTime time.Time) { endTime := time.Now() log.Println(” END:”, s, “ElapsedTime in seconds:”, endTime.Sub(startTime)) } After that, you get Squeaky Clean one line elapsed time log messages: func someFunction() … Read more

Groovy time durations

If you just want to find the difference between two times you create yourself (for instance to see how long something takes to execute) you could use: import groovy.time.* def timeStart = new Date() // Some code you want to time def timeStop = new Date() TimeDuration duration = TimeCategory.minus(timeStop, timeStart) println duration If you … Read more

How to get hours difference between two dates

Use time.Parse and time.Since: package main import ( “fmt” “time” ) const ( // See http://golang.org/pkg/time/#Parse timeFormat = “2006-01-02 15:04 MST” ) func main() { v := “2014-05-03 20:57 UTC” then, err := time.Parse(timeFormat, v) if err != nil { fmt.Println(err) return } duration := time.Since(then) fmt.Println(duration.Hours()) }

Nullable time.Time

You can use pq.NullTime, or with Go 1.13, you can now use the standard library’s sql.NullTime type. From lib/pq on github: type NullTime struct { Time time.Time Valid bool // Valid is true if Time is not NULL } // Scan implements the Scanner interface. func (nt *NullTime) Scan(value interface{}) error { nt.Time, nt.Valid = … Read more