[Go] Time

2022. 8. 2. 11:33·Go
반응형

Golang Time 패키지

Time struct

type Time struct {
    wall uint64
    ext  int64
    loc *Location
}

Time 생성 방법

time.now()
time.Date(year int, month Month...)
time.Unix(sec int64, nsec int64) 

Time Test

func TestTime(t *testing.T) {
    now := time.Now()
    t.Log(now) // 2022-08-02 11:14:48.8866312 +0900 KST m=+0.002534901

    nowFormat := time.Now().Format("2006-01-02 15:04:05")
    t.Log(nowFormat) // 2022-08-02 11:17:33 (https://flaviocopes.com/go-date-time-format/?fbclid=IwAR0zPE9XTBDAKnrWza6m6cOGQecch-MWdf1VNdcMGvCPSBxfhEIJcje2j6I)

    utc := time.UTC
    kst, _ := time.LoadLocation("Asia/Seoul")

    whenUtc := time.Date(2020, 1, 1, 12, 10, 20, 0, utc)
    t.Log(whenUtc) // 2020-01-01 12:10:20 +0000 UTC
    whenKst := time.Date(2020, 1, 1, 12, 10, 20, 0, kst)
    t.Log(whenKst) // 2020-01-01 12:10:20 +0900 KST
}

Duration

type Duration int64

// 아래 미리 정의된 Constants를 사용할 수도 있다.
const (
   Nanosecond  Duration = 1
   Microsecond          = 1000 * Nanosecond
   Millisecond          = 1000 * Microsecond
   Second               = 1000 * Millisecond
   Minute               = 60 * Second
   Hour                 = 60 * Minute
)

Duration Test

func TestDuration(t *testing.T) {
    when := time.Date(2020, 1, 1, 12, 10, 20, 0, time.UTC)
    t.Log(when) // 2020-01-01 12:10:20 +0000 UTC

    whenAddSec := when.Add(time.Second * 10)
    whenAddHour := when.Add(time.Hour * 10)
    t.Log(whenAddSec)  // 2020-01-01 12:10:30 +0000 UTC
    t.Log(whenAddHour) // 2020-01-01 22:10:20 +0000 UTC

    assert.Equal(t, when.Before(whenAddSec), true)
    assert.Equal(t, when.Before(whenAddHour), true)

    assert.Equal(t, when.After(whenAddSec), false)
    assert.Equal(t, when.After(whenAddHour), false)

    assert.Equal(t, when.Equal(whenAddSec), false)
    assert.Equal(t, when.Equal(whenAddHour), false)
}
728x90
반응형
저작자표시 비영리 변경금지 (새창열림)
'Go' 카테고리의 다른 글
  • [Go] Factory Pattern(팩토리 패턴) 구현
  • [Go] Channel 이용한 Queue
  • [Go] Int, String, Struct 정렬
  • [Go] Linked List 구현
상쾌한기분
상쾌한기분
  • 상쾌한기분
    상쾌한기분
    상쾌한기분
  • 전체
    오늘
    어제
    • 분류 전체보기 (251)
      • Python (44)
        • Python (26)
        • Django (6)
        • Flask (4)
        • Open Source (6)
      • Kotlin & Java (5)
        • Spring (2)
        • 프로젝트 (1)
      • Go (11)
      • Database (24)
        • MySQL (21)
        • Redis (3)
      • Infrastructure (2)
        • CDC (4)
        • Kafka (5)
        • Prometheus (2)
        • Fluentd (11)
        • Docker (1)
        • Airflow (2)
        • VPN (2)
      • IT (26)
        • AI (9)
        • Langchain (8)
        • Web (18)
        • Git (8)
        • 리팩토링 (9)
        • Micro Service Architecture (8)
        • Clean Code (16)
        • Design Pattern (0)
        • 수학 (1)
        • 알고리즘 (14)
      • OS (14)
        • Centos (10)
        • Ubuntu (3)
        • Mac (1)
      • Search Engine (2)
        • ElasticSearch (1)
        • Lucene Solr (1)
      • PHP (2)
        • Laravel (1)
        • Codeigniter (1)
  • 블로그 메뉴

    • Github 방문
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Redis
    ollama
    Kafka
    오블완
    python
    http
    go
    git
    MYSQL
    prompt
    LLM
    티스토리챌린지
    백준
    Golang
    CDC
    docker
    Langchain
    파이썬
    performance
    fluentd
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
상쾌한기분
[Go] Time
상단으로

티스토리툴바