"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > 이동 시간 및 기간 | 프로그래밍 튜토리얼

이동 시간 및 기간 | 프로그래밍 튜토리얼

2024-11-02에 게시됨
검색:681

소개

Go Time and Duration | Programming Tutorials

이 랩의 목표는 Go의 시간 및 기간 지원에 대한 이해도를 테스트하는 것입니다.

시간

아래 코드에는 Go에서 시간과 기간을 사용하는 방법에 대한 예가 포함되어 있습니다. 그러나 코드의 일부가 누락되었습니다. 귀하의 임무는 예상대로 작동하도록 코드를 완성하는 것입니다.

  • Go 프로그래밍 언어에 대한 기본 지식
  • Go의 시간 및 기간 지원에 대해 잘 알고 있습니다.
$ go run time.go
2012-10-31 15:50:13.793654  0000 UTC
2009-11-17 20:34:58.651387237  0000 UTC
2009
November
17
20
34
58
651387237
UTC
Tuesday
true
false
false
25891h15m15.142266763s
25891.25420618521
1.5534752523711128e 06
9.320851514226677e 07
93208515142266763
2012-10-31 15:50:13.793654  0000 UTC
2006-12-05 01:19:43.509120474  0000 UTC

# Next we'll look at the related idea of time relative to
# the Unix epoch.

아래에 전체 코드가 있습니다.

// Go offers extensive support for times and durations;
// here are some examples.

package main

import (
    "fmt"
    "time"
)

func main() {
    p := fmt.Println

    // We'll start by getting the current time.
    now := time.Now()
    p(now)

    // You can build a `time` struct by providing the
    // year, month, day, etc. Times are always associated
    // with a `Location`, i.e. time zone.
    then := time.Date(
        2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
    p(then)

    // You can extract the various components of the time
    // value as expected.
    p(then.Year())
    p(then.Month())
    p(then.Day())
    p(then.Hour())
    p(then.Minute())
    p(then.Second())
    p(then.Nanosecond())
    p(then.Location())

    // The Monday-Sunday `Weekday` is also available.
    p(then.Weekday())

    // These methods compare two times, testing if the
    // first occurs before, after, or at the same time
    // as the second, respectively.
    p(then.Before(now))
    p(then.After(now))
    p(then.Equal(now))

    // The `Sub` methods returns a `Duration` representing
    // the interval between two times.
    diff := now.Sub(then)
    p(diff)

    // We can compute the length of the duration in
    // various units.
    p(diff.Hours())
    p(diff.Minutes())
    p(diff.Seconds())
    p(diff.Nanoseconds())

    // You can use `Add` to advance a time by a given
    // duration, or with a `-` to move backwards by a
    // duration.
    p(then.Add(diff))
    p(then.Add(-diff))
}

요약

이 실습에서는 Go의 시간 및 기간 지원 작업 능력을 테스트했습니다. 시간 값의 다양한 구성요소를 추출하고, 두 시간을 비교하고, 기간의 길이를 계산하고, 주어진 기간만큼 시간을 앞당기는 방법을 배웠습니다.


? 지금 연습하세요: 이동 시간 및 기간 탐색


더 자세히 알고 싶으십니까?

  • ? 최신 바둑 스킬 트리를 알아보세요
  • ? 더 많은 Go 튜토리얼을 읽어보세요
  • ? Discord에 참여하거나 @WeAreLabEx로 트윗해 주세요.
릴리스 선언문 이 글은 https://dev.to/labex/go-time-and-duration-programming-tutorials-786?1에서 복제됩니다.1 침해 내용이 있는 경우, [email protected]으로 연락하여 삭제하시기 바랍니다.
최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3