xref: /php-src/ext/standard/datetime.c (revision 1a380989)
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    | https://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 */
php_std_date(time_t t)40 PHPAPI char *php_std_date(time_t t)
41 {
42 	struct tm *tm1, tmbuf;
43 	char *str;
44 
45 	tm1 = php_gmtime_r(&t, &tmbuf);
46 	str = emalloc(81);
47 	str[0] = '\0';
48 
49 	if (!tm1) {
50 		return str;
51 	}
52 
53 	snprintf(str, 80, "%s, %02d %s %04d %02d:%02d:%02d GMT",
54 			day_short_names[tm1->tm_wday],
55 			tm1->tm_mday,
56 			mon_short_names[tm1->tm_mon],
57 			tm1->tm_year + 1900,
58 			tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
59 
60 	str[79] = 0;
61 	return (str);
62 }
63 /* }}} */
64 
65 #ifdef HAVE_STRPTIME
66 #ifndef HAVE_DECL_STRPTIME
67 char *strptime(const char *s, const char *format, struct tm *tm);
68 #endif
69 
70 /* {{{ Parse a time/date generated with strftime() */
PHP_FUNCTION(strptime)71 PHP_FUNCTION(strptime)
72 {
73 	char      *ts;
74 	size_t        ts_length;
75 	char      *format;
76 	size_t        format_length;
77 	struct tm  parsed_time;
78 	char      *unparsed_part;
79 
80 	ZEND_PARSE_PARAMETERS_START(2, 2)
81 		Z_PARAM_STRING(ts, ts_length)
82 		Z_PARAM_STRING(format, format_length)
83 	ZEND_PARSE_PARAMETERS_END();
84 
85 	memset(&parsed_time, 0, sizeof(parsed_time));
86 
87 	unparsed_part = strptime(ts, format, &parsed_time);
88 	if (unparsed_part == NULL) {
89 		RETURN_FALSE;
90 	}
91 
92 	array_init(return_value);
93 	add_assoc_long(return_value, "tm_sec",   parsed_time.tm_sec);
94 	add_assoc_long(return_value, "tm_min",   parsed_time.tm_min);
95 	add_assoc_long(return_value, "tm_hour",  parsed_time.tm_hour);
96 	add_assoc_long(return_value, "tm_mday",  parsed_time.tm_mday);
97 	add_assoc_long(return_value, "tm_mon",   parsed_time.tm_mon);
98 	add_assoc_long(return_value, "tm_year",  parsed_time.tm_year);
99 	add_assoc_long(return_value, "tm_wday",  parsed_time.tm_wday);
100 	add_assoc_long(return_value, "tm_yday",  parsed_time.tm_yday);
101 	add_assoc_string(return_value, "unparsed", unparsed_part);
102 }
103 /* }}} */
104 
105 #endif
106