Skip to content
This repository has been archived by the owner on Jan 8, 2019. It is now read-only.

Commit

Permalink
time2:make Ticker to be simpler TimeTicker
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuah committed Oct 3, 2015
1 parent 660c91e commit 5606d40
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 151 deletions.
2 changes: 0 additions & 2 deletions regexp/regexp_test.go
Expand Up @@ -26,8 +26,6 @@ func Parse2() {
`

//TODO: remove t.Log

func TestMatch(t *testing.T) {
tt := testing2.Wrap(t)

Expand Down
59 changes: 21 additions & 38 deletions time2/ticker.go
Expand Up @@ -2,58 +2,41 @@ package time2

import "time"

type Ticker struct {
C <-chan time.Time
type TimeTicker struct {
timer *time.Timer
ticker *time.Ticker
tick time.Duration

tm time.Time
tk time.Duration
}

func NewTicker(first time.Time, tick time.Duration) *Ticker {
now := time.Now()
sub := now.Sub(first)
sub2 := sub / tick * tick
if sub2 < sub {
sub2 += tick
func NewTimeTicker(first time.Time, tick time.Duration) *TimeTicker {
now := Now()
sub := first.Sub(now)
for sub < 0 {
sub += tick
}

first = first.Add(sub2)
t := &Ticker{
tick: tick,
timer: time.NewTimer(sub2 - sub),
t := &TimeTicker{
timer: time.NewTimer(sub),

tm: now.Add(sub),
tk: tick,
}
t.C = t.timer.C
return t
}

func (t *Ticker) Wait() (time.Time, bool) {
var now time.Time
var ok bool

if t.ticker == nil {
now, ok = <-t.timer.C
} else {
now, ok = <-t.ticker.C
}
if ok {
t.Switch()
func (t *TimeTicker) C() <-chan time.Time {
if Now().Before(t.tm) {
return t.timer.C
}

return now, ok
}

func (t *Ticker) Switch() {
if t.ticker != nil {
return
if t.ticker == nil {
t.ticker = time.NewTicker(t.tk)
}

t.timer.Stop()
t.timer = nil
t.ticker = time.NewTicker(t.tick)
t.C = t.ticker.C
return t.ticker.C
}

func (t *Ticker) Stop() {
func (t *TimeTicker) Stop() {
if t.ticker == nil {
t.timer.Stop()
} else {
Expand Down
25 changes: 10 additions & 15 deletions time2/ticker_test.go
Expand Up @@ -3,25 +3,20 @@ package time2
import (
"testing"
"time"
"fmt"
"sync"
)

func TestTimer(t *testing.T) {
now := time.Now()
ticker := NewTicker(now.Add(time.Millisecond), time.Millisecond)
ticker := NewTimeTicker(DateDefNow(-1, -1, -1, 15, 41, 0, 0), time.Second)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
// for {
// select {
// case tnow, ok := <-ticker.C:
// if !ok {
// return
// }
// ticker.Switch()
// tt.Eq(time.Millisecond, tnow.Sub(now))
// }
// }
for {
ticker.Wait()
for i := 0; i < 5; i++ {
tm := <-ticker.C()
fmt.Println(DateTime(tm))
}
wg.Done()
}()
time.Sleep(time.Millisecond * 100)
wg.Wait()
}
70 changes: 23 additions & 47 deletions time2/time.go
Expand Up @@ -2,7 +2,6 @@ package time2

import (
"fmt"
"strconv"
"time"

"github.com/cosiner/gohper/errors"
Expand All @@ -24,21 +23,33 @@ func Now() time.Time {
return now.In(Location)
}

func DateString() string {
return Now().Format(DATE_FMT)
func CurrDate() string {
return Date(Now())
}

func TimeString() string {
return Now().Format(TIME_FMT)
func Date(t time.Time) string {
return t.Format(DATE_FMT)
}

func DateTimeString() string {
return Now().Format(DATETIME_FMT)
func CurrTime() string {
return Time(Now())
}

func DateAndTimeString() (string, string) {
func Time(t time.Time) string {
return t.Format(TIME_FMT)
}

func CurrDateTime() string {
return DateTime(Now())
}

func DateTime(t time.Time) string {
return t.Format(DATETIME_FMT)
}

func CurrDateAndTime() (string, string) {
now := Now()
return now.Format(DATE_FMT), now.Format(TIME_FMT)
return Date(now), Time(now)
}

func Parse(layout, value string) (time.Time, error) {
Expand Down Expand Up @@ -70,43 +81,6 @@ func NowTimeUnixNano() uint64 {
return uint64(Now().UnixNano())
}

// ToHuman convert nano to human time size, insufficient portion will be discarded
// performs rounding.
//
// support 0-999ns, 0-999us, 0-999ms, 0-Maxs,
func ToHuman(nano int64) string {
var base int64 = 1
if nano < 1000*base {
return strconv.Itoa(int(nano/base)) + "ns"
}

base *= 1000
if nano < 1000*base {
var us = int(nano / base)
if nano%base >= base/2 {
us++
}

return strconv.Itoa(us) + "us"
}

base *= 1000
if nano < 1000*base {
var ms = int(nano / base)
if nano%base >= base/2 {
ms++
}
return strconv.Itoa(ms) + "ms"
}

base *= 1000
var s = int(nano / base)
if nano%base >= base/2 {
s++
}
return strconv.Itoa(s) + "s"
}

const (
ErrWrongHumanTimeFormat = errors.Err("wrong human time format")
)
Expand Down Expand Up @@ -179,7 +153,9 @@ func IsLeapYear(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}

func NowDate(year, month, day, hour, minute, sec, nsec int) time.Time {
// DateDefNow create a timestamp with given field, default use value of now if a
// field is less than 0
func DateDefNow(year, month, day, hour, minute, sec, nsec int) time.Time {
now := Now()
nyear, nmonth, nday := now.Date()
nhour, nminute, nsec := now.Clock()
Expand Down
31 changes: 0 additions & 31 deletions time2/time_test.go
Expand Up @@ -7,37 +7,6 @@ import (
"github.com/cosiner/gohper/testing2"
)

func TestToHuman(t *testing.T) {
tt := testing2.Wrap(t)
type Test struct {
Time int64
Human string
}

tests := []Test{
Test{0, "0ns"},
Test{999, "999ns"},

Test{1000, "1us"},
Test{1499, "1us"},
Test{1500, "2us"},

Test{1000 * 1000, "1ms"},
Test{1000 * 1499, "1ms"},
Test{1000 * 1500, "2ms"},

Test{1000 * 1000 * 1000, "1s"},
Test{1000 * 1000 * 1499, "1s"},
Test{1000 * 1000 * 1500, "2s"},

Test{1000 * 1000 * 1000 * 10000, "10000s"},
}

for _, test := range tests {
tt.Eq(test.Human, ToHuman(test.Time))
}
}

func TestParseHuman(t *testing.T) {
testing2.
Expect(time.Hour, nil).Arg("1H").
Expand Down
27 changes: 9 additions & 18 deletions utils/pager/pager.go
Expand Up @@ -46,7 +46,7 @@ type PagerGroup struct {
lock sync.Mutex
}

func (pg *PagerGroup) Add(beginPage, beginIndex, pageSize int) (p *Pager) {
func (pg *PagerGroup) Add(beginPage, beginIndex, pageSize int) *Pager {
if beginPage < 0 {
beginPage = 1
}
Expand All @@ -56,22 +56,13 @@ func (pg *PagerGroup) Add(beginPage, beginIndex, pageSize int) (p *Pager) {
}

pg.lock.Lock()
if l := len(pg.pagers); l < cap(pg.pagers) {
pg.pagers = pg.pagers[:l+1]

p = &pg.pagers[l]
p.BeginPage = beginPage
p.BeginIndex = beginIndex
p.PageSize = pageSize
} else {
pg.pagers = append(pg.pagers, Pager{
BeginPage: beginPage,
BeginIndex: beginIndex,
PageSize: pageSize,
})
p = &pg.pagers[l]
}

l := len(pg.pagers)
pg.pagers = append(pg.pagers, Pager{
BeginPage: beginPage,
BeginIndex: beginIndex,
PageSize: pageSize,
})
p := &pg.pagers[l]
pg.lock.Unlock()
return
return p
}

0 comments on commit 5606d40

Please sign in to comment.