FreeBASIC  0.91.0
time_week.c
Go to the documentation of this file.
1 /* week functions */
2 
3 #include "fb.h"
4 #include <math.h>
5 
6 /*:::::*/
7 void fb_hGetBeginOfWeek( int *pYear, int *pMonth, int *pDay,
8  int first_day_of_week )
9 {
10  double serial;
11  int weekday;
12 
13  serial = fb_DateSerial( *pYear, *pMonth, *pDay );
14  weekday = fb_Weekday( serial, first_day_of_week );
15  serial -= weekday - 1;
16 
17  fb_hDateDecodeSerial( serial, pYear, pMonth, pDay );
18 }
19 
20 /*:::::*/
21 double fb_hGetFirstWeekOfYear( int year,
22  int first_day_of_year, int first_day_of_week )
23 {
24  int first_week_year, first_week_month, first_week_day;
25  double serial_week_begin, serial_year_begin;
26  int remaining_weekdays;
27 
28  if( first_day_of_year==FB_WEEK_FIRST_SYSTEM ) {
29  /* FIXME: query system default */
30  first_day_of_year = FB_WEEK_FIRST_DEFAULT;
31  }
32 
33  serial_year_begin = fb_DateSerial( year, 1, 1 );
34 
35  first_week_day = 1;
36  first_week_month = 1;
37  first_week_year = year;
38  fb_hGetBeginOfWeek( &first_week_year, &first_week_month, &first_week_day,
39  first_day_of_week );
40 
41  serial_week_begin = fb_DateSerial( first_week_year,
42  first_week_month,
43  first_week_day );
44  remaining_weekdays = (int) ((serial_week_begin + 7.0l) - serial_year_begin);
45 
46  switch( first_day_of_year ) {
48  break;
50  if( remaining_weekdays < 4 ) {
51  serial_week_begin += 7.0l;
52  }
53  break;
55  if( remaining_weekdays < 7 ) {
56  serial_week_begin += 7.0l;
57  }
58  break;
59  }
60 
61  return serial_week_begin;
62 }
63 
64 /*:::::*/
65 int fb_hGetWeekOfYear( int ref_year, double serial,
66  int first_day_of_year, int first_day_of_week )
67 {
68  int sign;
69  int year, week;
70  double serial_first_week;
71 
72  fb_hDateDecodeSerial( serial, &year, NULL, NULL );
73 
74  serial_first_week =
75  fb_hGetFirstWeekOfYear( ref_year,
76  first_day_of_year, first_day_of_week );
77 
78  serial = floor( serial - serial_first_week);
79  sign = fb_hSign( serial );
80  serial /= 7.0l;
81  week = (int) (serial + sign);
82 
83  return week;
84 }
85 
86 /*:::::*/
87 int fb_hGetWeeksOfYear( int ref_year, int first_day_of_year, int first_day_of_week )
88 {
89  double serial_start =
90  fb_hGetFirstWeekOfYear( ref_year,
91  first_day_of_year, first_day_of_week );
92  double serial_end =
93  fb_hGetFirstWeekOfYear( ref_year + 1,
94  first_day_of_year, first_day_of_week );
95  return (int) ((serial_end - serial_start) / 7.0l);
96 }