Back to index

php5  5.3.10
interval.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2010 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: interval.c 320563 2011-12-07 08:39:43Z pajoye $ */
00020 
00021 #include "timelib.h"
00022 #include <math.h>
00023 
00024 timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
00025 {
00026        timelib_rel_time *rt;
00027        timelib_time *swp;
00028        timelib_sll dst_h_corr = 0, dst_m_corr = 0;
00029        timelib_time one_backup, two_backup;
00030 
00031        rt = timelib_rel_time_ctor();
00032        rt->invert = 0;
00033        if (one->sse > two->sse) {
00034               swp = two;
00035               two = one;
00036               one = swp;
00037               rt->invert = 1;
00038        }
00039 
00040        /* Calculate correction for DST change over, but only if the TZ type is ID
00041         * and it's the same */
00042        if (one->zone_type == 3 && two->zone_type == 3
00043               && (strcmp(one->tz_info->name, two->tz_info->name) == 0)
00044               && (one->z != two->z))
00045        {
00046               dst_h_corr = (two->z - one->z) / 3600;
00047               dst_m_corr = ((two->z - one->z) % 3600) / 60;
00048        }
00049 
00050        /* Save old TZ info */
00051        memcpy(&one_backup, one, sizeof(one_backup));
00052        memcpy(&two_backup, two, sizeof(two_backup));
00053 
00054     timelib_apply_localtime(one, 0);
00055     timelib_apply_localtime(two, 0);
00056 
00057        rt->y = two->y - one->y;
00058        rt->m = two->m - one->m;
00059        rt->d = two->d - one->d;
00060        rt->h = two->h - one->h + dst_h_corr;
00061        rt->i = two->i - one->i + dst_m_corr;
00062        rt->s = two->s - one->s;
00063        rt->days = abs(floor((one->sse - two->sse - (dst_h_corr * 3600) - (dst_m_corr * 60)) / 86400));
00064 
00065        timelib_do_rel_normalize(rt->invert ? one : two, rt);
00066 
00067        /* Restore old TZ info */
00068        memcpy(one, &one_backup, sizeof(one_backup));
00069        memcpy(two, &two_backup, sizeof(two_backup));
00070 
00071        return rt;
00072 }