Repository: google/pprof
Pull request #903 (2024-10-08 22:26:54)

tui: use RFC3339-alike format with second precision for "Time"
The current time format (Jan 2, 2006 at 3:04pm (MST)) has a few issues:

 - It displays time at minute granularity, while profiles usually have have second-level (and more[^go]) granularity.
 - It uses a 12-hour clock, which is tricky to handle at AM/PM switchovers, for people (like myself) who are not used to it. By contrast, a 24-hour clock is unambiguous.
 - Minor: using names for months makes it more difficult to calculate with time differences.

Hence, I propose replacing it with an RFC3339-alike format, with spaces between elements for better readability.

[^go]: https://github.com/golang/go/blob/3aa71c12eacd68ec16e7172d92aa5c6af32f0c3b/src/runtime/pprof/proto.go#L351

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/google/pprof/pull/903?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
Attention: Patch coverage is `0%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 66.42%. Comparing base [(`0ed6a68`)](https://app.codecov.io/gh/google/pprof/commit/0ed6a68c8d9e6d5df69a1f2694c7b5c3297fdf4f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) to head [(`043aa6f`)](https://app.codecov.io/gh/google/pprof/commit/043aa6f0b57418a02a031551b5d376d11d8c3532?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).
> Report is 45 commits behind head on main.

| [Files with missing lines](https://app.codecov.io/gh/google/pprof/pull/903?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Patch % | Lines |
|---|---|---|
| [internal/report/report.go](https://app.codecov.io/gh/google/pprof/pull/903?src=pr&el=tree&filepath=internal%2Freport%2Freport.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-aW50ZXJuYWwvcmVwb3J0L3JlcG9ydC5nbw==) | 0.00% | [1 Missing :warning: ](https://app.codecov.io/gh/google/pprof/pull/903?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #903      +/-   ##
==========================================
- Coverage   66.86%   66.42%   -0.44%     
==========================================
  Files          44       44              
  Lines        9824     9857      +33     
==========================================
- Hits         6569     6548      -21     
- Misses       2794     2868      +74     
+ Partials      461      441      -20     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/google/pprof/pull/903?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).

aalexand: @aktau It's unfortunate that no test had to be updated. Should we look into adding a test that would verify the change? My worry is that by just looking at the code I can't tell if we used a format string that will work as expected. If adding an automated test is too much hassle then could you confirm that you tested the change manually / locally?
aktau: OK, I added a test. PTAL.

Patch:
diff --git a/internal/report/report.go b/internal/report/report.go
index 5ab0da6..8e73f17 100644
--- a/internal/report/report.go
+++ b/internal/report/report.go
@@ -1173,7 +1173,7 @@ func ProfileLabels(rpt *Report) []string {
 		label = append(label, "Doc: "+url)
 	}
 	if prof.TimeNanos != 0 {
-		const layout = "Jan 2, 2006 at 3:04pm (MST)"
+		const layout = "2006-01-02 15:04:05 MST"
 		label = append(label, "Time: "+time.Unix(0, prof.TimeNanos).Format(layout))
 	}
 	if prof.DurationNanos != 0 {


Test patch:
diff --git a/internal/report/report_test.go b/internal/report/report_test.go
index 845c369..bbb89a7 100644
--- a/internal/report/report_test.go
+++ b/internal/report/report_test.go
@@ -21,8 +21,10 @@ import (
 	"path/filepath"
 	"regexp"
 	"runtime"
+	"slices"
 	"strings"
 	"testing"
+	"time"
 
 	"github.com/google/pprof/internal/binutils"
 	"github.com/google/pprof/internal/graph"
@@ -595,3 +597,21 @@ func TestDocURLInLabels(t *testing.T) {
 		t.Errorf("expected URL %q not found in %s", url, labels)
 	}
 }
+
+func TestProfileLabels(t *testing.T) {
+	// Force the local timezone to UTC for the duration of this function to get a
+	// predictable result out of timezone printing.
+	defer func(prev *time.Location) { time.Local = prev }(time.Local)
+	time.Local = time.UTC
+
+	profile := testProfile.Copy()
+	profile.TimeNanos = time.Unix(131, 0).UnixNano()
+	rpt := New(profile, &Options{
+		SampleValue: func(v []int64) int64 { return v[1] },
+	})
+
+	const want = "Time: 1970-01-01 00:02:11 UTC"
+	if labels := ProfileLabels(rpt); !slices.Contains(labels, want) {
+		t.Errorf("wanted to find a label containing %q, but found none in %v", want, labels)
+	}
+}