FreeBASIC  0.91.0
time_dateadd.c
Go to the documentation of this file.
1 /* dateadd function */
2 
3 #include "fb.h"
4 
5 /*:::::*/
6 FBCALL double fb_DateAdd( FBSTRING *interval, double interval_value_arg, double serial )
7 {
8  int year, month, day, hour, minute, second;
9  int carry_value, test_value;
10  int interval_value = (int) fb_FIXDouble( interval_value_arg );
11  int interval_type = fb_hTimeGetIntervalType( interval );
12 
14 
15  fb_hTimeDecodeSerial ( serial, &hour, &minute, &second, FALSE );
16  fb_hDateDecodeSerial ( serial, &year, &month, &day );
17 
18  switch ( interval_type ) {
20  year += interval_value;
21  break;
23  month += interval_value * 3;
24  break;
26  month += interval_value;
27  break;
31  day += interval_value;
32  break;
34  day += interval_value * 7;
35  break;
37  hour += interval_value;
38  break;
40  minute += interval_value;
41  break;
43  second += interval_value;
44  break;
46  default:
48  break;
49  }
50 
51  /* Normalize date/time */
52  switch ( interval_type ) {
60  /* Nothing to do here because normalization will implicitly be done
61  * by the calculation of the new serial number. */
62  break;
63 
67  /* Handle wrap-around for month */
68  if( month < 1 ) {
69  carry_value = (month - 12) / 12;
70  } else {
71  carry_value = (month - 1) / 12;
72  }
73  year += carry_value;
74  month -= carry_value * 12;
75  /* No wrap-around ... instead we must saturate the day */
76  test_value = fb_hTimeDaysInMonth( month, year );
77  if( day > test_value )
78  day = test_value;
79  break;
80 
81  }
82 
83  serial = fb_DateSerial( year, month, day ) +
84  fb_TimeSerial( hour, minute, second );
85 
86  return serial;
87 }