Go Time and Duration | Programming Tutorials

Introduction

This lab aims to test your understanding of Go’s time and duration support.

Time

The code below contains examples of how to work with time and duration in Go. However, some parts of the code are missing. Your task …


This content originally appeared on DEV Community and was authored by Labby

Introduction

MindMap

This lab aims to test your understanding of Go's time and duration support.

Time

The code below contains examples of how to work with time and duration in Go. However, some parts of the code are missing. Your task is to complete the code to make it work as expected.

  • Basic knowledge of Go programming language.
  • Familiarity with Go's time and duration support.
$ 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.

There is the full code below:

// 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))
}

Summary

This lab tested your ability to work with Go's time and duration support. You learned how to extract various components of a time value, compare two times, compute the length of a duration, and advance a time by a given duration.

🚀 Practice Now: Go Time and Duration Exploration

Want to Learn More?


This content originally appeared on DEV Community and was authored by Labby


Print Share Comment Cite Upload Translate Updates
APA

Labby | Sciencx (2024-09-11T21:23:48+00:00) Go Time and Duration | Programming Tutorials. Retrieved from https://www.scien.cx/2024/09/11/go-time-and-duration-programming-tutorials/

MLA
" » Go Time and Duration | Programming Tutorials." Labby | Sciencx - Wednesday September 11, 2024, https://www.scien.cx/2024/09/11/go-time-and-duration-programming-tutorials/
HARVARD
Labby | Sciencx Wednesday September 11, 2024 » Go Time and Duration | Programming Tutorials., viewed ,<https://www.scien.cx/2024/09/11/go-time-and-duration-programming-tutorials/>
VANCOUVER
Labby | Sciencx - » Go Time and Duration | Programming Tutorials. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/11/go-time-and-duration-programming-tutorials/
CHICAGO
" » Go Time and Duration | Programming Tutorials." Labby | Sciencx - Accessed . https://www.scien.cx/2024/09/11/go-time-and-duration-programming-tutorials/
IEEE
" » Go Time and Duration | Programming Tutorials." Labby | Sciencx [Online]. Available: https://www.scien.cx/2024/09/11/go-time-and-duration-programming-tutorials/. [Accessed: ]
rf:citation
» Go Time and Duration | Programming Tutorials | Labby | Sciencx | https://www.scien.cx/2024/09/11/go-time-and-duration-programming-tutorials/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.