/src/php-src/ext/standard/datetime.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: Andi Gutmans <andi@php.net> | |
12 | | | Zeev Suraski <zeev@php.net> | |
13 | | | Rasmus Lerdorf <rasmus@php.net> | |
14 | | +----------------------------------------------------------------------+ |
15 | | */ |
16 | | |
17 | | #include "php.h" |
18 | | #include "php_globals.h" |
19 | | |
20 | | #include <time.h> |
21 | | #ifdef HAVE_SYS_TIME_H |
22 | | # include <sys/time.h> |
23 | | #endif |
24 | | |
25 | | #ifdef HAVE_STRPTIME |
26 | | #ifndef HAVE_DECL_STRPTIME |
27 | | char *strptime(const char *s, const char *format, struct tm *tm); |
28 | | #endif |
29 | | |
30 | | /* {{{ Parse a time/date generated with strftime() */ |
31 | | PHP_FUNCTION(strptime) |
32 | 0 | { |
33 | 0 | char *ts; |
34 | 0 | size_t ts_length; |
35 | 0 | char *format; |
36 | 0 | size_t format_length; |
37 | 0 | struct tm parsed_time; |
38 | 0 | char *unparsed_part; |
39 | |
|
40 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
41 | 0 | Z_PARAM_STRING(ts, ts_length) |
42 | 0 | Z_PARAM_STRING(format, format_length) |
43 | 0 | ZEND_PARSE_PARAMETERS_END(); |
44 | | |
45 | 0 | memset(&parsed_time, 0, sizeof(parsed_time)); |
46 | |
|
47 | 0 | unparsed_part = strptime(ts, format, &parsed_time); |
48 | 0 | if (unparsed_part == NULL) { |
49 | 0 | RETURN_FALSE; |
50 | 0 | } |
51 | | |
52 | 0 | array_init(return_value); |
53 | 0 | add_assoc_long(return_value, "tm_sec", parsed_time.tm_sec); |
54 | 0 | add_assoc_long(return_value, "tm_min", parsed_time.tm_min); |
55 | 0 | add_assoc_long(return_value, "tm_hour", parsed_time.tm_hour); |
56 | 0 | add_assoc_long(return_value, "tm_mday", parsed_time.tm_mday); |
57 | 0 | add_assoc_long(return_value, "tm_mon", parsed_time.tm_mon); |
58 | 0 | add_assoc_long(return_value, "tm_year", parsed_time.tm_year); |
59 | 0 | add_assoc_long(return_value, "tm_wday", parsed_time.tm_wday); |
60 | 0 | add_assoc_long(return_value, "tm_yday", parsed_time.tm_yday); |
61 | 0 | add_assoc_string(return_value, "unparsed", unparsed_part); |
62 | 0 | } |
63 | | /* }}} */ |
64 | | |
65 | | #endif |