/src/php-src/ext/standard/datetime.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | http://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Andi Gutmans <andi@php.net> | |
14 | | | Zeev Suraski <zeev@php.net> | |
15 | | | Rasmus Lerdorf <rasmus@php.net> | |
16 | | +----------------------------------------------------------------------+ |
17 | | */ |
18 | | |
19 | | #include "php.h" |
20 | | #include "zend_operators.h" |
21 | | #include "datetime.h" |
22 | | #include "php_globals.h" |
23 | | |
24 | | #include <time.h> |
25 | | #ifdef HAVE_SYS_TIME_H |
26 | | # include <sys/time.h> |
27 | | #endif |
28 | | #include <stdio.h> |
29 | | |
30 | | static const char * const mon_short_names[] = { |
31 | | "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
32 | | }; |
33 | | |
34 | | static const char * const day_short_names[] = { |
35 | | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" |
36 | | }; |
37 | | |
38 | | /* {{{ PHPAPI char *php_std_date(time_t t) |
39 | | Return date string in standard format for http headers */ |
40 | | PHPAPI char *php_std_date(time_t t) |
41 | 0 | { |
42 | 0 | struct tm *tm1, tmbuf; |
43 | 0 | char *str; |
44 | |
|
45 | 0 | tm1 = php_gmtime_r(&t, &tmbuf); |
46 | 0 | str = emalloc(81); |
47 | 0 | str[0] = '\0'; |
48 | |
|
49 | 0 | if (!tm1) { |
50 | 0 | return str; |
51 | 0 | } |
52 | | |
53 | 0 | snprintf(str, 80, "%s, %02d %s %04d %02d:%02d:%02d GMT", |
54 | 0 | day_short_names[tm1->tm_wday], |
55 | 0 | tm1->tm_mday, |
56 | 0 | mon_short_names[tm1->tm_mon], |
57 | 0 | tm1->tm_year + 1900, |
58 | 0 | tm1->tm_hour, tm1->tm_min, tm1->tm_sec); |
59 | |
|
60 | 0 | str[79] = 0; |
61 | 0 | return (str); |
62 | 0 | } |
63 | | /* }}} */ |
64 | | |
65 | | #if HAVE_STRPTIME |
66 | | #ifndef HAVE_STRPTIME_DECL_FAILS |
67 | | char *strptime(const char *s, const char *format, struct tm *tm); |
68 | | #endif |
69 | | |
70 | | /* {{{ proto array|false strptime(string timestamp, string format) |
71 | | Parse a time/date generated with strftime() */ |
72 | | PHP_FUNCTION(strptime) |
73 | 0 | { |
74 | 0 | char *ts; |
75 | 0 | size_t ts_length; |
76 | 0 | char *format; |
77 | 0 | size_t format_length; |
78 | 0 | struct tm parsed_time; |
79 | 0 | char *unparsed_part; |
80 | |
|
81 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
82 | 0 | Z_PARAM_STRING(ts, ts_length) |
83 | 0 | Z_PARAM_STRING(format, format_length) |
84 | 0 | ZEND_PARSE_PARAMETERS_END(); |
85 | |
|
86 | 0 | memset(&parsed_time, 0, sizeof(parsed_time)); |
87 | |
|
88 | 0 | unparsed_part = strptime(ts, format, &parsed_time); |
89 | 0 | if (unparsed_part == NULL) { |
90 | 0 | RETURN_FALSE; |
91 | 0 | } |
92 | |
|
93 | 0 | array_init(return_value); |
94 | 0 | add_assoc_long(return_value, "tm_sec", parsed_time.tm_sec); |
95 | 0 | add_assoc_long(return_value, "tm_min", parsed_time.tm_min); |
96 | 0 | add_assoc_long(return_value, "tm_hour", parsed_time.tm_hour); |
97 | 0 | add_assoc_long(return_value, "tm_mday", parsed_time.tm_mday); |
98 | 0 | add_assoc_long(return_value, "tm_mon", parsed_time.tm_mon); |
99 | 0 | add_assoc_long(return_value, "tm_year", parsed_time.tm_year); |
100 | 0 | add_assoc_long(return_value, "tm_wday", parsed_time.tm_wday); |
101 | 0 | add_assoc_long(return_value, "tm_yday", parsed_time.tm_yday); |
102 | 0 | add_assoc_string(return_value, "unparsed", unparsed_part); |
103 | 0 | } |
104 | | /* }}} */ |
105 | | |
106 | | #endif |