Rounding a date up or down

I’m working on a program which reads from a log file and groups certain events by hour. I’ve hit a snag while rounding the hours of the first and last entries in the log.
So basically for better presentation I’m rounding the first entry down, and rounding the hour of the last entry up.

I checked around and saw a solution posted by Rob for rounding the hour for a given time.Time type down:

fmt.Println(time.LocalTime().Format("1/2/2006 15:00"))

I have couple of questions regarding this, is there a similar way to round the hour of a given date up?
Also the documentation for the time package mentions a predefined layout for formatting and parsing time

Mon Jan 2 15:04:05 MST 2006

Then why does the format posted by Rob work?

Any help is appreciated.

It works because “00” isn’t part of the recognized format and thus becomes a literal “00”.

To round up, Truncate() the time to hour precision (a real round down) and Add() an hour.

1 Like

Solved.
Thank you.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.