Back to index

php5  5.3.10
dow.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2010 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: dow.c 293036 2010-01-03 09:23:27Z sebastian $ */
00020 
00021 #include "timelib.h"
00022 
00023 static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
00024 static int m_table_leap[13] =   { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
00025 
00026 static timelib_sll century_value(timelib_sll j)
00027 {
00028        timelib_sll i = j - 17;
00029        timelib_sll c = (4 - i * 2 + (i + 1) / 4) % 7;
00030 
00031        return c < 0 ? c + 7 : c;
00032 }
00033 
00034 static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_sll d, int iso)
00035 {
00036        timelib_sll c1, y1, m1, dow;
00037 
00038        /* Only valid for Gregorian calendar, commented out as we don't handle
00039         * julian calendar. We just return the 'wrong' day of week to be
00040         * consistent.
00041        if (y < 1753) {
00042               return -1;
00043        } */
00044        c1 = century_value(y / 100);
00045        y1 = (y % 100);
00046        m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
00047        dow = (c1 + y1 + m1 + (y1 / 4) + d) % 7;
00048        if (iso) {
00049               if (dow == 0) {
00050                      dow = 7;
00051               }
00052        }
00053        return dow;
00054 }
00055 
00056 timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
00057 {
00058        return timelib_day_of_week_ex(y, m, d, 0);
00059 }
00060 
00061 timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
00062 {
00063        return timelib_day_of_week_ex(y, m, d, 1);
00064 }
00065 
00066                                 /*     jan  feb  mar  apr  may  jun  jul  aug  sep  oct  nov  dec */
00067 static int d_table_common[13]  = {  0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
00068 static int d_table_leap[13]    = {  0,   0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335 };
00069 static int ml_table_common[13] = {  0,  31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
00070 static int ml_table_leap[13]   = {  0,  31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
00071 
00072 timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d)
00073 {
00074        return (timelib_is_leap(y) ? d_table_leap[m] : d_table_common[m]) + d - 1;
00075 }
00076 
00077 timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m)
00078 {
00079        return timelib_is_leap(y) ? ml_table_leap[m] : ml_table_common[m];
00080 }
00081 
00082 void timelib_isoweek_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iw, timelib_sll *iy)
00083 {
00084        int y_leap, prev_y_leap, doy, jan1weekday, weekday;
00085 
00086        y_leap = timelib_is_leap(y);
00087        prev_y_leap = timelib_is_leap(y-1);
00088        doy = timelib_day_of_year(y, m, d) + 1;
00089        if (y_leap && m > 2) {
00090               doy++;
00091        }
00092        jan1weekday = timelib_day_of_week(y, 1, 1);
00093        weekday = timelib_day_of_week(y, m, d);
00094        if (weekday == 0) weekday = 7;
00095        if (jan1weekday == 0) jan1weekday = 7;
00096        /* Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
00097        if (doy <= (8 - jan1weekday) && jan1weekday > 4) {
00098               *iy = y - 1;
00099               if (jan1weekday == 5 || (jan1weekday == 6 && prev_y_leap)) {
00100                      *iw = 53;
00101               } else {
00102                      *iw = 52;
00103               }
00104        } else {
00105               *iy = y;
00106        }
00107        /* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
00108        if (*iy == y) {
00109               int i;
00110 
00111               i = y_leap ? 366 : 365;
00112               if ((i - (doy - y_leap)) < (4 - weekday)) {
00113                      *iy = y + 1;
00114                      *iw = 1;
00115                      return;
00116               }
00117        }
00118        /* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
00119        if (*iy == y) {
00120               int j;
00121 
00122               j = doy + (7 - weekday) + (jan1weekday - 1);
00123               *iw = j / 7;
00124               if (jan1weekday > 4) {
00125                      *iw -= 1;
00126               }
00127        }
00128 }
00129 
00130 timelib_sll timelib_daynr_from_weeknr(timelib_sll y, timelib_sll w, timelib_sll d)
00131 {
00132        timelib_sll dow, day;
00133        
00134        /* Figure out the dayofweek for y-1-1 */
00135        dow = timelib_day_of_week(y, 1, 1);
00136        /* then use that to figure out the offset for day 1 of week 1 */
00137        day = 0 - (dow > 4 ? dow - 7 : dow);
00138 
00139        /* Add weeks and days */
00140        return day + ((w - 1) * 7) + d;
00141 }
00142 
00143 int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s)
00144 {
00145        if (h < 0 || h > 23 || i < 0 || i > 59 || s < 0 || s > 59) {
00146               return 0;
00147        }
00148        return 1;
00149 }
00150 
00151 int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d)
00152 {
00153        if (m < 1 || m > 12 || d < 1 || d > timelib_days_in_month(y, m)) {
00154               return 0;
00155        }
00156        return 1;
00157 }
00158 #if 0
00159 int main(void)
00160 {
00161        printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */
00162        printf("dow = %d\n", timelib_day_of_week(2005,  2, 19)); /* 6 */
00163 }
00164 #endif