Skip to content

Commit

Permalink
[time/duration] Fixed common durations typo!
Browse files Browse the repository at this point in the history
- Many fixes due to above changes
- Add new method for `Second` as `FromNanoSecond`
- Fixed `ToSecAndNano` method of `NanoSecond` to pass its tests!
  • Loading branch information
OmidHekayati committed Oct 18, 2024
1 parent f94f570 commit edeac20
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 28 deletions.
9 changes: 4 additions & 5 deletions time/duration/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

package duration

// Common durations.
const (
OneNanosecond NanoSecond = 1
OneMicrosecond = 1000 * OneNanosecond // 1e3
OneMillisecond = 1000 * OneMicrosecond // 1e6
OneSecond = 1000 * OneMillisecond // 1e9
// coefficientUnit_1000 is the metric base unit for many unit of measure such as mass, length, ...
coefficientUnit_1000 = 1000
coefficientUnit_100 = 100
coefficientUnit_10 = 10
)
7 changes: 7 additions & 0 deletions time/duration/micro-second.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ package duration
// The representation limits the largest representable duration to approximately 290 earth years.
type MicroSecond int64

// Common durations.
const (
OneMicroSecond MicroSecond = 1

NanoSecondInMicroSecond NanoSecond = coefficientUnit_1000 * OneNanosecond // 1e3
)

func (d *MicroSecond) FromSecAndNano(sec Second, nsec NanoInSecond) {
*d = (MicroSecond(sec) * 1e6) + MicroSecond(nsec/1e3)
}
9 changes: 9 additions & 0 deletions time/duration/milli-second.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ package duration
// The representation limits the largest representable duration to approximately 290 earth years.
type MilliSecond int64

// Common durations.
const (
OneMilliSecond MilliSecond = 1

NanoSecondInMilliSecond NanoSecond = coefficientUnit_1000 * NanoSecondInMicroSecond // 1e6

MicroSecondInMilliSecond MicroSecond = coefficientUnit_1000 * OneMicroSecond // 1e6
)

func (d *MilliSecond) FromSecAndNano(sec Second, nsec NanoInSecond) {
*d = (MilliSecond(sec) * 1e3) + MilliSecond(nsec/1e6)
}
13 changes: 9 additions & 4 deletions time/duration/nano-second.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ package duration
// The representation limits the largest representable duration to approximately 290 earth years.
type NanoSecond int64

// Common durations.
const (
OneNanosecond NanoSecond = 1
)

func (d NanoSecond) ToSecAndNano() (sec Second, nsec NanoInSecond) {
sec = Second(d / OneSecond)
sec.FromNanoSecond(d)
// TODO::: Is it worth to uncomment below logic?
// if sec == 0 {
// nsec = NanoInSecond(d)
// return
// }
var secPass = NanoSecond(sec) * OneSecond
nsec = NanoInSecond(d % secPass)
var secPass = sec.ToNanoSecond()
nsec = NanoInSecond(d - secPass)
return
}

func (d *NanoSecond) FromSecAndNano(sec Second, nsec NanoInSecond) {
*d = (NanoSecond(sec) * OneSecond) + NanoSecond(nsec)
*d = sec.ToNanoSecond() + NanoSecond(nsec)
}
30 changes: 12 additions & 18 deletions time/duration/nano-second_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,38 @@
package duration

import (
"reflect"
"testing"
)

func Test_nsecToSec(t *testing.T) {
type args struct {
d NanoSecond
}
func TestNanoSecond_ToSecAndNano(t *testing.T) {
tests := []struct {
name string
args args
d NanoSecond
wantSec Second
wantNsec NanoInSecond
}{
{
name: "test1",
args: args{
d: 1,
},
name: "test1",
d: 1,
wantSec: 0,
wantNsec: 1,
},
{
name: "test2",
args: args{
d: 1*OneSecond + 1,
},
name: "test2",
d: 1*NanoSecondInSecond + 1,
wantSec: 1,
wantNsec: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotSec, gotNsec := tt.args.d.ToSecAndNano()
if gotSec != tt.wantSec {
t.Errorf("nsecToSec() gotSec = %v, want %v", gotSec, tt.wantSec)
gotSec, gotNsec := tt.d.ToSecAndNano()
if !reflect.DeepEqual(gotSec, tt.wantSec) {
t.Errorf("NanoSecond.ToSecAndNano() gotSec = %v, want %v", gotSec, tt.wantSec)
}
if gotNsec != tt.wantNsec {
t.Errorf("nsecToSec() gotNsec = %v, want %v", gotNsec, tt.wantNsec)
if !reflect.DeepEqual(gotNsec, tt.wantNsec) {
t.Errorf("NanoSecond.ToSecAndNano() gotNsec = %v, want %v", gotNsec, tt.wantNsec)
}
})
}
Expand Down
18 changes: 17 additions & 1 deletion time/duration/second.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@ package duration
// https://en.wikipedia.org/wiki/Second
type Second int64

// Common durations.
const (
OneSecond Second = 1

NanoSecondInSecond NanoSecond = coefficientUnit_1000 * NanoSecondInMilliSecond // 1e9

MicroSecondInSecond MicroSecond = coefficientUnit_1000 * OneMicroSecond

MilliSecondInSecond MilliSecond = coefficientUnit_1000 * OneMilliSecond
)

func (d *Second) ToNanoSecond() (nsec NanoSecond) {
// TODO::: check overflow??
nsec = NanoSecond(*d) * OneSecond
nsec = NanoSecond(*d) * NanoSecondInSecond
return
}

func (d *Second) FromNanoSecond(nsec NanoSecond) {
*d = Second(nsec / NanoSecondInSecond)
// TODO::: return overflow nanosecond??
}

0 comments on commit edeac20

Please sign in to comment.