Skip to content

Commit

Permalink
style(linter): fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
mahbubzulkarnain committed Aug 17, 2020
1 parent 68e7048 commit 84f33aa
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 58 deletions.
3 changes: 0 additions & 3 deletions dummy.go

This file was deleted.

7 changes: 7 additions & 0 deletions dummy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package waktu_test

import (
. "github.com/gomodul/waktu"
)

var now = Date(1993, 9, 10, 13, 37, 11, 11, GetLocationByUTCOffset(7)) //nolint:gochecknoglobals,nolintlint
15 changes: 8 additions & 7 deletions endof.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ import (
"time"
)

// EndOfDay ex: 1993-09-10 23:59:59.999999999 +0700 GMT+7
// EndOfDay ex: 1993-09-10 23:59:59.999999999 +0700 GMT+7.
func (t Time) EndOfDay() Time {
return t.StartOfDay().AddDate(0, 0, 1).Add(-time.Nanosecond)
}

// EndOfWeek ex: 1993-09-11 23:59:59.999999999 +0700 GMT+7
// EndOfWeek ex: 1993-09-11 23:59:59.999999999 +0700 GMT+7.
func (t Time) EndOfWeek(startWeek ...Weekday) Time {
weekday := 6
if len(startWeek) > 0 {
weekday += int(startWeek[0])
}
weekday -= int(t.Weekday())
return t.AddDate(0, 0, weekday).EndOfDay()

return t.AddDate(0, 0, weekday-int(t.Weekday())).EndOfDay()
}

// EndOfMonth ex: 1993-09-30 23:59:59.999999999 +0700 GMT+7
// EndOfMonth ex: 1993-09-30 23:59:59.999999999 +0700 GMT+7.
func (t Time) EndOfMonth() Time {
return t.StartOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond)
}

// EndOfYear ex: 1993-12-31 23:59:59.999999999 +0700 GMT+7
// EndOfYear ex: 1993-12-31 23:59:59.999999999 +0700 GMT+7.
func (t Time) EndOfYear() Time {
return t.SetMonth(12).EndOfMonth()
month := 12
return t.SetMonth(month).EndOfMonth()
}
4 changes: 3 additions & 1 deletion endof_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package waktu
package waktu_test

import (
"fmt"
"testing"

. "github.com/gomodul/waktu"
)

func TestTime_EndOfDay(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ func fmtInt(buf []byte, v uint64) int {
w := len(buf)
if v == 0 {
w--

buf[w] = '0'
} else {
for v > 0 {
w--
buf[w] = byte(v%10) + '0'

const ten = 10
buf[w] = byte(v%ten) + '0'
v /= 10
}
}

return w
}
4 changes: 3 additions & 1 deletion layout_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package waktu
package waktu_test

import (
"testing"

. "github.com/gomodul/waktu"
)

func TestTime_Format_ANSIC(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions location.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"
)

// GetLocationByUTCOffset func(offset int) *time.Location
// GetLocationByUTCOffset func(offset int) *time.Location.
func GetLocationByUTCOffset(offset int) *time.Location {
if offset == 0 {
return time.UTC
Expand All @@ -16,5 +16,7 @@ func GetLocationByUTCOffset(offset int) *time.Location {
name = fmt.Sprintf("GMT+%v", offset)
}

return time.FixedZone(name, offset*60*60)
const sixty = 60

return time.FixedZone(name, offset*sixty*sixty)
}
9 changes: 7 additions & 2 deletions location_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package waktu
package waktu_test

import (
"testing"
"time"

. "github.com/gomodul/waktu"
)

func TestGetLocationByUTCOffset(t *testing.T) {
func TestGetLocationByUTCOffset_Offset0(t *testing.T) {
if GetLocationByUTCOffset(0) != time.UTC {
t.Fatal("invalid value")
}
}

func TestGetLocationByUTCOffset_Offset1(t *testing.T) {
if GetLocationByUTCOffset(1) == time.UTC {
t.Fatal("invalid value")
}
Expand Down
14 changes: 7 additions & 7 deletions month.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package waktu

// Month int
// Month int.
type Month int

const (
Expand Down Expand Up @@ -41,8 +41,8 @@ const (
Desember
)

// months var
var months = [...]string{
// months var.
var months = [...]string{ //nolint:gochecknoglobals
"Januari",
"Februari",
"Maret",
Expand All @@ -62,17 +62,17 @@ func (t Time) Month() Month {
return Month(t.Time.Month())
}

// SetMonth func
// SetMonth func.
func (t *Time) SetMonth(month int) Time {
return Date(t.Year(), Month(month), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}

// String func
// String func.
func (m Month) String() string {
if Januari <= m && m <= Desember {
return months[m-1]
}
buf := make([]byte, 20)
buf := make([]byte, 20) //nolint:wsl
n := fmtInt(buf, uint64(m))
return "%!Month(" + string(buf[n:]) + ")"
return "%!Month(" + string(buf[n:]) + ")" //nolint:wsl
}
2 changes: 1 addition & 1 deletion month_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package waktu
package waktu_test

import (
"fmt"
Expand Down
9 changes: 5 additions & 4 deletions startof.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package waktu

// StartOfDay ex: 1993-09-10 00:00:00 +0700 GMT+7
// StartOfDay ex: 1993-09-10 00:00:00 +0700 GMT+7.
func (t Time) StartOfDay() Time {
return t.ResetTimeLocal()
}

// StartOfWeek ex: 1993-09-05 00:00:00 +0700 GMT+7
// StartOfWeek ex: 1993-09-05 00:00:00 +0700 GMT+7.
func (t Time) StartOfWeek(startWeek ...Weekday) Time {
weekday := int(t.Weekday())
if len(startWeek) > 0 {
weekday -= int(startWeek[0])
}

return t.AddDate(0, 0, -weekday).StartOfDay()
}

// StartOfMonth ex: 1993-09-01 00:00:00 +0700 GMT+7
// StartOfMonth ex: 1993-09-01 00:00:00 +0700 GMT+7.
func (t Time) StartOfMonth() Time {
return Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
}

// StartOfYear ex: 1993-01-01 00:00:00 +0700 GMT+7
// StartOfYear ex: 1993-01-01 00:00:00 +0700 GMT+7.
func (t Time) StartOfYear() Time {
return Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
}
4 changes: 3 additions & 1 deletion startof_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package waktu
package waktu_test

import (
"fmt"
"testing"

. "github.com/gomodul/waktu"
)

func TestTime_StartOfDay(t *testing.T) {
Expand Down
50 changes: 27 additions & 23 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,119 +4,123 @@ import (
"time"
)

// Time struct
// Time struct.
type Time struct {
time.Time
}

// LastDay func
// LastDay func.
func LastDay() Time {
return Now().LastDay()
}

// Now func
// Now func.
func Now() Time {
loc, _ := time.LoadLocation("Asia/Jakarta")
return Time{
time.Now().In(loc),
}
return Time{time.Now().In(loc)}
}

// Parse func
// Parse func.
func Parse(layout, value string) (Time, error) {
t, err := time.Parse(layout, value)
return Time{t}, err
}

// Date func
// Date func.
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *time.Location) Time {
if loc == nil {
loc = time.UTC
}

return Time{time.Date(year, time.Month(month), day, hour, min, sec, nsec, loc)}
}

// AddDate func
// AddDate func.
func (t Time) AddDate(years int, months int, days int) Time {
return Time{t.Time.AddDate(years, months, days)}
}

// Add func
// Add func.
func (t Time) Add(d time.Duration) Time {
return Time{t.Time.Add(d)}
}

// ResetTime ex: 1993-09-10 07:00:00 +0700 GMT+7
// ResetTime ex: 1993-09-10 07:00:00 +0700 GMT+7.
func (t Time) ResetTime() Time {
return Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC).In(t.Location())
}

// ResetTimeLocal ex: 1993-09-10 00:00:00 +0700 GMT+7
// ResetTimeLocal ex: 1993-09-10 00:00:00 +0700 GMT+7.
func (t Time) ResetTimeLocal() Time {
return Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}

// SetDate func
// SetDate func.
func (t Time) SetDate(date interface{}) Time {
return Date(t.Year(), t.Month(), date.(int), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}

// GetDate ex: 930910
// GetDate ex: 930910.
func (t Time) GetDate(format ...string) string {
layout := YYMMDD
if len(format) > 0 {
layout = format[0]
}

return t.Format(layout)
}

// GetDateUTC ex: 930910
// GetDateUTC ex: 930910.
func (t Time) GetDateUTC(format ...string) string {
layout := YYMMDD
if len(format) > 0 {
layout = format[0]
}

return t.In(time.UTC).Format(layout)
}

// SetHour func
// SetHour func.
func (t Time) SetHour(hour interface{}) Time {
return Date(t.Year(), t.Month(), t.Day(), hour.(int), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}

// SetMinute func
// SetMinute func.
func (t Time) SetMinute(minute interface{}) Time {
return Date(t.Year(), t.Month(), t.Day(), t.Hour(), minute.(int), t.Second(), t.Nanosecond(), t.Location())
}

// SetSecond func
// SetSecond func.
func (t Time) SetSecond(second interface{}) Time {
return Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), second.(int), t.Nanosecond(), t.Location())
}

// SetNanosecond func
// SetNanosecond func.
func (t Time) SetNanosecond(nanosecond interface{}) Time {
return Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), nanosecond.(int), t.Location())
}

// In func
// In func.
func (t Time) In(loc *time.Location) Time {
return Time{
t.Time.In(loc),
}
}

// GetTime 133700
// GetTime 133700.
func (t Time) GetTime(format ...string) string {
layout := HHMMSS
if len(format) > 0 {
layout = format[0]
}

return t.Format(layout)
}

// LastDay ex: 1993-09-30 13:37:11.00000001 +0700 GMT+7
// LastDay ex: 1993-09-30 13:37:11.00000001 +0700 GMT+7.
func (t Time) LastDay() Time {
day := 24 * time.Hour
var twentyFour time.Duration = 24
day := twentyFour * time.Hour

return t.SetDate(1).AddDate(0, 1, 0).Add(-day - time.Nanosecond)
}
5 changes: 4 additions & 1 deletion time_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package waktu
package waktu_test

import (
"fmt"
"testing"
"time"

. "github.com/gomodul/waktu"
)

func TestNow(t *testing.T) {
Expand All @@ -12,6 +14,7 @@ func TestNow(t *testing.T) {

func TestParse(t *testing.T) {
value := "9309"

resParse, errParse := Parse(YYMM, value)
if errParse != nil {
t.Fatal(errParse)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package waktu

// Version current version
// Version current version.
const Version = "v0.0.1-alpha.1"
Loading

0 comments on commit 84f33aa

Please sign in to comment.