Given a date, is there an algorithm that can convert it to the week of the year?
-
1Lots of unknowns here - what operating system is this for, what shell are you using, what format does the week need to be in (e.g. number 1-52?) Are you looking for a math formula or a bash script? – ifiht Jul 27 '22 at 17:51
-
I am in a linux environment using python trying to mimic the output of a pandas output (e.g. dt.weekofyear). – J. Ike Jul 28 '22 at 14:08
-
I am looking for a math formula. – J. Ike Jul 28 '22 at 20:51
-
Does this answer your question? https://stackoverflow.com/questions/2600775/how-to-get-week-number-in-python – erickson Jul 29 '22 at 03:04
-
I know I've seen a math formula before, just can't seem to find it anymore. I can't use a library since my team is developing a server based on the "chapel" language that works with its own type of big data arrays. – J. Ike Jul 29 '22 at 11:49
-
If you saw a formula, it was probably approximate. There's no formula to compute daylight saving changes that are enacted by legislatures with no warning (or even any rational reason, usually). If you can list some simplifying assumptions, like you are working in Etc/UTC time zone, specify the day of the week *you* start *your* week, and *your* rules for deciding what is the first week of the year, you could get something rudimentary working. – erickson Jul 29 '22 at 16:14
-
No it wasn't. I was asked what operating system I was working in and I am using Python to code a formula if I could find one. So, again, there must be a formula if other libraries such as Pandas can create date-time objects and convert them back and forth between dates and epoch seconds. – J. Ike Jul 30 '22 at 01:11
-
"So, again, there must be a formula if other libraries such as Pandas can create date-time objects..." No, that doesn't follow necessarily. Other libraries rely on the "Olson database." It's data collected by human researchers and updated many times each year. These data are required if your time zone observes daylight saving (or might change its offset from UTC for any reason in the future). That's why I asked if you can assume Etc/UTC as the time zone, as well as other necessary, use-case specific information like what day your week starts and when the first week of the year starts. – erickson Aug 01 '22 at 15:39
-
I actually found an algorithm from the Pandas library. – J. Ike Aug 02 '22 at 17:02
2 Answers
Absolutely, but it gets very complicated.
In principle you should be able to divide by 60*60*24 = 86400
to get days, then follow the logic of the calendar to figure out days, weeks, years, and calculate the answer from that. There are 365 days in a year, except leap years. Leap years occur on years divisible by 4, except ones divisible by 100, but happen again on years divisible by 400. Since 2000 is divisible by 400, you can ignore the last 2 rules and you will be correct until the year 2100.
You also have to decide what a week is defined as. In the USA it is traditionally defined as Sunday through Saturday. In Europe it is traditionally defined as Monday through Sunday. But you know what day of the week 1970 started on (Thursday), and can therefore figure out the current year, what day of the week it started on, when that week started, and a little modulo 7 arithmetic gives you your answer.
That is...until you notice that actual date boundaries depend on timezone, whether daylight savings time is in effect, and other such things. This opens up a giant can of worms that everyone delegates to the Olson database. (Which itself needs multiple updates a year because some government, somewhere, tweaks their timezone rules.) And then every language and environment wraps their own date-time library around that. You are strongly advised to find and use that.
If time is represented in UTC, this is the end of the story. However in fact we also have leap seconds (27 so far, possibly a negative one coming soon). This is NOT dealt with by Olson or standard date-time libraries. All of whom try to find the most efficient way to ignore that the leap second happened, and pray that they don't crash when the next one comes. (Not a joke. Linux servers around the world crashed on Jul 2, 2012, and large companies have a variety of "time smearing" approaches to avoid it happening again.)
Only specialized tools like Frink deal with the ugliness of leap seconds in their full glory.

- 34,287
- 7
- 49
- 68

- 43,296
- 3
- 59
- 88
-
-
@erickson https://www.telegraph.co.uk/news/2021/01/04/earth-spinning-faster-now-time-past-half-century/ is linked from the Wikipedia page that I gave. As for how it can slow down AND speed up, see https://www.researchgate.net/publication/241394516_Decadal_Variations_in_Earth_Rotation_and_Mechanisms_of_Core-Mantle_Coupling. – btilly Jul 27 '22 at 21:16
-
Interesting. I doubt they will introduce a negative leap second. It's more likely that they will discontinue leap seconds altogether. – erickson Jul 27 '22 at 21:32
-
@erickson Such proposals have been made. See, for instance, https://engineering.fb.com/2022/07/25/production-engineering/its-time-to-leave-the-leap-second-in-the-past/. But the tail has been wagging the dog so long on this one that I'm not optimistic. – btilly Jul 27 '22 at 22:03
Assuming you already have the year, month, and day, here is the solution according to Pandas.
doy = get_day_of_year(year, month, day)
dow = dayofweek(year, month, day)
# estimate
iso_week = (doy - 1) - dow + 3
if iso_week >= 0:
iso_week = iso_week // 7 + 1
# verify
if iso_week < 0:
if (iso_week > -2) or (iso_week == -2 and is_leapyear(year - 1)):
iso_week = 53
else:
iso_week = 52
elif iso_week == 53:
if 31 - day + dow < 3:
iso_week = 1

- 19
- 5
-
This doesn't answer the question, which was how to convert *epoch time in seconds* to the week of year. Your input is completely different, and getting the date sidesteps all the difficulties involved in converting from Unix time to a local date (time zone, daylight saving, and leap days). If this is what you wanted, you asked the wrong question entirely. – erickson Aug 25 '22 at 16:25
-
Sorry, I had an algorithm to get the year, month, and day from Epoch seconds. So, I used that algorithm to then get the week of year. – J. Ike Aug 26 '22 at 17:41
-
I will edit the question, then this answer will make sense for it. Although code-only answers are not the best. Maybe you can provide some explanation of the algorithm too. – erickson Aug 26 '22 at 19:57