How to benchmark programs in Rust?

It might be worth noting two years later (to help any future Rust programmers who stumble on this page) that there are now tools to benchmark Rust code as a part of one’s test suite. (From the guide link below) Using the #[bench] attribute, one can use the standard Rust tooling to benchmark methods in … 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

extract week number from date postgres

To get the year and the week in a single character value, use to_char() select to_char(current_date, ‘IYYY-IW’); IW returns the year and the week number as defined in the ISO standard and IYYY returns the corresponding year (which might be the previous year). If you need the year and the week number as numbers, use … Read more

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

How do I get the current time as a TimeStamp in Kotlin?

Kotlin doesn’t have any time handling classes of its own, so you just use Java’s java.time. For an ISO-8601 timestamp (which is the preferred format): DateTimeFormatter.ISO_INSTANT.format(Instant.now()) That will return 2018-04-16T17:00:08.746Z. For your format, or if you need a different timezone, you can specify those: DateTimeFormatter .ofPattern(“yyyy-MM-dd HH:mm:ss.SSSSSS”) .withZone(ZoneOffset.UTC) .format(Instant.now()) See the java.time.format.DateTimeFormatter JavaDoc for details … Read more

tech