kubernetes: Human readable duration formats exact year inconsistently

What happened: When the duration is exactly 2 years or 3 years, human readable duration is returned as 2y0d or 3y0d. But when the duration is exactly 4 years, 5 years, etc, the human readable duration is returned as 4y, 5y, etc.

What you expected to happen: The format of an exact year should be consistent. Remove 0d when duration is an exact year:

  • 2y0d should be 2y
  • 3y0d should be 3y
  • Check other years… 4, 5, 6, 7… not sure if they also have the problem.

How to reproduce it (as minimally and precisely as possible): Because this is time-dependent, it can be difficult to reproduce via the CLI, but you can see the problem illustrated in this unit test which shows the current behavior:

		{d: 2 * 365 * 24 * time.Hour, want: "2y0d"},
		{d: 2*365*24*time.Hour + 23*time.Hour, want: "2y0d"},
		{d: 3 * 365 * 24 * time.Hour, want: "3y0d"},
		{d: 8*365*24*time.Hour - time.Millisecond, want: "7y364d"},
		{d: 8 * 365 * 24 * time.Hour, want: "8y"},
		{d: 8*365*24*time.Hour + 364*24*time.Hour, want: "8y"},
		{d: 9 * 365 * 24 * time.Hour, want: "9y"},

The 2y0d and 3y0d test cases should be changed to want 2y and 3y, and the code should be changed to make that pass.

Anything else we need to know?: Note, this is not a problem when the duration is exactly 1 year because the human readable duration is still shown in days. So nothing needs to change to handle the case of exactly 1 year.

Environment:

  • Kubernetes version (use kubectl version): 1.18
  • Cloud provider or hardware configuration: n/a
  • OS (e.g: cat /etc/os-release): n/a
  • Kernel (e.g. uname -a): n/a
  • Install tools: n/a
  • Network plugin and version (if this is a network-related bug): n/a
  • Others: n/a

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 20 (19 by maintainers)

Most upvoted comments

(FTR I agree that Xy0d looks worse than Xy.)

On Thu, Apr 9, 2020 at 4:26 PM Brian Pursley notifications@github.com wrote:

I think it is just used for a ballpark duration. It loses accuracy as the duration increases anyway because it changes the units. I guess my intention for opening the issue was to make it consistent with itself. For example it doesn’t say 2d0h or 2h0m, so it should not say 2y0d.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kubernetes/kubernetes/issues/89960#issuecomment-611800395, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAE6BFX4EP65RSQTNLAQQ3DRLZKR3ANCNFSM4MD6YNSA .

I think it is just used for a ballpark duration. It loses accuracy as the duration increases anyway because it changes the units. I guess my intention for opening the issue was to make it consistent with itself. For example it doesn’t say 2d0h or 2h0m, so it should not say 2y0d.

Also I just updated the issue description. Check the other exact years 4, 5, 6, 7 because they might also have the problem, I can’t remember.

I know 8 and beyond don’t have the problem because it just stops showing days after 8 years.