FreeBASIC  0.91.0
time_core.c
Go to the documentation of this file.
1 /* core time/date functions */
2 
3 #include "fb.h"
4 
5 /*:::::*/
6 int fb_hTimeLeap( int year )
7 {
8  if( ( year % 400 ) == 0 )
9  return 1;
10  if( ( year % 100 ) == 0 )
11  return 0;
12  return ( ( ( year & 3 ) == 0 ) ? 1 : 0 );
13 }
14 
15 /*:::::*/
16 int fb_hTimeDaysInMonth( int month, int year )
17 {
18  static const int days[] =
19  { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
20  DBG_ASSERT(month >= 1 && month <= 12 );
21  if( month==2 )
22  return days[month-1] + fb_hTimeLeap( year );
23  return days[month-1];
24 }
25 
26 /*:::::*/
27 void fb_hNormalizeDate( int *pDay, int *pMonth, int *pYear )
28 {
29  DBG_ASSERT( pDay!=NULL );
30  DBG_ASSERT( pMonth!=NULL );
31  DBG_ASSERT( pYear!=NULL );
32  {
33  int day = *pDay;
34  int month = *pMonth;
35  int year = *pYear;
36  if( month < 1 ) {
37  int sub_months = -month + 1;
38  int sub_years = (sub_months + 11) / 12;
39  year -= sub_years;
40  month = sub_years * 12 - sub_months + 1;
41  } else {
42  --month;
43  year += month / 12;
44  month %= 12;
45  ++month;
46  }
47 
48  if( day < 1 ) {
49  int sub_days = -day + 1;
50  while (sub_days > 0) {
51  int dom;
52  if( --month==0 ) {
53  month=12;
54  --year;
55  }
56  dom = fb_hTimeDaysInMonth( month, year );
57  if( sub_days > dom ) {
58  sub_days -= dom;
59  } else {
60  day = dom - sub_days + 1;
61  sub_days = 0;
62  }
63  }
64  } else {
65  int dom = fb_hTimeDaysInMonth( month, year );
66  while( day > dom ) {
67  day -= dom;
68  if( ++month==13 ) {
69  month = 1;
70  ++year;
71  }
72  dom = fb_hTimeDaysInMonth( month, year );
73  }
74  }
75  *pDay = day;
76  *pMonth = month;
77  *pYear = year;
78  }
79 }
80 
81 /*:::::*/
83 {
84  int result = FB_TIME_INTERVAL_INVALID;
85 
86  if( interval!=NULL && interval->data!=NULL ) {
87  if( strcmp( interval->data, "yyyy" )==0 ) {
88  result = FB_TIME_INTERVAL_YEAR;
89  } else if( strcmp( interval->data, "q" )==0 ) {
90  result = FB_TIME_INTERVAL_QUARTER;
91  } else if( strcmp( interval->data, "m" )==0 ) {
92  result = FB_TIME_INTERVAL_MONTH;
93  } else if( strcmp( interval->data, "y" )==0 ) {
95  } else if( strcmp( interval->data, "d" )==0 ) {
96  result = FB_TIME_INTERVAL_DAY;
97  } else if( strcmp( interval->data, "w" )==0 ) {
98  result = FB_TIME_INTERVAL_WEEKDAY;
99  } else if( strcmp( interval->data, "ww" )==0 ) {
101  } else if( strcmp( interval->data, "h" )==0 ) {
102  result = FB_TIME_INTERVAL_HOUR;
103  } else if( strcmp( interval->data, "n" )==0 ) {
104  result = FB_TIME_INTERVAL_MINUTE;
105  } else if( strcmp( interval->data, "s" )==0 ) {
106  result = FB_TIME_INTERVAL_SECOND;
107  }
108  }
109 
110  return result;
111 }