cvs: pecl /date t_date.c

From: Date: Thu, 08 Jan 2004 15:38:13 +0000
Subject: cvs: pecl /date t_date.c
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-691@lists.php.net to get a copy of this message
pajoye Thu Jan 8 10:38:13 2004 EDT Modified files: /pecl/date t_date.c Log: - CS

Index: pecl/date/t_date.c diff -u pecl/date/t_date.c:1.2 pecl/date/t_date.c:1.3 --- pecl/date/t_date.c:1.2 Wed Jan 7 19:13:06 2004 +++ pecl/date/t_date.c Thu Jan 8 10:38:12 2004 @@ -15,7 +15,7 @@ | Author: Pierre-Alain Joye <paj@pearfr.org | +----------------------------------------------------------------------+ - $Id: t_date.c,v 1.2 2004/01/08 00:13:06 fmk Exp $ + $Id: t_date.c,v 1.3 2004/01/08 15:38:12 pajoye Exp $ */ #ifdef PHP_WIN32 @@ -40,7 +40,7 @@ Date *d; d = emalloc(sizeof(Date)); d->weekstart = weekstart; - d->dmy=d->julian=DATE_FALSE; + d->dmy = d->julian=DATE_FALSE; d->status = DATE_TRUE; return d; } @@ -60,14 +60,14 @@ Date *date_new_julian(date_uint julian, DateDay weekstart TSRMLS_DC) { Date *d; - if(!date_valid_julian(julian)){ + if (!date_valid_julian(julian)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid julian day"); return NULL; } d = date_new(weekstart); - if(d){ - d->julian = DATE_TRUE; - d->dmy = DATE_FALSE; + if (d) { + d->julian = DATE_TRUE; + d->dmy = DATE_FALSE; d->julian_days = julian; return d; } @@ -79,7 +79,7 @@ Date *newd; date_return_val_if_failed(date_valid_dmy(d,m,y),NULL); newd = date_new(weekstart); - if(newd){ + if (newd) { newd->julian = DATE_FALSE; newd->dmy = DATE_TRUE; newd->day = d; @@ -104,7 +104,7 @@ } newd = date_new(weekstart); - if(newd){ + if (newd) { newd->day = ta->tm_mday; newd->month = ta->tm_mon+1; newd->year = ta->tm_year+1900; @@ -122,32 +122,26 @@ DateBoolean date_set_weekstart(Date *d, int ws) { - d->weekstart = ws?DATE_STARTWEEK_MONDAY:DATE_STARTWEEK_SUNDAY; + d->weekstart = ws ? DATE_STARTWEEK_MONDAY : DATE_STARTWEEK_SUNDAY; return d->weekstart; } -/* -DateBoolean date_set_weekstart_default(int ws) -{ - date_weekstart = ws?DATE_STARTWEEK_MONDAY:DATE_STARTWEEK_SUNDAY; - return date_weekstart; -} -*/ + /* * validation */ -DateBoolean date_valid (const Date *d) +DateBoolean date_valid(const Date *d) { date_return_val_if_failed(d != NULL, DATE_FALSE); return d->status && (d->julian || d->dmy); } -DateBoolean date_valid_julian (int j) +DateBoolean date_valid_julian(int j) { - return (j >= DATE_JULIAN_MIN && j <= DATE_JULIAN_MAX ); + return (j >= DATE_JULIAN_MIN && j <= DATE_JULIAN_MAX ); } -DateBoolean date_valid_dmy (DateDay d, DateMonth m, DateYear y) +DateBoolean date_valid_dmy(DateDay d, DateMonth m, DateYear y) { /* ??? DATE_BAD_YEAR==0, * y>DATE_BAD_YEAR is true even if y< @@ -183,7 +177,7 @@ DateBoolean date_is_leap_year(DateYear year) { - date_return_val_if_failed (date_valid_year (year), DATE_FALSE); + date_return_val_if_failed(date_valid_year(year), DATE_FALSE); return ( (year % 400) == 0 || (((year % 4) == 0) && ((year % 100) != 0)) @@ -201,10 +195,10 @@ DateYear year; int index; - date_return_if_failed (d != NULL); - date_return_if_failed (d->dmy); - date_return_if_failed (!d->julian); - date_return_if_failed (date_valid_dmy (d->day, d->month, d->year)); + date_return_if_failed(d != NULL); + date_return_if_failed(d->dmy); + date_return_if_failed(!d->julian); + date_return_if_failed(date_valid_dmy(d->day, d->month, d->year)); /* What we actually do is: multiply years * 365 days in the year, * add the number of years divided by 4, subtract the number of @@ -221,11 +215,11 @@ d->julian_days -= (year /= 25); /* divides original # years by 100 */ d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */ - index = date_is_leap_year (d->year) ? 1 : 0; + index = date_is_leap_year(d->year) ? 1 : 0; d->julian_days += days_in_year[index][d->month] + d->day; - date_return_if_failed (date_valid_julian (d->julian_days)); + date_return_if_failed(date_valid_julian (d->julian_days)); d->julian = DATE_TRUE; } @@ -239,10 +233,10 @@ date_uint A, B, C, D, E, M; - date_return_if_failed (d != NULL); - date_return_if_failed (d->julian); - date_return_if_failed (!d->dmy); - date_return_if_failed (date_valid_julian (d->julian_days)); + date_return_if_failed(d != NULL); + date_return_if_failed(d->julian); + date_return_if_failed(!d->dmy); + date_return_if_failed(date_valid_julian (d->julian_days)); /* * Formula taken from the formula was for the @@ -273,11 +267,11 @@ * Set and get based functions */ -DateBoolean date_set_dmy (Date *d, DateDay day, DateMonth m, DateYear y) +DateBoolean date_set_dmy(Date *d, DateDay day, DateMonth m, DateYear y) { - date_return_val_if_failed (d != NULL,DATE_FALSE); + date_return_val_if_failed(d != NULL,DATE_FALSE); - if(!date_valid_dmy(day, m, y)){ + if (!date_valid_dmy(day, m, y)) { d->status = DATE_FALSE; return DATE_FALSE; } @@ -294,9 +288,9 @@ DateBoolean date_set_julian(Date *d, int j) { - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); - if(!date_valid_julian(j)){ + if (!date_valid_julian(j)) { d->status = DATE_FALSE; return DATE_FALSE; } @@ -309,7 +303,7 @@ DateBoolean date_set_day(Date *d, DateDay day) { - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); if (!date_valid_day(day)) { d->day = -1; @@ -320,7 +314,7 @@ d->julian = DATE_FALSE; d->day = day; - if (date_valid_dmy (d->day, d->month, d->year)) { + if (date_valid_dmy(d->day, d->month, d->year)) { d->dmy = DATE_TRUE; d->status = DATE_TRUE; } else { @@ -332,17 +326,17 @@ DateBoolean date_set_month(Date *d, DateMonth month) { - date_return_val_if_failed (d != NULL, DATE_FALSE); - date_return_val_if_failed (date_valid_month (month),DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); + date_return_val_if_failed(date_valid_month (month),DATE_FALSE); - if (d->julian && !d->dmy){ + if (d->julian && !d->dmy) { date_update_dmy(d); } d->julian = DATE_FALSE; d->month = month; - if (date_valid_dmy (d->day, d->month, d->year)){ + if (date_valid_dmy(d->day, d->month, d->year)) { d->dmy = DATE_TRUE; } else { d->status = DATE_FALSE; @@ -353,8 +347,8 @@ DateBoolean date_set_year(Date *d, DateYear year) { - date_return_val_if_failed (d != NULL,DATE_FALSE); - date_return_val_if_failed (date_valid_year (year),DATE_FALSE); + date_return_val_if_failed(d != NULL,DATE_FALSE); + date_return_val_if_failed(date_valid_year (year),DATE_FALSE); if (d->julian && !d->dmy) { date_update_dmy(d); @@ -363,7 +357,7 @@ d->year = year; - if (date_valid_dmy (d->day, d->month, d->year)) { + if (date_valid_dmy(d->day, d->month, d->year)) { d->dmy = DATE_TRUE; } else { d->status = DATE_FALSE; @@ -375,8 +369,8 @@ /* Get Date properties */ DateDay date_get_day(Date *d) { - date_return_val_if_failed (d->status>0,DATE_FALSE); - if(!d->dmy){ + date_return_val_if_failed(d->status>0,DATE_FALSE); + if (!d->dmy) { date_update_dmy(d); } return d->day; @@ -384,8 +378,8 @@ DateMonth date_get_month(Date *d) { - date_return_val_if_failed (d->status>0,DATE_FALSE); - if(!d->dmy){ + date_return_val_if_failed(d->status>0,DATE_FALSE); + if (!d->dmy) { date_update_dmy(d); } return d->month; @@ -393,26 +387,17 @@ DateYear date_get_year(Date *d) { - date_return_val_if_failed (d->status>0,DATE_FALSE); - if(!d->dmy){ + date_return_val_if_failed(d->status>0,DATE_FALSE); + if (!d->dmy) { date_update_dmy(d); } return d->year; } -/* -int date_get_century(Date *d) -{ - date_return_val_if_failed (d->status>0,DATE_FALSE); - if(!d->dmy){ - date_update_dmy(d); - } - return d->year/1000; -} -*/ + int date_get_julian(Date *d) { - date_return_val_if_failed (d->status>0,DATE_FALSE); - if(!d->julian){ + date_return_val_if_failed(d->status > 0, DATE_FALSE); + if (!d->julian) { date_update_julian(d); } return d->julian_days; @@ -423,14 +408,14 @@ DateBoolean date_add_days(Date *d, int days) { - date_return_val_if_failed (d->status>0,DATE_FALSE); - if(days<0){ + date_return_val_if_failed(d->status > 0, DATE_FALSE); + if (days < 0) { return date_sub_days(d,-days); } - if(!d->julian){ + if (!d->julian) { date_update_julian(d); } - if(days<=(DATE_JULIAN_MAX-d->julian_days)){ + if (days <= (DATE_JULIAN_MAX-d->julian_days)) { d->julian_days += days; d->dmy = DATE_FALSE; return DATE_TRUE; @@ -440,14 +425,14 @@ DateBoolean date_sub_days(Date *d, int days) { - date_return_val_if_failed (d->status>0,DATE_FALSE); - if(!d->julian){ + date_return_val_if_failed(d->status > 0, DATE_FALSE); + if (!d->julian) { date_update_julian(d); } - if(days<0){ + if (days < 0) { date_add_days(d,-days); } - date_return_val_if_failed(d->julian_days>days,DATE_FALSE); + date_return_val_if_failed(d->julian_days>days, DATE_FALSE); d->julian_days -= days; d->dmy = DATE_FALSE; return DATE_TRUE; @@ -455,13 +440,13 @@ DateBoolean date_add_weeks(Date *d, int weeks) { - date_return_val_if_failed (d->status>0,DATE_FALSE); + date_return_val_if_failed(d->status > 0, DATE_FALSE); return date_add_days(d, weeks*7); } DateBoolean date_sub_weeks(Date *d, int weeks) { - date_return_val_if_failed (d->status>0,DATE_FALSE); + date_return_val_if_failed(d->status > 0, DATE_FALSE); return date_sub_days(d, weeks*7); } @@ -470,17 +455,17 @@ date_uint years, months; int index; - date_return_val_if_failed (d->status>0,DATE_FALSE); - if(nmonths<0){ + date_return_val_if_failed(d->status > 0, DATE_FALSE); + if (nmonths < 0) { date_sub_months(d, -nmonths); } - date_return_val_if_failed (d != NULL, DATE_FALSE ); - date_return_val_if_failed (date_valid (d), DATE_FALSE ); + date_return_val_if_failed(d != NULL, DATE_FALSE ); + date_return_val_if_failed(date_valid(d), DATE_FALSE ); if (!d->dmy) { - date_update_dmy (d); + date_update_dmy(d); } - date_return_val_if_failed (d->dmy,DATE_FALSE); + date_return_val_if_failed(d->dmy, DATE_FALSE); nmonths += d->month - 1; @@ -488,13 +473,13 @@ years = nmonths/12; d->month = months + 1; - if(years<=(DateYear)DATE_YEAR_MAX-d->year) { + if (years <= (DateYear)DATE_YEAR_MAX-d->year) { d->year += years; } else { return DATE_FALSE; } - index = date_is_leap_year (d->year) ? 1 : 0; + index = date_is_leap_year(d->year) ? 1 : 0; if (d->day > days_in_months[index][d->month]) { d->day = days_in_months[index][d->month]; @@ -502,7 +487,7 @@ d->julian = DATE_FALSE; - date_return_val_if_failed ( date_valid (d), DATE_FALSE); + date_return_val_if_failed(date_valid (d), DATE_FALSE); return DATE_TRUE; } @@ -511,27 +496,27 @@ date_uint years, months; int index; - date_return_val_if_failed (d->status>0,DATE_FALSE); - if(nmonths<0){ + date_return_val_if_failed(d->status>0, DATE_FALSE); + if (nmonths < 0) { date_add_months(d, -nmonths); } - date_return_val_if_failed (d != NULL, DATE_FALSE ); - date_return_val_if_failed (date_valid (d), DATE_FALSE ); + date_return_val_if_failed(d != NULL, DATE_FALSE ); + date_return_val_if_failed(date_valid(d), DATE_FALSE ); if (!d->dmy) { date_update_dmy (d); } - date_return_val_if_failed (d->dmy, DATE_FALSE); + date_return_val_if_failed(d->dmy, DATE_FALSE); years = nmonths/12; months = nmonths%12; - date_return_val_if_failed (d->year > years, DATE_FALSE); + date_return_val_if_failed(d->year > years, DATE_FALSE); - if(years>d->year){ + if (years > d->year) { return DATE_FALSE; } - d->year -= years; + d->year -= years; if (d->month > months) { d->month -= months; @@ -541,7 +526,7 @@ d->year -= 1; } - index = date_is_leap_year (d->year) ? 1 : 0; + index = date_is_leap_year(d->year) ? 1 : 0; if (d->day > days_in_months[index][d->month]) { d->day = days_in_months[index][d->month]; @@ -549,51 +534,51 @@ d->julian = DATE_FALSE; - date_return_val_if_failed (date_valid (d),DATE_FALSE); + date_return_val_if_failed(date_valid (d),DATE_FALSE); return DATE_TRUE; } DateBoolean date_add_years(Date *d, int years) { - if(years<0){ - date_sub_years(d, -years); + if (years < 0) { + return date_sub_years(d, -years); } - date_return_val_if_failed (d != NULL, DATE_FALSE); - date_return_val_if_failed (date_valid (d), DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); + date_return_val_if_failed(date_valid(d), DATE_FALSE); if (!d->dmy) { date_update_dmy (d); } - date_return_val_if_failed (d->dmy, DATE_FALSE); + date_return_val_if_failed(d->dmy, DATE_FALSE); d->year += years; if (d->month == 2 && d->day == 29) { - if (!date_is_leap_year (d->year)) { + if (!date_is_leap_year(d->year)) { d->day = 28; } } d->julian = DATE_FALSE; - date_return_val_if_failed (date_valid (d),DATE_FALSE); + date_return_val_if_failed(date_valid(d), DATE_FALSE); return DATE_TRUE; } DateBoolean date_sub_years(Date *d, int years) { - if(years<0){ - date_add_years(d, -years); + if (years < 0) { + return date_add_years(d, -years); } - date_return_val_if_failed (d != NULL, DATE_FALSE); - date_return_val_if_failed (date_valid (d), DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); + date_return_val_if_failed(date_valid (d), DATE_FALSE); if (!d->dmy) { - date_update_dmy (d); + date_update_dmy(d); } - date_return_val_if_failed (d->dmy, DATE_FALSE); - date_return_val_if_failed (d->year > years, DATE_FALSE); + date_return_val_if_failed(d->dmy, DATE_FALSE); + date_return_val_if_failed(d->year > years, DATE_FALSE); d->year -= years; if (d->month == 2 && d->day == 29) { - if (!date_is_leap_year (d->year)) { + if (!date_is_leap_year(d->year)) { d->day = 28; } } @@ -608,10 +593,10 @@ int date_compare(Date *d1, Date *d2) { - date_return_val_if_failed (d1 != NULL, 0); - date_return_val_if_failed (d2 != NULL, 0); - date_return_val_if_failed (date_valid (d1), 0); - date_return_val_if_failed (date_valid (d2), 0); + date_return_val_if_failed(d1 != NULL, 0); + date_return_val_if_failed(d2 != NULL, 0); + date_return_val_if_failed(date_valid(d1), 0); + date_return_val_if_failed(date_valid(d2), 0); while (DATE_TRUE) { if (d1->julian && d2->julian) { if (d1->julian_days < d2->julian_days) { @@ -642,10 +627,14 @@ } } } else { - if (!d1->julian) date_update_julian (d1); - if (!d2->julian) date_update_julian (d2); - date_return_val_if_failed (d1->julian, 0); - date_return_val_if_failed (d2->julian, 0); + if (!d1->julian) { + date_update_julian(d1); + } + if (!d2->julian) { + date_update_julian(d2); + } + date_return_val_if_failed(d1->julian, 0); + date_return_val_if_failed(d2->julian, 0); } } return 0; /* warnings */ @@ -658,15 +647,15 @@ */ void date_clamp(Date *date, Date *min_date, Date *max_date) { - date_return_if_failed (date); - date_return_if_failed (date_valid (date)); + date_return_if_failed(date); + date_return_if_failed(date_valid(date)); if (min_date != NULL) { - date_return_if_failed (date_valid (min_date)); + date_return_if_failed(date_valid(min_date)); } if (max_date != NULL) { - date_return_if_failed (date_valid (max_date)); + date_return_if_failed(date_valid(max_date)); } /* @@ -697,15 +686,15 @@ * If date falls after max_date, sets date equal to max_date. * Either min_date and max_date may be NULL. All non-NULL dates must be valid. */ -void date_order (Date *d1, Date *d2) +void date_order(Date *d1, Date *d2) { Date tmp; - date_return_if_failed (d1 != NULL); - date_return_if_failed (d2 != NULL); - date_return_if_failed (date_valid (d1)); - date_return_if_failed (date_valid (d2)); + date_return_if_failed(d1 != NULL); + date_return_if_failed(d2 != NULL); + date_return_if_failed(date_valid (d1)); + date_return_if_failed(date_valid (d2)); - if (date_compare (d1, d2) > 0) { + if (date_compare(d1, d2) > 0) { tmp = *d1; *d1 = *d2; *d2 = tmp; @@ -716,44 +705,46 @@ DateBoolean date_is_first_of_month(Date *d) { - date_return_val_if_failed (d != NULL, DATE_FALSE); - date_return_val_if_failed (date_valid (d), DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); + date_return_val_if_failed(date_valid(d), DATE_FALSE); if (!d->dmy) { - date_update_dmy (d); + date_update_dmy(d); } - date_return_val_if_failed (d->dmy, DATE_FALSE); + date_return_val_if_failed(d->dmy, DATE_FALSE); - if (d->day == 1) return DATE_TRUE; - else return DATE_FALSE; + return d->day == 1 ? DATE_TRUE : DATE_FALSE; } DateBoolean date_is_last_of_month(Date *d) { int index; - date_return_val_if_failed (d != NULL, DATE_FALSE); - date_return_val_if_failed (date_valid (d), DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); + date_return_val_if_failed(date_valid (d), DATE_FALSE); if (!d->dmy) { date_update_dmy (d); } - date_return_val_if_failed (d->dmy, DATE_FALSE); + date_return_val_if_failed(d->dmy, DATE_FALSE); - index = date_is_leap_year (d->year) ? 1 : 0; + index = date_is_leap_year(d->year) ? 1 : 0; - if (d->day == days_in_months[index][d->month]) return DATE_TRUE; - else return DATE_FALSE; + if (d->day == days_in_months[index][d->month]) { + return DATE_TRUE; + } else { + return DATE_FALSE; + } } DateWeekday date_get_weekday(Date *d) { - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); if (!d->julian) { - date_update_julian (d); + date_update_julian(d); } - date_return_val_if_failed (d->julian, DATE_BAD_WEEKDAY); + date_return_val_if_failed(d->julian, DATE_BAD_WEEKDAY); return ((d->julian_days - 1) % 7) + 1; } @@ -762,45 +753,47 @@ { int index; - date_return_val_if_failed (d != NULL, 0); + date_return_val_if_failed(d != NULL, 0); if (!d->dmy) { - date_update_dmy (d); + date_update_dmy(d); } - date_return_val_if_failed (date_valid (d), 0); + date_return_val_if_failed(date_valid (d), 0); - date_return_val_if_failed (d->dmy, 0); + date_return_val_if_failed(d->dmy, 0); - index = date_is_leap_year (d->year) ? 1 : 0; + index = date_is_leap_year(d->year) ? 1 : 0; return (days_in_year[index][d->month] + d->day); } -int date_get_week_of_year (Date *d) +int date_get_week_of_year(Date *d) { DateWeekday wd; int day; Date first; - date_return_val_if_failed (d != NULL, 0); - date_return_val_if_failed (date_valid (d), 0); + date_return_val_if_failed(d != NULL, 0); + date_return_val_if_failed(date_valid (d), 0); if (!d->dmy) { date_update_dmy (d); } - date_return_val_if_failed (d->dmy, 0); + date_return_val_if_failed(d->dmy, 0); - date_clear (&first, 1); - date_set_dmy (&first, 1, 1, d->year); + date_clear(&first, 1); + date_set_dmy(&first, 1, 1, d->year); - if(d->weekstart==DATE_STARTWEEK_MONDAY){ - wd = date_get_weekday (&first) - 1; - day = date_get_day_of_year (d) - 1; + if (d->weekstart == DATE_STARTWEEK_MONDAY) { + wd = date_get_weekday(&first) - 1; + day = date_get_day_of_year(d) - 1; } else { - wd = date_get_weekday (&first); - if (wd == 7) wd = 0; - day = date_get_day_of_year (d) - 1; + wd = date_get_weekday(&first); + if (wd == 7) { + wd = 0; + } + day = date_get_day_of_year(d) - 1; } return ((DateDay)((day + wd)/7U) + (wd == 0 ? 1 : 0)); } @@ -808,25 +801,23 @@ /* 'A la' Date::Calc * Still to define which I have to use */ -int date_get_week_of_year2 (Date *d) +int date_get_week_of_year2(Date *d) { DateDay first; Date d0101; - date_return_val_if_failed (d != NULL, 0); - date_return_val_if_failed (date_valid (d), 0); + date_return_val_if_failed(d != NULL, 0); + date_return_val_if_failed(date_valid (d), 0); if (!d->dmy) { - date_update_dmy (d); + date_update_dmy(d); } - date_return_val_if_failed (d->dmy, 0); + date_return_val_if_failed(d->dmy, 0); d0101.weekstart = d->weekstart; date_set_dmy(&d0101, 1, 1, d->year); first = date_get_weekday(&d0101)-1; - return( (int) - ( (date_days_between(&d0101, d) + first) / (int)7 ) - + (first < 4) ); + return( (int)( (date_days_between(&d0101, d) + first) / (int)7 ) + (first < 4) ); } /* @@ -846,13 +837,13 @@ DateBoolean y_1_isleap; DateBoolean y_isleap; - date_return_val_if_failed (date != NULL, 0); - date_return_val_if_failed (date_valid (date), 0); + date_return_val_if_failed(date != NULL, 0); + date_return_val_if_failed(date_valid (date), 0); if (!date->dmy) { - date_update_dmy (date); + date_update_dmy(date); } - date_return_val_if_failed (date->dmy, 0); + date_return_val_if_failed(date->dmy, 0); m = date->month; y = date->year; @@ -911,27 +902,35 @@ } -int date_get_weeks_of_year (DateYear year) +int date_get_weeks_of_year(DateYear year) { Date d; int ws; - date_return_val_if_failed (date_valid_year (year), 0); + date_return_val_if_failed(date_valid_year (year), 0); ws = d.weekstart; - date_clear (&d, 1); - date_set_dmy (&d, 1, 1, year); + date_clear(&d, 1); + date_set_dmy(&d, 1, 1, year); - if (date_get_weekday (&d) == ws) return 53; + if (date_get_weekday(&d) == ws) { + return 53; + } date_set_dmy (&d, 31, 12, year); - if (date_get_weekday (&d) == ws) return 53; + if (date_get_weekday(&d) == ws) { + return 53; + } - if (date_is_leap_year (year)) { - date_set_dmy (&d, 2, 1, year); - if (date_get_weekday (&d) == ws) return 53; - date_set_dmy (&d, 30, 12, year); - if (date_get_weekday (&d) == ws) return 53; + if (date_is_leap_year(year)) { + date_set_dmy(&d, 2, 1, year); + if (date_get_weekday(&d) == ws) { + return 53; + } + date_set_dmy(&d, 30, 12, year); + if (date_get_weekday(&d) == ws) { + return 53; + } } return 52; } @@ -941,15 +940,15 @@ * Computes the number of days between two dates. * If date2 is prior to date1, the returned value is negative. Both dates must be valid. */ -DateDay date_days_between (Date *d1, Date *d2) +DateDay date_days_between(Date *d1, Date *d2) { - date_return_val_if_failed (d1 != NULL, 0); - date_return_val_if_failed (d2 != NULL, 0); + date_return_val_if_failed(d1 != NULL, 0); + date_return_val_if_failed(d2 != NULL, 0); - date_return_val_if_failed (date_valid (d1), 0); - date_return_val_if_failed (date_valid (d2), 0); + date_return_val_if_failed(date_valid (d1), 0); + date_return_val_if_failed(date_valid (d2), 0); - return (int)date_get_julian (d2) - (int)date_get_julian (d1); + return (int)date_get_julian(d2) - (int)date_get_julian(d1); } DateBoolean date_set_to_weekday(Date *d, DateWeekday weekday, int n) @@ -958,18 +957,18 @@ int diff; DateMonth m; DateDay day; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid_weekday(weekday), DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); m = date_get_month(d); day = date_get_day(d); - if(n>0){ + if (n>0) { /* count from the start of d->month */ date_set_day(d, 1); wd = date_get_weekday(d); diff = weekday - wd; - if(diff<0){ + if (diff < 0) { diff += 7; } diff += 7*(n - 1); @@ -979,32 +978,33 @@ date_set_to_lastmonthday(d); wd = date_get_weekday(d); diff = wd - weekday; - if(diff<0){ + if (diff < 0) { diff += 7; } - diff += 7*(-n-1); - date_sub_days(d,diff); + diff += 7 * (-n-1); + date_sub_days(d, diff); } - if(m!=date_get_month(d)){ + if (m != date_get_month(d)) { date_set_dmy(d, day, m, date_get_year(d)); return DATE_FALSE; } return DATE_TRUE; } + Date *date_get_to_weekday(Date *d, DateWeekday weekday, int n) { Date *newd; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid_weekday(weekday), DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); newd = date_new(d->weekstart); - date_return_val_if_failed (newd != NULL, DATE_FALSE); + date_return_val_if_failed(newd != NULL, DATE_FALSE); date_set_julian(newd, date_get_julian(d)); - if( !date_set_to_weekday(newd, weekday, n) ){ + if (!date_set_to_weekday(newd, weekday, n)) { return NULL; } return newd; @@ -1013,24 +1013,21 @@ DateBoolean date_set_to_lastmonthday(Date *d) { - return date_set_day( d, - date_is_leap_year(d->year)? - days_in_months[1][d->month]: - days_in_months[0][d->month] - ); + return date_set_day(d, date_is_leap_year(d->year) ? days_in_months[1][d->month] : days_in_months[0][d->month]); } + Date* date_get_to_lastmonthday(Date *d) { Date *newd; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); newd = date_new(d->weekstart); - date_return_val_if_failed (newd != NULL, DATE_FALSE); + date_return_val_if_failed(newd != NULL, DATE_FALSE); date_set_julian(newd, date_get_julian(d)); - if (!date_set_to_lastmonthday(newd)){ + if (!date_set_to_lastmonthday(newd)) { return NULL; } return newd; @@ -1040,32 +1037,33 @@ DateBoolean date_set_first_dow(Date *d) { DateWeekday wd; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); wd = date_get_weekday(d); - if(d->weekstart==DATE_STARTWEEK_MONDAY){ - if(wd!=DATE_MONDAY){ + if (d->weekstart == DATE_STARTWEEK_MONDAY) { + if (wd!=DATE_MONDAY) { return date_sub_days(d, wd-DATE_MONDAY); } } else { - if(wd!=DATE_SUNDAY){ + if (wd!=DATE_SUNDAY) { return date_sub_days(d, wd); } } return DATE_TRUE; } + Date* date_get_first_dow(Date *d) { Date *newd; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); newd = date_new(d->weekstart); - date_return_val_if_failed (newd != NULL, DATE_FALSE); + date_return_val_if_failed(newd != NULL, DATE_FALSE); date_set_julian(newd, date_get_julian(d)); - if (date_set_first_dow(newd) ) { + if (date_set_first_dow(newd)) { date_free(newd); return NULL; } @@ -1075,32 +1073,33 @@ DateBoolean date_set_last_dow(Date *d) { DateWeekday wd; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); wd = date_get_weekday(d); - if( d->weekstart==DATE_STARTWEEK_MONDAY) { - if(wd!=DATE_SUNDAY) { - return date_add_days(d,DATE_SUNDAY-wd); + if (d->weekstart == DATE_STARTWEEK_MONDAY) { + if (wd != DATE_SUNDAY) { + return date_add_days(d, DATE_SUNDAY - wd); } } else { - if(wd!=DATE_SATURDAY){ - return date_add_days(d,DATE_SATURDAY-wd); + if (wd != DATE_SATURDAY) { + return date_add_days(d, DATE_SATURDAY - wd); } } return DATE_TRUE; } + Date* date_get_last_dow(Date *d) { Date *newd; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); newd = date_new(d->weekstart); - date_return_val_if_failed (newd != NULL, DATE_FALSE); + date_return_val_if_failed(newd != NULL, DATE_FALSE); date_set_julian(newd, date_get_julian(d)); - if ( !date_set_last_dow(newd) ) { + if (!date_set_last_dow(newd)) { date_free(newd); return NULL; } @@ -1110,13 +1109,14 @@ DateBoolean date_settoweekdayinsameweek(Date *d, DateWeekday wd) { DateWeekday currentwd; - date_return_val_if_failed (d != NULL, DATE_FALSE); + + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); date_return_val_if_failed(date_valid_weekday(wd), DATE_FALSE); currentwd = date_get_weekday(d); - if(currentwd!=wd){ - if(wd<currentwd){ + if (currentwd != wd) { + if (wd < currentwd) { return date_sub_days(d, currentwd-wd); } else { return date_add_days(d, wd-currentwd); @@ -1124,18 +1124,19 @@ } return DATE_TRUE; } + Date* date_gettoweekdayinsameweek(Date *d, DateWeekday wd) { Date *newd; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); date_return_val_if_failed(date_valid_weekday(wd), DATE_FALSE); newd = date_new(d->weekstart); - date_return_val_if_failed (newd != NULL, DATE_FALSE); + date_return_val_if_failed(newd != NULL, DATE_FALSE); date_set_julian(newd, date_get_julian(d)); - if ( !date_settoweekdayinsameweek(newd, wd) ) { + if (!date_settoweekdayinsameweek(newd, wd)) { date_free(newd); return NULL; } @@ -1147,30 +1148,33 @@ DateWeekday currentwd; int diff; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); date_return_val_if_failed(date_valid_weekday(wd), DATE_FALSE); currentwd = date_get_weekday(d); - if(currentwd==wd){ + if (currentwd==wd) { return DATE_TRUE; } - if(wd<currentwd) { + if (wd < currentwd) { diff = 7 - (currentwd-wd); } else { diff = wd-currentwd; } - if( !date_add_days(d, diff) ){ + if (!date_add_days(d, diff)) { return DATE_FALSE; } return DATE_TRUE; } + Date* date_gettonextweekday(Date *d, DateWeekday wd) { Date *newd; + newd = date_new(d->weekstart); - date_return_val_if_failed (newd != NULL, DATE_FALSE); - if ( !date_settonextweekday(newd, wd) ) { + date_return_val_if_failed(newd != NULL, DATE_FALSE); + + if (!date_settonextweekday(newd, wd)) { date_free(newd); return NULL; } @@ -1183,30 +1187,32 @@ DateWeekday currentwd; int diff; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); date_return_val_if_failed(date_valid_weekday(wd), DATE_FALSE); currentwd = date_get_weekday(d); - if(wd==currentwd){ + if (wd == currentwd) { return DATE_TRUE; } - if(wd>currentwd){ + if (wd > currentwd) { diff = 7 - (wd-currentwd); } else { diff = currentwd - wd; } - if( !date_sub_days(d, diff) ) { + if (!date_sub_days(d, diff)) { return DATE_FALSE; } return DATE_TRUE; } + Date* date_gettoprevweekday(Date *d, DateWeekday wd) { Date *newd; + newd = date_new(d->weekstart); date_return_val_if_failed (newd != NULL, DATE_FALSE); - if ( !date_settoprevweekday(newd, wd) ) { + if (!date_settoprevweekday(newd, wd)) { date_free(newd); return NULL; } @@ -1218,7 +1224,7 @@ { int year; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); date_return_val_if_failed(date_valid_weekday(wd), DATE_FALSE); @@ -1226,13 +1232,12 @@ /* Jan 4 always lies in the 1st week of the year */ date_set_dmy(d, 4, DATE_JANUARY, year); - if (!date_settoweekdayinsameweek(d, wd) ){ + if (!date_settoweekdayinsameweek(d, wd)) { return DATE_FALSE; } else { date_add_weeks(d, week-1); } - if ( date_get_year(d)!=year ) - { + if (date_get_year(d) != year) { return DATE_FALSE; } return DATE_TRUE; @@ -1243,12 +1248,12 @@ { Date *newd; - date_return_val_if_failed (d != NULL, DATE_FALSE); + date_return_val_if_failed(d != NULL, DATE_FALSE); date_return_val_if_failed(date_valid(d), DATE_FALSE); date_return_val_if_failed(date_valid_weekday(wd), DATE_FALSE); newd = date_new_dmy(d->day,d->month,d->year,d->weekstart); - if(newd && date_settotheweek(newd, week, wd)){ + if (newd && date_settotheweek(newd, week, wd)) { return newd; } else { return NULL; @@ -1260,7 +1265,7 @@ int date_get_days_in_month(DateMonth month, DateYear year) { int index; - index = date_is_leap_year (year) ? 1 : 0; + index = date_is_leap_year(year) ? 1 : 0; return days_in_months[index][month]; } @@ -1270,10 +1275,11 @@ { int golden, solar, lunar, pfm, dom, tmp, easter; Date *date; + /* the Golden number */ golden = (year % 19) + 1; - if ( year <= 1752 ) { + if (year <= 1752) { /* JULIAN CALENDAR */ /* the "Dominical number" - finding a Sunday */ dom = (year + (year/4) + 5) % 7;
« previous php.pecl.cvs (#691) next »