summaryrefslogtreecommitdiff
path: root/time/titrax/patches/patch-weekno.perl
blob: 23f2366c1c4d1333eaf05ad1c4e22ea50cb72bfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
$NetBSD: patch-weekno.perl,v 1.1 2014/11/05 22:41:38 he Exp $

Fix this so that it uses week numbering according to ISO 8601.

--- weekno.perl.orig	1995-10-17 13:44:49.000000000 +0000
+++ weekno.perl
@@ -1,7 +1,7 @@
 #!/local/bin/perl
 # Package for date handling
-# Use the PERL std library timelocal - older had his own
-require "timelocal.pl";
+# Use the PERL std package Time::Local
+use Time::Local;
 
 package DATE;
 #               J   F   M   A   M   J   J   A   S   O   N   D
@@ -51,7 +51,21 @@ sub weekno {
 
 
 sub firstdayfirstweek {
-# Return first day of week 1 of any year
+# Return the day of the year (0 is Jan 1st)
+# for the first day of week 1 of any year.
+#
+# Quoting strftime(3):
+#  %V    is replaced by the week number of the year (Monday as the first day
+#        of the week) as a decimal number [01,53]. According to ISO 8601 the
+#        week containing January 1 is week 1 if it has four or more days in
+#        the new year, otherwise it is week 53 of the previous year, and the
+#        next week is week 1.  The year is given by the `%G' conversion
+#        specification.
+#
+# So this will return a negative value in some cases, i.e. when
+# Jan 1st falls on either of Tuesday, Wednesday or Thursday, and
+# will be positive (1) or zero for Sunday or Monday respectively.
+#
    local($y) = @_;
    local($ret);
    # Get time of January 1, 0.0.0.0
@@ -59,13 +73,14 @@ sub firstdayfirstweek {
    local($firsttime) = &main'timelocal(0, 0, 0, 1, 0, $y, 0, 0, 0);
    local(@firstday) = localtime($firsttime);
    local($wday) = $firstday[6];
-   # Rule works for some years.....89 to 92 tested, they all hit branch 2...
-   if ($wday > 3) {
-      $ret = 8 - $wday;
+   # Weekday 4 is Thursday (localtime returns zero-based with Sunday = 0)
+   if ($wday <= 4) {
+       $ret = 1 - $wday;	# first day of week 1 may be in late December
+       				# The exception is when Jan 1 = Sunday (wday 0)
    } else {
-      $ret = 1 - $wday;
+       $ret = 8 - $wday;	# first day of week 1 is in January
    }
-   $ret;
+   return $ret;
 }
  
 sub firstinweek {