PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/date/lib - dow.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 60 60 100.0 %
Date: 2014-07-27 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include "timelib.h"
      22             : 
      23             : static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
      24             : static int m_table_leap[13] =   { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
      25             : 
      26       21174 : static timelib_sll century_value(timelib_sll j)
      27             : {
      28       21174 :         timelib_sll i = j - 17;
      29       21174 :         timelib_sll c = (4 - i * 2 + (i + 1) / 4) % 7;
      30             : 
      31       21174 :         return c < 0 ? c + 7 : c;
      32             : }
      33             : 
      34       21174 : static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_sll d, int iso)
      35             : {
      36             :         timelib_sll c1, y1, m1, dow;
      37             : 
      38             :         /* Only valid for Gregorian calendar, commented out as we don't handle
      39             :          * julian calendar. We just return the 'wrong' day of week to be
      40             :          * consistent.
      41             :         if (y < 1753) {
      42             :                 return -1;
      43             :         } */
      44       21174 :         c1 = century_value(y / 100);
      45       21174 :         y1 = (y % 100);
      46       21174 :         m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
      47       21174 :         dow = (c1 + y1 + m1 + (y1 / 4) + d) % 7;
      48       21174 :         if (iso) {
      49           8 :                 if (dow == 0) {
      50           3 :                         dow = 7;
      51             :                 }
      52             :         }
      53       21174 :         return dow;
      54             : }
      55             : 
      56       21166 : timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
      57             : {
      58       21166 :         return timelib_day_of_week_ex(y, m, d, 0);
      59             : }
      60             : 
      61           8 : timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
      62             : {
      63           8 :         return timelib_day_of_week_ex(y, m, d, 1);
      64             : }
      65             : 
      66             :                                 /*     jan  feb  mar  apr  may  jun  jul  aug  sep  oct  nov  dec */
      67             : static int d_table_common[13]  = {  0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
      68             : static int d_table_leap[13]    = {  0,   0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335 };
      69             : static int ml_table_common[13] = {  0,  31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
      70             : static int ml_table_leap[13]   = {  0,  31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
      71             : 
      72        9832 : timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d)
      73             : {
      74        9832 :         return (timelib_is_leap(y) ? d_table_leap[m] : d_table_common[m]) + d - 1;
      75             : }
      76             : 
      77        6346 : timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m)
      78             : {
      79        6346 :         return timelib_is_leap(y) ? ml_table_leap[m] : ml_table_common[m];
      80             : }
      81             : 
      82        9436 : void timelib_isoweek_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iw, timelib_sll *iy)
      83             : {
      84             :         int y_leap, prev_y_leap, doy, jan1weekday, weekday;
      85             : 
      86        9436 :         y_leap = timelib_is_leap(y);
      87        9436 :         prev_y_leap = timelib_is_leap(y-1);
      88        9436 :         doy = timelib_day_of_year(y, m, d) + 1;
      89        9436 :         if (y_leap && m > 2) {
      90         790 :                 doy++;
      91             :         }
      92        9436 :         jan1weekday = timelib_day_of_week(y, 1, 1);
      93        9436 :         weekday = timelib_day_of_week(y, m, d);
      94        9436 :         if (weekday == 0) weekday = 7;
      95        9436 :         if (jan1weekday == 0) jan1weekday = 7;
      96             :         /* Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
      97        9618 :         if (doy <= (8 - jan1weekday) && jan1weekday > 4) {
      98         182 :                 *iy = y - 1;
      99         322 :                 if (jan1weekday == 5 || (jan1weekday == 6 && prev_y_leap)) {
     100         140 :                         *iw = 53;
     101             :                 } else {
     102          42 :                         *iw = 52;
     103             :                 }
     104             :         } else {
     105        9254 :                 *iy = y;
     106             :         }
     107             :         /* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
     108        9436 :         if (*iy == y) {
     109             :                 int i;
     110             : 
     111        9254 :                 i = y_leap ? 366 : 365;
     112        9254 :                 if ((i - (doy - y_leap)) < (4 - weekday)) {
     113          63 :                         *iy = y + 1;
     114          63 :                         *iw = 1;
     115          63 :                         return;
     116             :                 }
     117             :         }
     118             :         /* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
     119        9373 :         if (*iy == y) {
     120             :                 int j;
     121             : 
     122        9191 :                 j = doy + (7 - weekday) + (jan1weekday - 1);
     123        9191 :                 *iw = j / 7;
     124        9191 :                 if (jan1weekday > 4) {
     125        4986 :                         *iw -= 1;
     126             :                 }
     127             :         }
     128             : }
     129             : 
     130         107 : timelib_sll timelib_daynr_from_weeknr(timelib_sll y, timelib_sll w, timelib_sll d)
     131             : {
     132             :         timelib_sll dow, day;
     133             :         
     134             :         /* Figure out the dayofweek for y-1-1 */
     135         107 :         dow = timelib_day_of_week(y, 1, 1);
     136             :         /* then use that to figure out the offset for day 1 of week 1 */
     137         107 :         day = 0 - (dow > 4 ? dow - 7 : dow);
     138             : 
     139             :         /* Add weeks and days */
     140         107 :         return day + ((w - 1) * 7) + d;
     141             : }
     142             : 
     143        4468 : int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s)
     144             : {
     145        4468 :         if (h < 0 || h > 23 || i < 0 || i > 59 || s < 0 || s > 59) {
     146           4 :                 return 0;
     147             :         }
     148        4464 :         return 1;
     149             : }
     150             : 
     151        6349 : int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d)
     152             : {
     153        6349 :         if (m < 1 || m > 12 || d < 1 || d > timelib_days_in_month(y, m)) {
     154          32 :                 return 0;
     155             :         }
     156        6317 :         return 1;
     157             : }
     158             : #if 0
     159             : int main(void)
     160             : {
     161             :         printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */
     162             :         printf("dow = %d\n", timelib_day_of_week(2005,  2, 19)); /* 6 */
     163             : }
     164             : #endif

Generated by: LCOV version 1.10

Generated at Sun, 27 Jul 2014 12:58:27 +0000 (41 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.