Back to index

php5  5.3.10
datetime.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    |          Rasmus Lerdorf <rasmus@php.net>                             |
00018    +----------------------------------------------------------------------+
00019  */
00020 
00021 /* $Id: datetime.c 321634 2012-01-01 13:15:04Z felipe $ */
00022 
00023 #include "php.h"
00024 #include "zend_operators.h"
00025 #include "datetime.h"
00026 #include "php_globals.h"
00027 
00028 #include <time.h>
00029 #ifdef HAVE_SYS_TIME_H
00030 # include <sys/time.h>
00031 #endif
00032 #include <stdio.h>
00033 
00034 char *mon_full_names[] = {
00035        "January", "February", "March", "April",
00036        "May", "June", "July", "August",
00037        "September", "October", "November", "December"
00038 };
00039 
00040 char *mon_short_names[] = {
00041        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
00042 };
00043 
00044 char *day_full_names[] = {
00045        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
00046 };
00047 
00048 char *day_short_names[] = {
00049        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
00050 };
00051 
00052 /* {{{ PHPAPI char *php_std_date(time_t t TSRMLS_DC)
00053    Return date string in standard format for http headers */
00054 PHPAPI char *php_std_date(time_t t TSRMLS_DC)
00055 {
00056        struct tm *tm1, tmbuf;
00057        char *str;
00058 
00059        tm1 = php_gmtime_r(&t, &tmbuf);
00060        str = emalloc(81);
00061        str[0] = '\0';
00062 
00063        if (!tm1) {
00064               return str;
00065        }
00066 
00067        if (PG(y2k_compliance)) {
00068               snprintf(str, 80, "%s, %02d %s %04d %02d:%02d:%02d GMT",
00069                             day_short_names[tm1->tm_wday],
00070                             tm1->tm_mday,
00071                             mon_short_names[tm1->tm_mon],
00072                             tm1->tm_year + 1900,
00073                             tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
00074        } else {
00075               snprintf(str, 80, "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
00076                             day_full_names[tm1->tm_wday],
00077                             tm1->tm_mday,
00078                             mon_short_names[tm1->tm_mon],
00079                             ((tm1->tm_year) % 100),
00080                             tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
00081        }
00082 
00083        str[79] = 0;
00084        return (str);
00085 }
00086 /* }}} */
00087 
00088 #if HAVE_STRPTIME
00089 #ifndef HAVE_STRPTIME_DECL_FAILS
00090 char *strptime(const char *s, const char *format, struct tm *tm);
00091 #endif
00092 
00093 /* {{{ proto string strptime(string timestamp, string format)
00094    Parse a time/date generated with strftime() */
00095 PHP_FUNCTION(strptime)
00096 {
00097        char      *ts;
00098        int        ts_length;
00099        char      *format;
00100        int        format_length;
00101        struct tm  parsed_time;
00102        char      *unparsed_part;
00103 
00104        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ts, &ts_length, &format, &format_length) == FAILURE) {
00105               return;
00106        }
00107 
00108        memset(&parsed_time, 0, sizeof(parsed_time));
00109 
00110        unparsed_part = strptime(ts, format, &parsed_time);
00111        if (unparsed_part == NULL) {
00112               RETURN_FALSE;
00113        }
00114 
00115        array_init(return_value);
00116        add_assoc_long(return_value, "tm_sec",   parsed_time.tm_sec);
00117        add_assoc_long(return_value, "tm_min",   parsed_time.tm_min);
00118        add_assoc_long(return_value, "tm_hour",  parsed_time.tm_hour);
00119        add_assoc_long(return_value, "tm_mday",  parsed_time.tm_mday);
00120        add_assoc_long(return_value, "tm_mon",   parsed_time.tm_mon);
00121        add_assoc_long(return_value, "tm_year",  parsed_time.tm_year);
00122        add_assoc_long(return_value, "tm_wday",  parsed_time.tm_wday);
00123        add_assoc_long(return_value, "tm_yday",  parsed_time.tm_yday);
00124        add_assoc_string(return_value, "unparsed", unparsed_part, 1);
00125 }
00126 /* }}} */
00127 
00128 #endif
00129 
00130 /*
00131  * Local variables:
00132  * tab-width: 4
00133  * c-basic-offset: 4
00134  * End:
00135  * vim600: sw=4 ts=4 fdm=marker
00136  * vim<600: sw=4 ts=4
00137  */