|
Handling and definition of weeknumbers according to ISO ? |
Posted by M. Koenen on Nov-06-2008 01:10 |
|
Hi,
I am struggling with the latest version of ChartDirector and its definition of Week numbers. I was expecting it to handle week numbers (with selectStartOfWeek() ) to be ISO compliant, but unfortunately it isnt. Is this a bug or by design?
I have a test script (see code below) which prints the date which was used by selectStartOfWeek to show you. The output shows that Chartdirector thinks
that the weeks start at Sunday as all the dates returned are Sundays.
However the Iso week definition: http://en.wikipedia.org/wiki/ISO_week_date says that start of week is Monday.
Can you elaborate on this?
Best regards
M. Koenen
Code:
=====
my $aggr = new ArrayMath( $Chart_timestamps );
$aggr->selectStartOfWeek();
my $aggrTimeStamps = $aggr->aggregate($Chart_timestamps, $perlchartdir::Aggregat
eFirst);
foreach $check ( @$aggrTimeStamps )
{
printf("timeweekstart: %0.f\\n", perlchartdir::getChartYMD($check) );
}
Result:
=====
timeweekstart: 20071216
timeweekstart: 20071223
timeweekstart: 20071230
timeweekstart: 20080106
timeweekstart: 20080113
timeweekstart: 20080120
timeweekstart: 20080127 |
Re: Handling and definition of weeknumbers according to ISO ? |
Posted by Peter Kwan on Nov-06-2008 01:53 |
|
Hi M. Koenen,
The first day of a week is considered as Sunday in ChartDirector. This is chosen to be consistent with the programming language (in your case, it is Perl). The Perl language numbers the weekdays as 0 - 6 for Sunday to Saturday (instead of 1 - 7 from Monday to Sunday as per ISO). I think most programming languages uses Sunday as the start of the week.
Also, personally, I suspect (but I have no proof - it is just my opinion) that most people considered Sunday to be the start of the week. See "http://en.wikipedia.org/wiki/Days_of_the_week".
Note that ChartDirector does not return "Week Numbers". It only filters out the dates that are the first available dates of weeks.
If you need to compute the week numbers, or want to use Monday as the start of the week, you may need to write a function yourself to pick out the start of the week from the date array. (If the data are from a database, you may also modify your SQL query so it returns the week number.)
Hope this can help.
Regards
Peter Kwan |
Re: Handling and definition of weeknumbers according to ISO ? |
Posted by M. Koenen on Nov-06-2008 05:08 |
|
Hi Peter,
Thanks for the swift reply. I understand that it is debatable in different countries what the definition of the first day of the week is. That's why for professional / business / financial usage they had to define a standard like ISO and I was hoping that ChartDirector as it is a professional package would somehow support this standard.
For my perl programming I use the Date::Calc package (http://search.cpan.org/dist/Date-Calc/Calc.pod) to be able to use correct week and date calculations. That's why I noticed strange deviations by ChartDirector reports for the weeks when I used the ChartD. aggregate by week function.
Well, at least I know now what 'algorithm' ChartDirector uses to determine the new week. I will have to play around with alternative function to see how I can manipulate the $aggr array myself to cope with the ISO standard.
Thanks again!
M. Koenen |
Re: Handling and definition of weeknumbers according to ISO ? |
Posted by M. Koenen on Nov-06-2008 05:11 |
|
Oh, I forgot to ask you to document that selectStartOfWeek(); uses Sunday as start of week. That will help others to understand as well
Best regards
M. Koenen |
Re: Handling and definition of weeknumbers according to ISO ? |
Posted by Peter Kwan on Nov-06-2008 22:56 |
|
Hi M. Koenen,
I have encountered cases in which the customers even requested the first day of week be Saturday. They said it was what the "end users" expected in their country, or it needed to be consistent with the official calendar of their government, or that it was required by the laws of their country, etc..
I was thinking may be we can make it configurable in ChartDirector, so it can meet the needs of all our customers. However, after some thoughts, I found an easy solution that works for any day of the week. The code is like:
#use Monday as the start of the week
my $aggr = new ArrayMath($Chart_timestamps)->sub(86400)->selectStartOfWeek();
Hope this can help.
Regards
Peter Kwan |
Re: Handling and definition of weeknumbers according to ISO ? |
Posted by Peter Kwan on Nov-06-2008 23:01 |
|
Hi M. Koenen,
And most importantly, we need to update our documentation to make it clear the first day of the week in Sunday in selectStartOfWeek. We may probably include the above script fragment in the documentation to inform our users how to adjust it to use other days as the first day of the week.
Also, I think the "general code" should be:
my $aggr = new ArrayMath($Chart_timestamps)->sub(86400)->selectStartOfWeek()->add(86400);
The last ->add(86400) is not needed (but does not harm) if the $aggr is used as an aggregator, but is needed if the $aggr itself is used for display.
Hope this can help.
Regards
Peter Kwan |
Re: Handling and definition of weeknumbers according to ISO ? |
Posted by M. Koenen on Nov-06-2008 23:39 |
|
Hi Peter,
YES! Your tip works. Now I can use the build in aggregate function again!
Thank you for this.
Best regards,
M. Koenen |
|