1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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: Andi Gutmans <andi@zend.com> |
16 : | Zeev Suraski <zeev@zend.com> |
17 : | Rasmus Lerdorf <rasmus@php.net> |
18 : +----------------------------------------------------------------------+
19 : */
20 :
21 : /* $Id: datetime.c 276986 2009-03-10 23:40:06Z helly $ */
22 :
23 : #include "php.h"
24 : #include "zend_operators.h"
25 : #include "datetime.h"
26 : #include "php_globals.h"
27 :
28 : #include <time.h>
29 : #ifdef HAVE_SYS_TIME_H
30 : # include <sys/time.h>
31 : #endif
32 : #include <stdio.h>
33 :
34 : char *mon_full_names[] = {
35 : "January", "February", "March", "April",
36 : "May", "June", "July", "August",
37 : "September", "October", "November", "December"
38 : };
39 :
40 : char *mon_short_names[] = {
41 : "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
42 : };
43 :
44 : char *day_full_names[] = {
45 : "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
46 : };
47 :
48 : char *day_short_names[] = {
49 : "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
50 : };
51 :
52 : /* {{{ PHPAPI char *php_std_date(time_t t TSRMLS_DC)
53 : Return date string in standard format for http headers */
54 : PHPAPI char *php_std_date(time_t t TSRMLS_DC)
55 0 : {
56 : struct tm *tm1, tmbuf;
57 : char *str;
58 :
59 0 : tm1 = php_gmtime_r(&t, &tmbuf);
60 0 : str = emalloc(81);
61 0 : str[0] = '\0';
62 :
63 0 : if (!tm1) {
64 0 : return str;
65 : }
66 :
67 0 : if (PG(y2k_compliance)) {
68 0 : snprintf(str, 80, "%s, %02d %s %04d %02d:%02d:%02d GMT",
69 : day_short_names[tm1->tm_wday],
70 : tm1->tm_mday,
71 : mon_short_names[tm1->tm_mon],
72 : tm1->tm_year + 1900,
73 : tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
74 : } else {
75 0 : snprintf(str, 80, "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
76 : day_full_names[tm1->tm_wday],
77 : tm1->tm_mday,
78 : mon_short_names[tm1->tm_mon],
79 : ((tm1->tm_year) % 100),
80 : tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
81 : }
82 :
83 0 : str[79] = 0;
84 0 : return (str);
85 : }
86 : /* }}} */
87 :
88 : #if HAVE_STRPTIME
89 : #ifndef HAVE_STRPTIME_DECL_FAILS
90 : char *strptime(const char *s, const char *format, struct tm *tm);
91 : #endif
92 :
93 : /* {{{ proto string strptime(string timestamp, string format) U
94 : Parse a time/date generated with strftime() */
95 : PHP_FUNCTION(strptime)
96 7 : {
97 : zstr ts;
98 : int ts_length;
99 : zstr format;
100 : int format_length;
101 : struct tm parsed_time;
102 : char *unparsed_part;
103 : zend_uchar type;
104 :
105 7 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "TT", &ts, &ts_length, &type, &format, &format_length, &type) == FAILURE) {
106 3 : return;
107 : }
108 :
109 4 : if (type == IS_UNICODE) {
110 : char *temp;
111 : int temp_len;
112 :
113 4 : if (zend_unicode_to_string(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &temp, &temp_len, ts.u, ts_length TSRMLS_CC) == FAILURE) {
114 0 : RETURN_FALSE;
115 : }
116 4 : ts.s = temp;
117 4 : ts_length = temp_len;
118 :
119 4 : if (zend_unicode_to_string(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &temp, &temp_len, format.u, format_length TSRMLS_CC) == FAILURE) {
120 0 : RETURN_FALSE;
121 : }
122 4 : format.s = temp;
123 4 : format_length = temp_len;
124 : }
125 :
126 4 : memset(&parsed_time, 0, sizeof(parsed_time));
127 :
128 4 : unparsed_part = strptime(ts.s, format.s, &parsed_time);
129 4 : if (unparsed_part == NULL) {
130 0 : RETURN_FALSE;
131 : }
132 :
133 4 : array_init(return_value);
134 4 : add_ascii_assoc_long(return_value, "tm_sec", parsed_time.tm_sec);
135 4 : add_ascii_assoc_long(return_value, "tm_min", parsed_time.tm_min);
136 4 : add_ascii_assoc_long(return_value, "tm_hour", parsed_time.tm_hour);
137 4 : add_ascii_assoc_long(return_value, "tm_mday", parsed_time.tm_mday);
138 4 : add_ascii_assoc_long(return_value, "tm_mon", parsed_time.tm_mon);
139 4 : add_ascii_assoc_long(return_value, "tm_year", parsed_time.tm_year);
140 4 : add_ascii_assoc_long(return_value, "tm_wday", parsed_time.tm_wday);
141 4 : add_ascii_assoc_long(return_value, "tm_yday", parsed_time.tm_yday);
142 4 : if (type == IS_UNICODE) {
143 : UChar *temp;
144 : int temp_len;
145 :
146 4 : zend_string_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &temp, &temp_len, unparsed_part, strlen(unparsed_part) TSRMLS_CC);
147 4 : add_ascii_assoc_unicodel(return_value, "unparsed", temp, temp_len, 0);
148 :
149 4 : efree(ts.s);
150 4 : efree(format.s);
151 : } else {
152 0 : add_ascii_assoc_string(return_value, "unparsed", unparsed_part, 1);
153 : }
154 : }
155 : /* }}} */
156 :
157 : #endif
158 :
159 : /*
160 : * Local variables:
161 : * tab-width: 4
162 : * c-basic-offset: 4
163 : * End:
164 : * vim600: sw=4 ts=4 fdm=marker
165 : * vim<600: sw=4 ts=4
166 : */
|