xref: /PHP-7.4/ext/standard/datetime.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Rasmus Lerdorf <rasmus@php.net>                             |
18    +----------------------------------------------------------------------+
19  */
20 
21 #include "php.h"
22 #include "zend_operators.h"
23 #include "datetime.h"
24 #include "php_globals.h"
25 
26 #include <time.h>
27 #ifdef HAVE_SYS_TIME_H
28 # include <sys/time.h>
29 #endif
30 #include <stdio.h>
31 
32 static const char * const mon_short_names[] = {
33 	"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
34 };
35 
36 static const char * const day_short_names[] = {
37 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
38 };
39 
40 /* {{{ PHPAPI char *php_std_date(time_t t)
41    Return date string in standard format for http headers */
php_std_date(time_t t)42 PHPAPI char *php_std_date(time_t t)
43 {
44 	struct tm *tm1, tmbuf;
45 	char *str;
46 
47 	tm1 = php_gmtime_r(&t, &tmbuf);
48 	str = emalloc(81);
49 	str[0] = '\0';
50 
51 	if (!tm1) {
52 		return str;
53 	}
54 
55 	snprintf(str, 80, "%s, %02d %s %04d %02d:%02d:%02d GMT",
56 			day_short_names[tm1->tm_wday],
57 			tm1->tm_mday,
58 			mon_short_names[tm1->tm_mon],
59 			tm1->tm_year + 1900,
60 			tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
61 
62 	str[79] = 0;
63 	return (str);
64 }
65 /* }}} */
66 
67 #if HAVE_STRPTIME
68 #ifndef HAVE_STRPTIME_DECL_FAILS
69 char *strptime(const char *s, const char *format, struct tm *tm);
70 #endif
71 
72 /* {{{ proto string strptime(string timestamp, string format)
73    Parse a time/date generated with strftime() */
PHP_FUNCTION(strptime)74 PHP_FUNCTION(strptime)
75 {
76 	char      *ts;
77 	size_t        ts_length;
78 	char      *format;
79 	size_t        format_length;
80 	struct tm  parsed_time;
81 	char      *unparsed_part;
82 
83 	ZEND_PARSE_PARAMETERS_START(2, 2)
84 		Z_PARAM_STRING(ts, ts_length)
85 		Z_PARAM_STRING(format, format_length)
86 	ZEND_PARSE_PARAMETERS_END();
87 
88 	memset(&parsed_time, 0, sizeof(parsed_time));
89 
90 	unparsed_part = strptime(ts, format, &parsed_time);
91 	if (unparsed_part == NULL) {
92 		RETURN_FALSE;
93 	}
94 
95 	array_init(return_value);
96 	add_assoc_long(return_value, "tm_sec",   parsed_time.tm_sec);
97 	add_assoc_long(return_value, "tm_min",   parsed_time.tm_min);
98 	add_assoc_long(return_value, "tm_hour",  parsed_time.tm_hour);
99 	add_assoc_long(return_value, "tm_mday",  parsed_time.tm_mday);
100 	add_assoc_long(return_value, "tm_mon",   parsed_time.tm_mon);
101 	add_assoc_long(return_value, "tm_year",  parsed_time.tm_year);
102 	add_assoc_long(return_value, "tm_wday",  parsed_time.tm_wday);
103 	add_assoc_long(return_value, "tm_yday",  parsed_time.tm_yday);
104 	add_assoc_string(return_value, "unparsed", unparsed_part);
105 }
106 /* }}} */
107 
108 #endif
109