dgryski / dgoogauth

Google Authenticator for Go
426 stars 59 forks source link

use utc time #3

Closed zemirco closed 8 years ago

dgryski commented 9 years ago

Oops. This is definitely the correct fix. I want to merge this, but at the same time don't want to break everybody who's currently using this code :/

Need a way to allow users to inject time, and have the local time injected by default.

kisom commented 9 years ago

Maybe add a UTC bool field to OTPConfig: it will default to false, but if it set to true, it uses UTC. Then the code might be:

if c.UTC {
    t0 := int(time.Now().Unix() / 30)
} else {
    t0 := int(time.Now().UTC().Unix() / 30)
}
cedricfung commented 7 years ago

I don't understand how does this make difference?

int(time.Now().UTC().Unix() / 30) should equal to int(time.Now().Unix() / 30), right?

AndrewWPhillips commented 5 years ago

I don't understand how does this make difference?

Exactly. time.Now().Unix() and time.Now().UTC().Unix() will return the same value. The Unix() function returns the current Epoch value which is always UTC.

dgryski commented 5 years ago

You're right.

https://golang.org/pkg/time/#Time.Unix says

The result does not depend on the location associated with t.

Seem like reverting this fix should simplify the code and otherwise be a no-op.