移動時間と所要時間 |プログラミングチュートリアル

王林
リリース: 2024-09-12 10:16:35
オリジナル
515 人が閲覧しました

導入

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 の時間と期間のサポートを使用して作業する能力をテストしました。時間値のさまざまな要素を抽出し、2 つの時間を比較し、期間の長さを計算し、指定された期間だけ時間を進める方法を学習しました。


?今すぐ練習: 移動時間と期間の探索


もっと詳しく知りたいですか?

  • ?最新の Go スキル ツリーを学ぶ
  • ?続きを読む Go チュートリアル
  • ? Discord に参加するか、@WeAreLabEx でツイートしてください

以上が移動時間と所要時間 |プログラミングチュートリアルの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!