week number not following ISO 8601

52 views
Skip to first unread message

Ferran Vidal Marginet

unread,
Jul 10, 2019, 10:40:48 AM7/10/19
to weewx-user
Hello,

Today, 10th of July according to my Outlook calendar seems to be week 28 but the "%W" format in Weewx shows week 27. In general this year this format shows one more week than Excel/Outlook/etc.

Somebody knows why? Shouldn't follow ISO8610?

In this thread (https://groups.google.com/forum/#!msg/weewx-user/funRVI-T_p0/e58oZtK6AwAJ) says that week number follow the following rule:

%WWeek number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.

and this is not the same as ISO8610. Why? How can I get the same week number as ISO8610? The rule that ISO follows is:

There are several mutually equivalent and compatible descriptions of week 01:

    • the week with the year's first Thursday in it (the formal ISO definition),
    • the week with 4 January in it,
    • the first week with the majority (four or more) of its days in the starting year, and
    • the week starting with the Monday in the period 29 December – 4 January.
Acording to this today, 10th of July of 2019 is W28.

Thanks,
Ferran

Andrew Milner

unread,
Jul 10, 2019, 11:25:31 AM7/10/19
to weewx-user
googling came up with the following function to convert simple week number to iso week number:

From the simple week number to the ISO week number.

In [ISO8601], the week number is defined by:

  • weeks start on a monday
  • week 1 of a given year is the one that includes the first Thursday of that year. (or, equivalently, week 1 is the week that includes 4 January.)

This means that the days before week 1 in a given year are attributed to the last week of the previous year. Also the days that come after the last week of a given year are attributed to the first week of the next year.

If we adapt approximation SWN5 for the simple week number to reflect the differences between the definitions of both week numbers, we arrive at the final solution, adopted for the week number wristapp:

    ISO_WN( y, m, d )
    {
        dow     = DOW( y, m, d );
        dow0101 = DOW( y, 1, 1 );
    
        if      ( m == 1  &&  3 < dow0101 < 7 - (d-1) )
        {
            // days before week 1 of the current year have the same week number as
            // the last day of the last week of the previous year
    
            dow     = dow0101 - 1;
            dow0101 = DOW( y-1, 1, 1 );
            m       = 12;
            d       = 31;
        }
        else if ( m == 12  &&  30 - (d-1) < DOW( y+1, 1, 1 ) < 4 )
        {
            // days after the last week of the current year have the same week number as
            // the first day of the next year, (i.e. 1)
    
            return 1;
        }
    
        return ( DOW( y, 1, 1 ) < 4 ) + 4 * (m-1) + ( 2 * (m-1) + (d-1) + dow0101 - dow + 6 ) * 36 / 256;
    
    }

Thomas Keffer

unread,
Jul 10, 2019, 11:29:13 AM7/10/19
to weewx-user
The strftime directive "%V" will get you what you want:

>>> import datetime
>>> datetime.date(2019,7,10).strftime("%V")
'28'
>>> datetime.date(2019,7,10).strftime("%W")
'27'

-tk

--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/97d6b0f5-d717-4e51-8620-82b31c86c4c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ferran Vidal Marginet

unread,
Jul 10, 2019, 3:44:54 PM7/10/19
to weewx-user
Many thanks Thomas!
Ferran

Andrew Milner

unread,
Jul 10, 2019, 9:41:56 PM7/10/19
to weewx-user
..... I must read more manuals!!!!  You really are a mine of information Thomas!!

On Wednesday, 10 July 2019 22:44:54 UTC+3, Ferran Vidal Marginet wrote:
Many thanks Thomas!
Ferran
Reply all
Reply to author
Forward
0 new messages