This commit is contained in:
badkaktus 2019-10-09 22:28:15 +03:00
parent 74329f91b4
commit 8d6f62699d
2 changed files with 13 additions and 14 deletions

View File

@ -1,7 +1,6 @@
// A common requirement in programs is getting the number // Общим требованием в программах является получение
// of seconds, milliseconds, or nanoseconds since the // количества секунд, миллисекунд или наносекунд в [Unixtime](http://en.wikipedia.org/wiki/Unix_time).
// [Unix epoch](http://en.wikipedia.org/wiki/Unix_time). // Вот как это сделать в Go.
// Here's how to do it in Go.
package main package main
@ -12,24 +11,24 @@ import (
func main() { func main() {
// Use `time.Now` with `Unix` or `UnixNano` to get // Используйте `time.Now` с `Unix` или `UnixNano`,
// elapsed time since the Unix epoch in seconds or // чтобы получить время, прошедшее с начала эпохи Unix в
// nanoseconds, respectively. // секундах или наносекундах соответственно.
now := time.Now() now := time.Now()
secs := now.Unix() secs := now.Unix()
nanos := now.UnixNano() nanos := now.UnixNano()
fmt.Println(now) fmt.Println(now)
// Note that there is no `UnixMillis`, so to get the // Обратите внимание, что `UnixMillis` не существует,
// milliseconds since epoch you'll need to manually // поэтому, чтобы получить миллисекунды с начала эпохи Unix,
// divide from nanoseconds. // вам нужно будет вручную делить наносекунды.
millis := nanos / 1000000 millis := nanos / 1000000
fmt.Println(secs) fmt.Println(secs)
fmt.Println(millis) fmt.Println(millis)
fmt.Println(nanos) fmt.Println(nanos)
// You can also convert integer seconds or nanoseconds // Вы также можете конвертировать целые секунды или наносекунды
// since the epoch into the corresponding `time`. // Unixtime в соответствующее `время`.
fmt.Println(time.Unix(secs, 0)) fmt.Println(time.Unix(secs, 0))
fmt.Println(time.Unix(0, nanos)) fmt.Println(time.Unix(0, nanos))
} }

View File

@ -6,5 +6,5 @@ $ go run epoch.go
2012-10-31 16:13:58 +0000 UTC 2012-10-31 16:13:58 +0000 UTC
2012-10-31 16:13:58.292387 +0000 UTC 2012-10-31 16:13:58.292387 +0000 UTC
# Next we'll look at another time-related task: time # Далее мы рассмотрим еще одну задачу, связанную со
# parsing and formatting. # временем: разбор и форматирование времени.