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: 63 63 100.0 %
Date: 2014-12-13 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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       21057 : static timelib_sll positive_mod(timelib_sll x, timelib_sll y)
      27             : {
      28             :         timelib_sll tmp;
      29             : 
      30       21057 :         tmp = x % y;
      31       21057 :         if (tmp < 0) {
      32          22 :                 tmp += y;
      33             :         }
      34             : 
      35       21057 :         return tmp;
      36             : }
      37             : 
      38        7019 : static timelib_sll century_value(timelib_sll j)
      39             : {
      40        7019 :         return 6 - positive_mod(j, 4) * 2;
      41             : }
      42             : 
      43        7019 : static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_sll d, int iso)
      44             : {
      45             :         timelib_sll c1, y1, m1, dow;
      46             : 
      47             :         /* Only valid for Gregorian calendar, commented out as we don't handle
      48             :          * Julian calendar. We just return the 'wrong' day of week to be
      49             :          * consistent. */
      50        7019 :         c1 = century_value(y / 100);
      51        7019 :         y1 = positive_mod(y, 100);
      52        7019 :         m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
      53        7019 :         dow = positive_mod((c1 + y1 + m1 + (y1 / 4) + d), 7);
      54        7019 :         if (iso) {
      55           8 :                 if (dow == 0) {
      56           3 :                         dow = 7;
      57             :                 }
      58             :         }
      59        7019 :         return dow;
      60             : }
      61             : 
      62        7011 : timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
      63             : {
      64        7011 :         return timelib_day_of_week_ex(y, m, d, 0);
      65             : }
      66             : 
      67           8 : timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
      68             : {
      69           8 :         return timelib_day_of_week_ex(y, m, d, 1);
      70             : }
      71             : 
      72             :                                 /*     jan  feb  mar  apr  may  jun  jul  aug  sep  oct  nov  dec */
      73             : static int d_table_common[13]  = {  0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334 };
      74             : static int d_table_leap[13]    = {  0,   0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335 };
      75             : static int ml_table_common[13] = {  0,  31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
      76             : static int ml_table_leap[13]   = {  0,  31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
      77             : 
      78        2548 : timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d)
      79             : {
      80        2548 :         return (timelib_is_leap(y) ? d_table_leap[m] : d_table_common[m]) + d - 1;
      81             : }
      82             : 
      83        6655 : timelib_sll timelib_days_in_month(timelib_sll y, timelib_sll m)
      84             : {
      85        6655 :         return timelib_is_leap(y) ? ml_table_leap[m] : ml_table_common[m];
      86             : }
      87             : 
      88        2147 : void timelib_isoweek_from_date(timelib_sll y, timelib_sll m, timelib_sll d, timelib_sll *iw, timelib_sll *iy)
      89             : {
      90             :         int y_leap, prev_y_leap, doy, jan1weekday, weekday;
      91             : 
      92        2147 :         y_leap = timelib_is_leap(y);
      93        2147 :         prev_y_leap = timelib_is_leap(y-1);
      94        2147 :         doy = timelib_day_of_year(y, m, d) + 1;
      95        2147 :         if (y_leap && m > 2) {
      96         278 :                 doy++;
      97             :         }
      98        2147 :         jan1weekday = timelib_day_of_week(y, 1, 1);
      99        2147 :         weekday = timelib_day_of_week(y, m, d);
     100        2147 :         if (weekday == 0) weekday = 7;
     101        2147 :         if (jan1weekday == 0) jan1weekday = 7;
     102             :         /* Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
     103        2219 :         if (doy <= (8 - jan1weekday) && jan1weekday > 4) {
     104          72 :                 *iy = y - 1;
     105         123 :                 if (jan1weekday == 5 || (jan1weekday == 6 && prev_y_leap)) {
     106          51 :                         *iw = 53;
     107             :                 } else {
     108          21 :                         *iw = 52;
     109             :                 }
     110             :         } else {
     111        2075 :                 *iy = y;
     112             :         }
     113             :         /* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
     114        2147 :         if (*iy == y) {
     115             :                 int i;
     116             : 
     117        2075 :                 i = y_leap ? 366 : 365;
     118        2075 :                 if ((i - (doy - y_leap)) < (4 - weekday)) {
     119          48 :                         *iy = y + 1;
     120          48 :                         *iw = 1;
     121          48 :                         return;
     122             :                 }
     123             :         }
     124             :         /* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
     125        2099 :         if (*iy == y) {
     126             :                 int j;
     127             : 
     128        2027 :                 j = doy + (7 - weekday) + (jan1weekday - 1);
     129        2027 :                 *iw = j / 7;
     130        2027 :                 if (jan1weekday > 4) {
     131         829 :                         *iw -= 1;
     132             :                 }
     133             :         }
     134             : }
     135             : 
     136         109 : timelib_sll timelib_daynr_from_weeknr(timelib_sll y, timelib_sll w, timelib_sll d)
     137             : {
     138             :         timelib_sll dow, day;
     139             :         
     140             :         /* Figure out the dayofweek for y-1-1 */
     141         109 :         dow = timelib_day_of_week(y, 1, 1);
     142             :         /* then use that to figure out the offset for day 1 of week 1 */
     143         109 :         day = 0 - (dow > 4 ? dow - 7 : dow);
     144             : 
     145             :         /* Add weeks and days */
     146         109 :         return day + ((w - 1) * 7) + d;
     147             : }
     148             : 
     149        4561 : int timelib_valid_time(timelib_sll h, timelib_sll i, timelib_sll s)
     150             : {
     151        4561 :         if (h < 0 || h > 23 || i < 0 || i > 59 || s < 0 || s > 59) {
     152           4 :                 return 0;
     153             :         }
     154        4557 :         return 1;
     155             : }
     156             : 
     157        6663 : int timelib_valid_date(timelib_sll y, timelib_sll m, timelib_sll d)
     158             : {
     159        6663 :         if (m < 1 || m > 12 || d < 1 || d > timelib_days_in_month(y, m)) {
     160          37 :                 return 0;
     161             :         }
     162        6626 :         return 1;
     163             : }
     164             : #if 0
     165             : int main(void)
     166             : {
     167             :         printf("dow = %d\n", timelib_day_of_week(1978, 12, 22)); /* 5 */
     168             :         printf("dow = %d\n", timelib_day_of_week(2005,  2, 19)); /* 6 */
     169             : }
     170             : #endif

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:11 +0000 (9 days ago)

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