php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
datetime.c
Go to the documentation of this file.
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
30static const char * const mon_short_names[] = {
31 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
32};
33
34static 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 */
40PHPAPI 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
67char *strptime(const char *s, const char *format, struct tm *tm);
68#endif
69
70/* {{{ Parse a time/date generated with strftime() */
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
81 Z_PARAM_STRING(ts, ts_length)
82 Z_PARAM_STRING(format, format_length)
84
85 memset(&parsed_time, 0, sizeof(parsed_time));
86
87 unparsed_part = strptime(ts, format, &parsed_time);
88 if (unparsed_part == NULL) {
90 }
91
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
sizeof(Countable|array $value, int $mode=COUNT_NORMAL)
strptime(string $timestamp, string $format)
char s[4]
Definition cdf.c:77
PHPAPI char * php_std_date(time_t t)
Definition datetime.c:40
memset(ptr, 0, type->size)
#define NULL
Definition gdcache.h:45
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
#define PHP_FUNCTION
Definition php.h:364
#define PHPAPI
Definition php.h:71
PHPAPI struct tm * php_gmtime_r(const time_t *const timep, struct tm *p_tm)
Definition reentrancy.c:173
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define Z_PARAM_STRING(dest, dest_len)
Definition zend_API.h:2071
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define array_init(arg)
Definition zend_API.h:537
#define emalloc(size)
Definition zend_alloc.h:151
#define snprintf
zval * return_value