-2

The following program is not returning the desired value

package main

import (
    "fmt"
    "time"
)

func main() {
    t:= time.Date(2011, 1, 1, 0, 0, 0, 0, time.UTC)
    _, week := t.ISOWeek()
    fmt.Printf("\nWeek of the year: %v\n", week)


}

it returns

52

instead of

1

For me, makes sense that the first day of the year must be in the first week of the year. I couldn't find an alternative function in the docs

zeellos
  • 139
  • 11
  • You are arguing based on your opinions, rather than facts, references, or specific expertise. The Go ISOWeek function is an implementation of the ISO 8601 standard. This question is primarily opinion-based. – peterSO Jul 28 '18 at 00:54
  • ISOs are not facts, only conventions, but I like your answer – zeellos Jul 29 '18 at 20:12

1 Answers1

4

Look at the docs. Particularly "Week ranges from 1 to 53. Jan 01 to Jan 03 of year n might belong to week 52 or 53 of year n-1, and Dec 29 to Dec 31 might belong to week 1 of year n+1"

Mainly your assumption that there are 52 weeks in a year is wrong. Which is why you are confused.

Go's calculation of week 52 is the same as every other programming language. Please take a look at this JavaScript implementation for that date, notice it too provides 52 as the week.

You're asking for a function for something which isn't accurate or useful, which is why such a function does not exist in the language. To make that function work, weeks would have to be cut off at the beginning and end of the year. Perhaps you don't realize a week is defined as 7 days, various countries and cultures begin and end their weeks on different days.

You can trivially write a function to do what you want to accomplish with your definition of a "week", but no there is not one in the standard library.

Zachscs
  • 3,353
  • 7
  • 27
  • 52
  • I didn't say the year has 52 weeks, I said that the program returns 52 – zeellos Jul 28 '18 at 00:56
  • 2
    The reason for that is in the penultimate paragraph @zeellos, the week is the 52th week of 2010, and that number gets "carried over" to the next year, so the week doesn't have two numbers. When it comes to dates and times, things are rarely neat and clearcut, and most simple rules have a boatload of exceptions :-( – Martin Tournoij Jul 28 '18 at 08:26