Datum add_months(PG_FUNCTION_ARGS) { DateADT day = PG_GETARG_DATEADT(0); int n = PG_GETARG_INT32(1); int y, m, d; int days; DateADT result; div_t v; bool last_day; j2date(day + POSTGRES_EPOCH_JDATE, &y, &m, &d); last_day = (d == days_of_month(y, m)); v = div(y * 12 + m - 1 + n, 12); y = v.quot; if (y < 0) y += 1; /* offset because of year 0 */ m = v.rem + 1; days = days_of_month(y, m); if (last_day || d > days) d = days; result = date2j(y, m, d) - POSTGRES_EPOCH_JDATE; PG_RETURN_DATEADT (result); }
void rtc_increase_days() { CCR &= ~CLKEN; DOM++; if (DOM > days_of_month(get_month(), get_year())) DOM = days_of_month(get_month(), get_year()); CCR |= CLKEN; }
bool date::is_valid(int32_t __year, int32_t __month, int32_t __day) { return (__year >= 0 && __year <= 9999) && (__month >= 1 && __month <= 12) && (__day >= 1 && __day <= days_of_month(__year, __month)); }
void getCalenday(int y, int m, int calenday[][5]) { int day; int days = days_of_month(m,y); for (day = 1;day <= days;day++) { int i = getDayOfWeek(y,m,day); int j = day / 7; printf("i = %d,j = %d, day = %d\n", i,j,day); calenday[i][j] = day; } }
int32_t date::day_of_year() const { int32_t __doy = 0; for (int32_t __month = 1; __month < _month; ++__month) { __doy += days_of_month(_year, __month); } __doy += _day; return __doy; }
int main(void) { int year,month,day,numberday=0,i; int days_of_month(int year, int month); printf("Input year, month, day: ");scanf("%d%d%d",&year,&month,&day); for(i=1;i<month;i++) { numberday+=days_of_month(year,i); } numberday+=day; printf("Days of year: %d",numberday); return 0; }
Datum months_between(PG_FUNCTION_ARGS) { DateADT date1 = PG_GETARG_DATEADT(0); DateADT date2 = PG_GETARG_DATEADT(1); int y1, m1, d1; int y2, m2, d2; float8 result; j2date(date1 + POSTGRES_EPOCH_JDATE, &y1, &m1, &d1); j2date(date2 + POSTGRES_EPOCH_JDATE, &y2, &m2, &d2); /* Ignore day components for last days, or based on a 31-day month. */ if (d1 == days_of_month(y1, m1) && d2 == days_of_month(y2, m2)) result = (y1 - y2) * 12 + (m1 - m2); else result = (y1 - y2) * 12 + (m1 - m2) + (d1 - d2) / 31.0; PG_RETURN_NUMERIC( DirectFunctionCall1(float8_numeric, Float8GetDatumFast(result))); }
void set_date(unsigned int d, unsigned int m, unsigned int y) { if (d > days_of_month(m, y) || m > 12 || y > 4095) return; CCR &= ~CLKEN; DOM = d; MONTH = m; YEAR = y; DOW = d_o_w(y, m, d); CCR |= CLKEN; return; }
date& date::assign(int32_t __year, int32_t __month, int32_t __day) { ufc_assert (__year >= 0 && __year <= 9999); ufc_assert (__month >= 1 && __month <= 12); ufc_assert (__day >= 1 && __day <= days_of_month(__year, __month)); unsigned short __a = static_cast<unsigned short>((14-__month)/12); unsigned short __y = static_cast<unsigned short>(__year + 4800 - __a); unsigned short __m = static_cast<unsigned short>(__month + 12*__a - 3); _date_val = __day + ((153*__m + 2)/5) + 365*__y + (__y/4) - (__y/100) + (__y/400) - 32045; _year = __year; _month = __month; _day = __day; return *this; }
long get_diff_date1_and_date2(int y1,int m1, int d1, int y2,int m2,int d2) { long count1 = days_of_year(y1 - 1) + m1 * days_of_month(m1 - 1, y1) + d1; long count2 = days_of_year(y2 - 1) + m2 * days_of_month(m2 - 1, y2) + d2; return count1 - count2; }