FreeBASIC  0.91.0
time_datediff.c
Go to the documentation of this file.
1 /* datediff function */
2 
3 #include "fb.h"
4 #include <math.h>
5 
6 /*:::::*/
7 FBCALL long long fb_DateDiff( FBSTRING *interval, double serial1, double serial2,
8  int first_day_of_week, int first_day_of_year )
9 {
10  int year1, month1, hour, minute, second, week;
11  int year2, month2;
12  long long result = 0;
13  double serial;
14  int interval_type = fb_hTimeGetIntervalType( interval );
15 
17 
18  switch ( interval_type ) {
20  fb_hDateDecodeSerial ( serial1, &year1, NULL, NULL );
21  fb_hDateDecodeSerial ( serial2, &year2, NULL, NULL );
22  result = year2 - year1;
23  break;
26  fb_hDateDecodeSerial ( serial1, &year1, &month1, NULL );
27  fb_hDateDecodeSerial ( serial2, &year2, &month2, NULL );
28  result = (month2 - month1) + (year2 - year1) * 12;
29  if( interval_type==FB_TIME_INTERVAL_QUARTER )
30  result = result / 3;
31  break;
34  result = (long long) (floor(serial2) - floor(serial1));
35  break;
38  fb_hDateDecodeSerial ( serial1, &year1, NULL, NULL );
39  week = fb_hGetWeekOfYear( year1,
40  serial1,
41  first_day_of_year, first_day_of_week );
42  result = fb_hGetWeekOfYear( year1,
43  serial2,
44  first_day_of_year, first_day_of_week );
45  if( week > 0 )
46  --week;
47  if( result > 0 )
48  --result;
49  result -= week;
50  if( interval_type==FB_TIME_INTERVAL_WEEKDAY ) {
51  int add_value;
52  if( serial1 > serial2 ) {
53  double serial_tmp = serial1;
54  serial1 = serial2;
55  serial2 = serial_tmp;
56  add_value = 1;
57  } else {
58  add_value = -1;
59  }
60  if( fb_Weekday( serial1, first_day_of_week ) > fb_Weekday( serial2, first_day_of_week ) )
61  result += add_value;
62  }
63  break;
65  serial = serial2 - serial1;
66  fb_hTimeDecodeSerial ( serial, &hour, NULL, NULL, FALSE );
67  result = (long long) (hour + floor(serial) * 24.0l);
68  break;
70  serial = serial2 - serial1;
71  fb_hTimeDecodeSerial ( serial, &hour, &minute, NULL, FALSE );
72  result = (long long) (minute + (hour + floor(serial) * 24.0l) * 60.0l);
73  break;
75  serial = serial2 - serial1;
76  fb_hTimeDecodeSerial ( serial, &hour, &minute, &second, FALSE );
77  result = (long long) (second + (minute + (hour + floor(serial) * 24.0l) * 60.0l) * 60.0l);
78  break;
80  default:
82  break;
83  }
84 
85  return result;
86 }