Annotation of embedaddon/php/ext/date/lib/interval.c, revision 1.1.1.4

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>