2019-06-23 05:21:30 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
module time
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
2019-06-27 13:02:47 +03:00
|
|
|
struct Time {
|
|
|
|
sec i64
|
|
|
|
nsec i32
|
|
|
|
mono i64
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 13:02:47 +03:00
|
|
|
fn (a Time) + (b Time) Time {
|
|
|
|
sec := a.sec + b.sec
|
|
|
|
nsec := a.nsec + b.nsec
|
|
|
|
return Time{
|
|
|
|
sec: sec + i64(nsec)/i64(1000000000)
|
|
|
|
nsec: nsec % i32(1000000000)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 13:02:47 +03:00
|
|
|
fn (a Time) - (b Time) Time {
|
|
|
|
sec := a.sec - b.sec - i64(1)
|
|
|
|
nsec := a.nsec - b.nsec + i32(1000000000)
|
|
|
|
t := Time{
|
|
|
|
sec: sec + i64(nsec)/i64(1000000000)
|
|
|
|
nsec: nsec % i32(1000000000)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
if a.mono <= i64(0) || b.mono <= i64(0) {
|
|
|
|
return t
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
|
|
|
|
mono := a.mono - b.mono
|
|
|
|
if mono > i64(0) && t.sec >= i64(0) && t.nsec >= i32(0) {
|
|
|
|
return t
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
if mono < i64(0) && t.sec <= i64(0) && t.nsec <= i32(0) {
|
|
|
|
return t
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
if mono == i64(0) {
|
|
|
|
return Time{}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
return Time{
|
|
|
|
sec: mono / i64(1000000000)
|
|
|
|
nsec: i32(mono%i64(1000000000))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 13:02:47 +03:00
|
|
|
fn (t Time) days() f64 {
|
|
|
|
return f64(t.sec)/86400.0
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 13:02:47 +03:00
|
|
|
fn (t Time) hours() f64 {
|
|
|
|
return f64(t.sec)/3600.0
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
|
|
|
|
fn (t Time) minutes() f64 {
|
|
|
|
return f64(t.sec)/60.0
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 13:02:47 +03:00
|
|
|
fn (t Time) seconds() f64 {
|
|
|
|
return f64(t.sec) + f64(t.nsec)/1000000000.0
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 13:02:47 +03:00
|
|
|
fn (t Time) milliseconds() f64 {
|
|
|
|
return 1000.0*f64(t.sec) + f64(t.nsec)/1000000.0
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 13:02:47 +03:00
|
|
|
fn (t Time) microseconds() f64 {
|
|
|
|
return 1000000.0*f64(t.sec) + f64(t.nsec)/1000.0
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 13:02:47 +03:00
|
|
|
fn (t Time) nanoseconds() f64 {
|
|
|
|
return 1000000000.0*f64(t.sec) + f64(t.nsec)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 13:02:47 +03:00
|
|
|
fn (t Time) str() string {
|
|
|
|
if t.sec == i64(0) {
|
|
|
|
if t.nsec == i32(0) {
|
|
|
|
return '0s'
|
|
|
|
}
|
|
|
|
if t.nsec < i32(1000) && t.nsec > i32(-1000) {
|
|
|
|
return '${t.nsec}ns'
|
|
|
|
}
|
|
|
|
if t.nsec < i32(1000000) && t.nsec > i32(-1000000) {
|
|
|
|
return '${f64(t.nsec)/1000.0:.1f}µs'
|
|
|
|
}
|
|
|
|
if t.nsec < i32(1000000000) && t.nsec > i32(-1000000000) {
|
|
|
|
return '${f64(t.nsec)/1000000.0:.1f}ms'
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
if t.sec < i64(60) && t.sec > i64(-60) {
|
|
|
|
return '${f64(t.sec)+f64(t.nsec)/1000000000.0:.1f}s'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
if t.sec < i64(3600) && t.sec > i64(-3600) {
|
|
|
|
return '${f64(t.sec)/60.0:.1f}m'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
if t.sec < i64(86400) && t.sec > i64(-86400) {
|
|
|
|
return '${f64(t.sec)/3600.0:.1f}h'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
if t.sec < i64(864000) && t.sec > i64(-864000) {
|
|
|
|
return '${f64(t.sec)/86400.0:.1f}d'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 13:02:47 +03:00
|
|
|
return '${f64(t.sec)/86400.0:.0f}d'
|
2019-06-23 22:18:24 +03:00
|
|
|
}
|