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-08-04 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      173479 : timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
      25             : {
      26             :         timelib_rel_time *rt;
      27             :         timelib_time *swp;
      28      173479 :         timelib_sll dst_corr = 0 ,dst_h_corr = 0, dst_m_corr = 0;
      29             :         timelib_time one_backup, two_backup;
      30             : 
      31      173479 :         rt = timelib_rel_time_ctor();
      32      173479 :         rt->invert = 0;
      33      173479 :         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      692932 :         if (one->zone_type == 3 && two->zone_type == 3
      43      346340 :                 && (strcmp(one->tz_info->name, two->tz_info->name) == 0)
      44      173113 :                 && (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      173479 :         memcpy(&one_backup, one, sizeof(one_backup));
      53      173479 :         memcpy(&two_backup, two, sizeof(two_backup));
      54             : 
      55      173479 :     timelib_apply_localtime(one, 0);
      56      173479 :     timelib_apply_localtime(two, 0);
      57             : 
      58      173479 :         rt->y = two->y - one->y;
      59      173479 :         rt->m = two->m - one->m;
      60      173479 :         rt->d = two->d - one->d;
      61      173479 :         rt->h = two->h - one->h;
      62      173479 :         rt->i = two->i - one->i;
      63      173479 :         rt->s = two->s - one->s;
      64      173479 :         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      173479 :         rt->days = abs(floor((one->sse - two->sse - (dst_h_corr * 3600) - (dst_m_corr * 60)) / 86400));
      70             : 
      71      173479 :         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      173479 :         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      173479 :         memcpy(one, &one_backup, sizeof(one_backup));
      86      173479 :         memcpy(two, &two_backup, sizeof(two_backup));
      87             : 
      88      173479 :         return rt;
      89             : }
      90             : 
      91      167470 : timelib_time *timelib_add(timelib_time *old_time, timelib_rel_time *interval)
      92             : {
      93      167470 :         int bias = 1;
      94      167470 :         timelib_time *t = timelib_time_clone(old_time);
      95             : 
      96      167472 :         if (interval->have_weekday_relative || interval->have_special_relative) {
      97           2 :                 memcpy(&t->relative, interval, sizeof(struct timelib_rel_time));
      98             :         } else {
      99      167468 :                 if (interval->invert) {
     100         133 :                         bias = -1;
     101             :                 }
     102      167468 :                 memset(&t->relative, 0, sizeof(struct timelib_rel_time));
     103      167468 :                 t->relative.y = interval->y * bias;
     104      167468 :                 t->relative.m = interval->m * bias;
     105      167468 :                 t->relative.d = interval->d * bias;
     106      167468 :                 t->relative.h = interval->h * bias;
     107      167468 :                 t->relative.i = interval->i * bias;
     108      167468 :                 t->relative.s = interval->s * bias;
     109             :         }
     110      167470 :         t->have_relative = 1;
     111      167470 :         t->sse_uptodate = 0;
     112             : 
     113      167470 :         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      167470 :         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      167470 :         timelib_update_from_sse(t);
     123      167470 :         t->have_relative = 0;
     124             : 
     125      167470 :         return t;
     126             : }
     127             : 
     128        7507 : timelib_time *timelib_sub(timelib_time *old_time, timelib_rel_time *interval)
     129             : {
     130        7507 :         int bias = 1;
     131        7507 :         timelib_time *t = timelib_time_clone(old_time);
     132             : 
     133        7507 :         if (interval->invert) {
     134         133 :                 bias = -1;
     135             :         }
     136             : 
     137        7507 :         memset(&t->relative, 0, sizeof(struct timelib_rel_time));
     138        7507 :         t->relative.y = 0 - (interval->y * bias);
     139        7507 :         t->relative.m = 0 - (interval->m * bias);
     140        7507 :         t->relative.d = 0 - (interval->d * bias);
     141        7507 :         t->relative.h = 0 - (interval->h * bias);
     142        7507 :         t->relative.i = 0 - (interval->i * bias);
     143        7507 :         t->relative.s = 0 - (interval->s * bias);
     144        7507 :         t->have_relative = 1;
     145        7507 :         t->sse_uptodate = 0;
     146             : 
     147        7507 :         timelib_update_ts(t, NULL);
     148             : 
     149             :         /* Adjust for backwards DST changeover */
     150        7507 :         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        7507 :         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        7507 :         timelib_update_from_sse(t);
     161             : 
     162        7507 :         t->have_relative = 0;
     163             : 
     164        7507 :         return t;
     165             : }

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:02 +0000 (26 days ago)

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