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 - interval.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 86 86 100.0 %
Date: 2014-10-24 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       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             : #include <math.h>
      23             : 
      24      173476 : timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
      25             : {
      26             :         timelib_rel_time *rt;
      27             :         timelib_time *swp;
      28      173476 :         timelib_sll dst_corr = 0 ,dst_h_corr = 0, dst_m_corr = 0;
      29             :         timelib_time one_backup, two_backup;
      30             : 
      31      173476 :         rt = timelib_rel_time_ctor();
      32      173476 :         rt->invert = 0;
      33      173476 :         if (one->sse > two->sse) {
      34        7414 :                 swp = two;
      35        7414 :                 two = one;
      36        7414 :                 one = swp;
      37        7414 :                 rt->invert = 1;
      38             :         }
      39             : 
      40             :         /* Calculate correction for DST change over, but only if the TZ type is ID
      41             :          * and it's the same */
      42      692920 :         if (one->zone_type == 3 && two->zone_type == 3
      43      346334 :                 && (strcmp(one->tz_info->name, two->tz_info->name) == 0)
      44      173110 :                 && (one->z != two->z))
      45             :         {
      46         114 :                 dst_corr = two->z - one->z;
      47         114 :                 dst_h_corr = dst_corr / 3600;
      48         114 :                 dst_m_corr = (dst_corr % 3600) / 60;
      49             :         }
      50             : 
      51             :         /* Save old TZ info */
      52      173476 :         memcpy(&one_backup, one, sizeof(one_backup));
      53      173476 :         memcpy(&two_backup, two, sizeof(two_backup));
      54             : 
      55      173476 :     timelib_apply_localtime(one, 0);
      56      173476 :     timelib_apply_localtime(two, 0);
      57             : 
      58      173476 :         rt->y = two->y - one->y;
      59      173476 :         rt->m = two->m - one->m;
      60      173476 :         rt->d = two->d - one->d;
      61      173476 :         rt->h = two->h - one->h;
      62      173476 :         rt->i = two->i - one->i;
      63      173476 :         rt->s = two->s - one->s;
      64      173476 :         if (one_backup.dst == 0 && two_backup.dst == 1 && two->sse >= one->sse + 86400 - dst_corr) {
      65          88 :                 rt->h += dst_h_corr;
      66          88 :                 rt->i += dst_m_corr;
      67             :         }
      68             : 
      69      173476 :         rt->days = abs(floor((one->sse - two->sse - (dst_h_corr * 3600) - (dst_m_corr * 60)) / 86400));
      70             : 
      71      173476 :         timelib_do_rel_normalize(rt->invert ? one : two, rt);
      72             : 
      73             :         /* We need to do this after normalisation otherwise we can't get "24H" */
      74      173476 :         if (one_backup.dst == 1 && two_backup.dst == 0 && two->sse >= one->sse + 86400) {
      75          63 :                 if (two->sse < one->sse + 86400 - dst_corr) {
      76           1 :                         rt->d--;
      77           1 :                         rt->h = 24;
      78             :                 } else {
      79          62 :                         rt->h += dst_h_corr;
      80          62 :                         rt->i += dst_m_corr;
      81             :                 }
      82             :         }
      83             : 
      84             :         /* Restore old TZ info */
      85      173476 :         memcpy(one, &one_backup, sizeof(one_backup));
      86      173476 :         memcpy(two, &two_backup, sizeof(two_backup));
      87             : 
      88      173476 :         return rt;
      89             : }
      90             : 
      91      167468 : timelib_time *timelib_add(timelib_time *old_time, timelib_rel_time *interval)
      92             : {
      93      167468 :         int bias = 1;
      94      167468 :         timelib_time *t = timelib_time_clone(old_time);
      95             : 
      96      167470 :         if (interval->have_weekday_relative || interval->have_special_relative) {
      97           2 :                 memcpy(&t->relative, interval, sizeof(struct timelib_rel_time));
      98             :         } else {
      99      167466 :                 if (interval->invert) {
     100         133 :                         bias = -1;
     101             :                 }
     102      167466 :                 memset(&t->relative, 0, sizeof(struct timelib_rel_time));
     103      167466 :                 t->relative.y = interval->y * bias;
     104      167466 :                 t->relative.m = interval->m * bias;
     105      167466 :                 t->relative.d = interval->d * bias;
     106      167466 :                 t->relative.h = interval->h * bias;
     107      167466 :                 t->relative.i = interval->i * bias;
     108      167466 :                 t->relative.s = interval->s * bias;
     109             :         }
     110      167468 :         t->have_relative = 1;
     111      167468 :         t->sse_uptodate = 0;
     112             : 
     113      167468 :         timelib_update_ts(t, NULL);
     114             : 
     115             : //      printf("%lld %lld %d\n", old_time->dst, t->dst, (t->sse - old_time->sse));
     116             :         /* Adjust for backwards DST changeover */
     117      167468 :         if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
     118          23 :                 t->sse -= old_time->z;
     119          23 :                 t->sse += t->z;
     120             :         }
     121             : 
     122      167468 :         timelib_update_from_sse(t);
     123      167468 :         t->have_relative = 0;
     124             : 
     125      167468 :         return t;
     126             : }
     127             : 
     128        7505 : timelib_time *timelib_sub(timelib_time *old_time, timelib_rel_time *interval)
     129             : {
     130        7505 :         int bias = 1;
     131        7505 :         timelib_time *t = timelib_time_clone(old_time);
     132             : 
     133        7505 :         if (interval->invert) {
     134         133 :                 bias = -1;
     135             :         }
     136             : 
     137        7505 :         memset(&t->relative, 0, sizeof(struct timelib_rel_time));
     138        7505 :         t->relative.y = 0 - (interval->y * bias);
     139        7505 :         t->relative.m = 0 - (interval->m * bias);
     140        7505 :         t->relative.d = 0 - (interval->d * bias);
     141        7505 :         t->relative.h = 0 - (interval->h * bias);
     142        7505 :         t->relative.i = 0 - (interval->i * bias);
     143        7505 :         t->relative.s = 0 - (interval->s * bias);
     144        7505 :         t->have_relative = 1;
     145        7505 :         t->sse_uptodate = 0;
     146             : 
     147        7505 :         timelib_update_ts(t, NULL);
     148             : 
     149             :         /* Adjust for backwards DST changeover */
     150        7505 :         if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
     151          19 :                 t->sse -= old_time->z;
     152          19 :                 t->sse += t->z;
     153             :         }
     154             :         /* Adjust for forwards DST changeover */
     155        7505 :         if (old_time->dst == 0 && t->dst == 1 && !interval->y && !interval->m && !interval->d ) {
     156          29 :                 t->sse -= old_time->z;
     157          29 :                 t->sse += t->z;
     158             :         }
     159             : 
     160        7505 :         timelib_update_from_sse(t);
     161             : 
     162        7505 :         t->have_relative = 0;
     163             : 
     164        7505 :         return t;
     165             : }

Generated by: LCOV version 1.10

Generated at Fri, 24 Oct 2014 05:21:45 +0000 (40 hours ago)

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