| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package main
- import (
- "fmt"
- "time"
- )
- func main() {
- timeMethod()
- /*
- // Получаем текущее время
- now := time.Now()
- // Создаем времч с помощью конкретных значений
- currentTime := time.Date(
- 2020,
- time.May,
- 15, // день
- 10, // часы
- 13, // минуты
- 12, // секунды
- 45, // наносекунды
- time.UTC,
- )
- // Создаем время, используя секунды и наносекунды, прошедшие с начала эпохи Unix
- unixTime := time.Unix(
- 150000, // секунды
- 1, // наносекунды
- )
- // 01-02 03:04.05 0006 15
- // день месяц час(12) минуты секунды год час(24)
- // Форматируем и выводим время в строковом виде
- // fmt.Println(now.Format("02-01-2006 15:04:05"))
- fmt.Println(now.Format("02-01-2006 15:04:05"))
- fmt.Println(currentTime.Format("02-01-2006 15:04:05"))
- fmt.Println(unixTime.Format("02-01-2006 15:04:05"))
- // Функция Parse парсит строку в соответствии с заданным шаблоном
- firstTime, err := time.Parse("2006/01/02 15-04", "2020/05/15 17-45")
- if err != nil {
- panic(err)
- }
- fmt.Println(firstTime.Format("02-01-2006 15:04:05"))
- // LoadLocation находит временную зону в справочнике IANA
- // https://www.iana.org/time-zones
- loc, err := time.LoadLocation("Asia/Yekaterinburg")
- if err != nil {
- panic(err)
- }
- // Функция ParseInLocation парсит строку в указанной временной зоне
- secondTime, err := time.ParseInLocation("Jan 2 06 03:04:05pm", "May 15 20 05:45:10pm", loc)
- if err != nil {
- panic(err)
- }
- fmt.Println(firstTime.Format("02-01-2006 15:04:05")) // 15-05-2020 17:45:00
- fmt.Println(secondTime.Format("02-01-2006 15:04:05")) // 15-05-2020 17:45:10
- */
- }
- func timeMethod() {
- current := time.Date(2020, time.May, 15, 17, 45, 12, 0, time.Local)
- fmt.Println(current.Date())
- fmt.Println(current.Year())
- fmt.Println(current.Month())
- fmt.Println(current.Day())
- fmt.Println(current.Clock())
- fmt.Println(current.Hour())
- fmt.Println(current.Minute())
- fmt.Println(current.Second())
- fmt.Println(current.Unix())
- fmt.Println(current.Weekday())
- fmt.Println(current.YearDay())
- current = time.Date(2020, time.May, 15, 17, 45, 12, 0, time.Local)
- fmt.Println(current.Format("02-01-2006 15:04:05"))
- // Сравнение структур Time
- firstTime := time.Date(2020, time.May, 15, 17, 45, 12, 0, time.Local)
- secondTime := time.Date(2020, time.May, 15, 16, 45, 12, 0, time.Local)
- // func (t Time) After(u Time) bool
- // true если позже
- fmt.Println(firstTime.After(secondTime)) // true
- // func (t Time) Before(u Time) bool
- // true если раньше
- fmt.Println(firstTime.Before(secondTime)) // false
- // func (t Time) Equal(u Time) bool
- // true если равны
- fmt.Println(firstTime.Equal(secondTime)) // false
- // Методы, изменяющие структуру Time
- now := time.Date(2020, time.May, 15, 17, 45, 12, 0, time.Local)
- // func (t Time) Add(d Duration) Time
- // изменяет дату в соответствии с параметром - "продолжительностью"
- future := now.Add(time.Hour * 12) // перемещаемся на 12 часов вперед
- // func (t Time) AddDate(years int, months int, days int) Time
- // изменяет дату в соответствии с параметрами - количеством лет, месяцев и дней
- past := now.AddDate(-1, -2, -3) // перемещаемся на 1 год, два месяца и 3 дня назад
- // func (t Time) Sub(u Time) Duration
- // вычисляет время, прошедшее между двумя датами
- fmt.Println(future.Sub(past)) // 10332h0m0s
- }
|