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

王林
풀어 주다: 2024-09-12 10:16:35
원래의
516명이 탐색했습니다.

소개

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로 트윗해 주세요.

위 내용은 이동 시간 및 기간 | 프로그래밍 튜토리얼의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!