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

Generated by: LCOV version 1.10

Generated at Thu, 30 Oct 2014 07:41:28 +0000 (39 hours ago)

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