liveBook · Manning

Timestamp

As explained in Learn Go with Pocket-Sized Projects

A timestamp is a value representing a specific point in time, typically including date, time, and time-zone offset information.

A timestamp records when a habit was ticked and lets the service group ticks by ISO 8601 year and week. The storage keeps time.Time values under each habit and week. section 10.7.2

Use timestamps to make time-based habit operations explicit:

  • Pass a timestamp when adding a tick so forgotten activity can be recorded in the past. section 10.7.5
  • Convert the value with Go's ISOWeek method before storing or retrieving weekly status. section 10.7.2
  • Represent API values with google.protobuf.Timestamp and RFC3339 strings such as 2024-01-25T10:05:08+00:00. section 10.7.5
  • Treat server time as authoritative when client and server may use different time zones. section 10.7.5

Recording a tick

A gRPC client can create Timestamp: timestamppb.Now(). The backend receives it, verifies that the habit exists, and calls AddTick with the habit ID and time.Time value. section 11.3.2 section 10.7.2

Listing 10.37 api/proto/service.go: GetHabitStatus definition
    // GetHabitStatus is the endpoint to retrieve the status of 
    //ticks of a habit.
    rpc GetHabitStatus(
        GetHabitStatusRequest
    ) returns (GetHabitStatusResponse);

// GetHabitStatusRequest is the request to GetHabitStatus endpoint.
message GetHabitStatusRequest {
    // The identifier of the habit we want to retrieve.
    string habit_id = 1;

    // The time for which we want to retrieve the status of a habit.
    optional google.protobuf.Timestamp timestamp = 2;
}

// GetHabitStatusResponse is the response to retrieving the status of a 
// habit.
message GetHabitStatusResponse {
    // All the information of a habit.
    Habit habit = 1;
    // The number of times the habit has been ticked for a given week.
    int32 ticks_count = 2;
}

Inspect the GetHabitStatus request to see an optional Protobuf timestamp selecting the week whose tick count is returned. section 10.7.4

Listing 10.35 repository/memory.go: ticks storage
// ticksPerWeek holds all the timestamps for a given week number.
type ticksPerWeek map[isoweek.ISO8601][]time.Time

// HabitRepository holds all the current habits.
type HabitRepository struct {
    habits map[habit.ID]habit.Habit #1
    ticks  map[habit.ID]ticksPerWeek #2
}

Inspect the Go repository structure that stores timestamp slices for each habit and ISO week. section 10.7.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains storage, ISO-week conversion, API representation, and time-zone tradeoffs, but does not develop timestamp internals.
Examples
Uses weekly habit ticks, GetHabitStatus requests, RFC3339 values, timestamppb.Now(), and cache insertion or expiration times.
What it assumes
Assumes Go, gRPC, and Protobuf request handling while demonstrating timestamp use within a habit-tracking service.
What you can do after
Apply timestamps via Go time.Time and Protobuf Timestamp values to record ticks and retrieve weekly status.
How it connects
Connects timestamps to ISO 8601, time zones, Protocol Buffers, gRPC, habit tracking, and time-to-live caching.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.