timelint

package module
v0.0.0-...-a288941 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2021 License: MIT Imports: 5 Imported by: 0

README

What is timelint?

timelint is a lint tool to avoid problems related to timezone when writing Go programs.

When calling functions that depend on a timezone, such as the Date() and Hour() functions, you must be aware of the timezone of the time.Time variable, or you may encounter a fatal bug. Since these bugs are generally hard to find in reviews and tests, they should be checked with lint tools.

Examples

The following code will output reports in three lines where timezone is not explicitly specified.

func myTimeFunc() time.Time {
	return time.Now()
}

func f() {
	a := time.Now()
	fmt.Printf("%+v", a.Day()+20) // report "time.Day() called without In(timezone)"
	fmt.Printf("%+v", a.In(time.UTC).Day())
	if h := a.Hour(); h > 10 { // report "time.Hour() called without In(timezone)"
		fmt.Println(h)
	}
	defer func() {
		fmt.Println(myTimeFunc().Date()) // report "time.Date() called without In(timezone)"
	}()
}

How to install

go get -u github.com/tomoemon/go-time-lint

How to use

timelint ./...

Not implemented yet

Calling method assigned to a variable

timelint cannot detect calling methods without In() if they are called after being assigned to a variable.

day := time.Now().Day
fmt.Println(day())

Timezone specified variable

Even if the timezone is explicitly specified in the same function, it will raise the report unless it is specified immediately before.

utcTime := time.Now().In(time.UTC)
fmt.Println(utcTime.Day())

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "timelint",
	Doc:  "timelint checks if timezone-aware method is called with timezone",
	Run:  run,
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL