FreeBASIC  0.91.0
time_decodeserdate.c
Go to the documentation of this file.
1 /* functions to decode a serial date number */
2 
3 #include "fb.h"
4 #include <math.h>
5 
6 /*:::::*/
7 FBCALL void fb_hDateDecodeSerial ( double serial,
8  int *pYear, int *pMonth, int *pDay )
9 {
10  int tmp_days;
11  int cur_year = 1900;
12  int cur_month = 1;
13  int cur_day = 1;
14 
15  serial = floor( serial );
16 
17  serial -= 2;
18  while( serial < 0 ) {
19  serial += fb_hTimeDaysInYear( --cur_year );
20  }
21 
22  while( serial >= (tmp_days = fb_hTimeDaysInYear( cur_year ) ) ) {
23  serial -= tmp_days;
24  ++cur_year;
25  }
26 
27  if( pMonth || pDay ) {
28  while( serial >= (tmp_days = fb_hTimeDaysInMonth( cur_month, cur_year ) ) ) {
29  serial -= tmp_days;
30  ++cur_month;
31  }
32  }
33 
34  cur_day += serial;
35 
36  if( pYear )
37  *pYear = cur_year;
38  if( pMonth )
39  *pMonth = cur_month;
40  if( pDay )
41  *pDay = cur_day;
42 }
43 
44 FBCALL int fb_Year( double serial )
45 {
46  int year;
47  fb_hDateDecodeSerial( serial, &year, NULL, NULL );
48  return year;
49 }
50 
51 FBCALL int fb_Month( double serial )
52 {
53  int month;
54  fb_hDateDecodeSerial( serial, NULL, &month, NULL );
55  return month;
56 }
57 
58 FBCALL int fb_Day( double serial )
59 {
60  int day;
61  fb_hDateDecodeSerial( serial, NULL, NULL, &day );
62  return day;
63 }
64 
69 FBCALL int fb_Weekday( double serial, int first_day_of_week )
70 {
71  int dow = ((int) (floor(serial) - 1) % 7) + 1;
72 
73  if( first_day_of_week==FB_WEEK_DAY_SYSTEM ) {
74  /* FIXME: query system default */
75  first_day_of_week = FB_WEEK_DAY_DEFAULT;
76  }
77 
78  dow -= first_day_of_week - 1;
79  if( dow < 1 ) {
80  dow += 7;
81  } else if( dow > 7 ) {
82  dow -= 7;
83  }
84  return dow;
85 }
86 
87 /*:::::*/
88 int fb_hGetDayOfYearEx( int year, int month, int day )
89 {
90  int result = 0;
91  int cur_month;
92  for( cur_month=1; cur_month!=month; ++cur_month )
93  result += fb_hTimeDaysInMonth( cur_month, year );
94  return result + day;
95 }
96 
97 /*:::::*/
98 int fb_hGetDayOfYear( double serial )
99 {
100  int year, month, day;
101  fb_hDateDecodeSerial( serial, &year, &month, &day );
102  return fb_hGetDayOfYearEx( year, month, day );
103 }