spencermountain / spacetime

A lightweight javascript timezone library
http://spacetime.how/
Other
3.97k stars 183 forks source link

unixFmt('ww') wrong / Calender Week of Year #303

Open nmaas87 opened 3 years ago

nmaas87 commented 3 years ago

Dear all,

var s = spacetime.now();
s = s.goto('UTC');
console.log(s.unixFmt('ww'));

is giving me 33, while it is Calender Week 32... Any ideas?

spencermountain commented 3 years ago

hey thanks Nico, apologies for the delay.

if you run this in the cli: date +"So this is week: %U" it appears our results are in-line with the unix date command. That's what we should return, i think.

I think it's probably just a off-by-one confusion, can you confirm? thanks

nmaas87 commented 3 years ago

Hey there :) Sorry I cannot confirm:

https://man7.org/linux/man-pages/man1/date.1.html

%U     week number of year, with Sunday as first day of week
              (00..53)

trying this in WSL gives me: So this is week: 34 (I am on a Windows 10, German machine, but as date is configuring Sunday to be first day of the new week (in Germany we have monday as first day ;)) - this is correct)

Spacetime tells me on the same machine its week: 34, so far so good.

However, if I set the machines date to tomorrow (Monday), date tells me still week 34, but spacetime tells me its week 35, So it looks like Spacetime trys to match with the German timesystem (changing week count from sunday->monday but its off by one).

So I started adding a "Calendar Week: " + (s.unixFmt('ww')-1); which worked out and has been flawless for me.

I guess its an ugly error to search for, and I should not bandaid in that way, but I needed some solution fast :)

Cheers,

Nico

spencermountain commented 3 years ago

oh right - good observation. If I remember, we define weeks as starting on monday, and the first week is one with a thursday in it. I can look into how the unix command defines it, and write some documentation. cheers

nmaas87 commented 3 years ago

Yeah, but it seems to be still one week ahead of the real one and I have to decrement 1 as written above. Maybe you could check if its something you could observe as well :)