Menu
Coddy logo textTech

Creating a Specific Time

Part of the Logic & Flow section of Coddy's GO journey — lesson 59 of 68.

While time.Now() gives you the current moment, you'll often need to work with specific dates and times. Go provides the time.Date function for creating a time.Time object representing any date and time you specify.

The time.Date function takes seven parameters in this exact order: year, month, day, hour, minute, second, nanosecond, and location. Here's the basic syntax:

birthday := time.Date(1990, time.March, 15, 14, 30, 0, 0, time.UTC)

Notice that Go uses constants like time.March for months, which makes your code more readable than using numbers. You can also use integers (1-12) if you prefer. The location parameter typically uses time.UTC for UTC time or time.Local for your system's local timezone.

This approach gives you complete control over creating any specific moment in time, whether it's a birthday, deadline, or historical date. The resulting time.Time object works exactly like the one from time.Now() and can be used with all the same time operations.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a historical event timeline application that uses time.Date to represent specific dates and times from history. This challenge will test your ability to create time.Time objects for precise historical moments and display them in an organized format.

You will receive two inputs:

  • A string containing historical events in the format "event1:year:month:day:hour:minute,event2:year:month:day:hour:minute" (e.g., "Moon Landing:1969:7:20:20:17,Berlin Wall Fall:1989:11:9:21:0")
  • A string containing display options separated by commas in the format "option1,option2,option3" (e.g., "chronological,details,summary")

Your task is to:

  1. Parse the first input by splitting on commas to get individual event entries
  2. For each event entry, split on colons to extract the event name, year, month, day, hour, and minute
  3. Convert the numeric strings to integers and create a time.Time object using time.Date for each event (use time.UTC for the location parameter and 0 for seconds and nanoseconds)
  4. Display the application header: "=== HISTORICAL TIMELINE APPLICATION ==="
  5. Display the events loading confirmation: "Loading historical events..."
  6. For each event, display: "Loaded: [event_name] - [complete_time_object]"
  7. Parse the second input by splitting on commas to get display options
  8. Process each display option:
    • For "chronological": Display "=== CHRONOLOGICAL ORDER ===" followed by events sorted by date (earliest first), showing "[event_name]: [year]-[month]-[day] at [hour]:[minute] UTC"
    • For "details": Display "=== DETAILED INFORMATION ===" followed by each event showing:
      • "Event: [event_name]"
      • " Date: [year]-[month]-[day]"
      • " Time: [hour]:[minute] UTC"
      • " Weekday: [weekday_name]"
      • " Unix timestamp: [unix_seconds]"
    • For "summary": Display "=== TIMELINE SUMMARY ===" followed by:
      • "Total events: [number_of_events]"
      • "Earliest event: [earliest_event_name] ([year])"
      • "Latest event: [latest_event_name] ([year])"
      • "Time span: [years_between_earliest_and_latest] years"
    • For "years": Display "=== EVENTS BY DECADE ===" followed by events grouped by decade, showing "[decade]s: [comma_separated_event_names]"
  9. Display the completion message: "Historical timeline processing completed"

Use the time package to create specific dates with time.Date, the strings package to split input strings, the strconv package to convert string numbers to integers, and the fmt package for formatted output. Remember that time.Date takes parameters in the exact order: year, month, day, hour, minute, second, nanosecond, location. For sorting chronologically, you can compare time.Time objects using the Before() method. This challenge demonstrates how to create specific moments in time and work with historical data using Go's time functionality.

Cheat sheet

Use time.Date to create a time.Time object for a specific date and time:

birthday := time.Date(1990, time.March, 15, 14, 30, 0, 0, time.UTC)

The time.Date function takes seven parameters in exact order:

  • year (int)
  • month (time.Month or int 1-12)
  • day (int)
  • hour (int)
  • minute (int)
  • second (int)
  • nanosecond (int)
  • location (*time.Location)

Use month constants like time.March for readability, or integers (1-12). Common locations are time.UTC and time.Local.

Try it yourself

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
	"time"
)

func main() {
	// Read input using bufio.Scanner to handle spaces properly
	scanner := bufio.NewScanner(os.Stdin)
	
	scanner.Scan()
	eventsInput := scanner.Text()
	
	scanner.Scan()
	optionsInput := scanner.Text()
	
	// Parse events from input
	eventEntries := strings.Split(eventsInput, ",")
	
	// Parse display options from input
	displayOptions := strings.Split(optionsInput, ",")
	
	// TODO: Write your code below
	// 1. Parse each event entry to extract name, year, month, day, hour, minute
	// 2. Remember to check if parts slice has enough elements before accessing
	// 3. Convert string numbers to integers using strconv.Atoi()
	// 4. Create time.Time objects using time.Date() for each event
	// 5. Display the application header and loading messages
	// 6. Process each display option (chronological, details, summary, years)
	// 7. Display the completion message
	
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow