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
