1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2013 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
timelib_diff(timelib_time * one,timelib_time * two)24 timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
25 {
26 timelib_rel_time *rt;
27 timelib_time *swp;
28 timelib_sll dst_h_corr = 0, dst_m_corr = 0;
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 {
46 dst_h_corr = (two->z - one->z) / 3600;
47 dst_m_corr = ((two->z - one->z) % 3600) / 60;
48 }
49
50 /* Save old TZ info */
51 memcpy(&one_backup, one, sizeof(one_backup));
52 memcpy(&two_backup, two, sizeof(two_backup));
53
54 timelib_apply_localtime(one, 0);
55 timelib_apply_localtime(two, 0);
56
57 rt->y = two->y - one->y;
58 rt->m = two->m - one->m;
59 rt->d = two->d - one->d;
60 rt->h = two->h - one->h + dst_h_corr;
61 rt->i = two->i - one->i + dst_m_corr;
62 rt->s = two->s - one->s;
63 rt->days = abs(floor((one->sse - two->sse - (dst_h_corr * 3600) - (dst_m_corr * 60)) / 86400));
64
65 timelib_do_rel_normalize(rt->invert ? one : two, rt);
66
67 /* Restore old TZ info */
68 memcpy(one, &one_backup, sizeof(one_backup));
69 memcpy(two, &two_backup, sizeof(two_backup));
70
71 return rt;
72 }
73