/src/php-src/ext/date/php_time.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Derick Rethans <derick@derickrethans.nl> | |
12 | | | Tim Düsterhus <timwolla@php.net> | |
13 | | +----------------------------------------------------------------------+ |
14 | | */ |
15 | | |
16 | | #include "php.h" |
17 | | #include "Zend/zend_exceptions.h" |
18 | | |
19 | | #include "php_time.h" |
20 | | #include "time_arginfo.h" |
21 | | |
22 | | zend_class_entry *php_date_ce_time_duration; |
23 | | zend_class_entry *php_date_ce_time_timeexception; |
24 | | |
25 | | static zend_object_handlers time_duration_object_handlers; |
26 | | |
27 | | static zend_object *time_duration_object_create(zend_class_entry *ce) |
28 | 31 | { |
29 | 31 | php_date_time_duration *obj = zend_object_alloc(sizeof(*obj), ce); |
30 | | |
31 | 31 | zend_object_std_init(&obj->std, ce); |
32 | 31 | object_properties_init(&obj->std, ce); |
33 | | |
34 | 31 | timelib_duration_ctor_static(&obj->duration, /* seconds */ 0, /* nanoseconds */ 0, /* negative */ false); |
35 | | |
36 | 31 | return &obj->std; |
37 | 31 | } |
38 | | |
39 | | static zend_object *time_duration_object_clone(zend_object *object) |
40 | 0 | { |
41 | 0 | const php_date_time_duration *obj = php_date_time_duration_from_obj(object); |
42 | |
|
43 | 0 | php_date_time_duration *new_obj = php_date_time_duration_from_obj(object->ce->create_object(object->ce)); |
44 | |
|
45 | 0 | new_obj->duration = obj->duration; |
46 | 0 | zend_objects_clone_members(&new_obj->std, &obj->std); |
47 | |
|
48 | 0 | return &new_obj->std; |
49 | 0 | } |
50 | | |
51 | | static int time_duration_object_compare(zval *a, zval *b) |
52 | 0 | { |
53 | 0 | ZEND_COMPARE_OBJECTS_FALLBACK(a, b); |
54 | |
|
55 | 0 | return timelib_duration_compare(&Z_DATE_TIME_DURATION_P(a)->duration, &Z_DATE_TIME_DURATION_P(b)->duration); |
56 | 0 | } |
57 | | |
58 | | PHP_MINIT_FUNCTION(date_time) |
59 | 16 | { |
60 | | /* Time\TimeException */ |
61 | 16 | php_date_ce_time_timeexception = register_class_Time_TimeException(zend_ce_exception); |
62 | | |
63 | | /* Time\Duration */ |
64 | 16 | memcpy(&time_duration_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); |
65 | 16 | time_duration_object_handlers.offset = offsetof(php_date_time_duration, std); |
66 | 16 | time_duration_object_handlers.clone_obj = time_duration_object_clone; |
67 | 16 | time_duration_object_handlers.compare = time_duration_object_compare; |
68 | 16 | php_date_ce_time_duration = register_class_Time_Duration(); |
69 | 16 | php_date_ce_time_duration->create_object = time_duration_object_create; |
70 | 16 | php_date_ce_time_duration->default_object_handlers = &time_duration_object_handlers; |
71 | | |
72 | 16 | return SUCCESS; |
73 | 16 | } |