datetime.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. /* $Id$ */
  21. #include "php.h"
  22. #include "zend_operators.h"
  23. #include "datetime.h"
  24. #include "php_globals.h"
  25. #include <time.h>
  26. #ifdef HAVE_SYS_TIME_H
  27. # include <sys/time.h>
  28. #endif
  29. #include <stdio.h>
  30. char *mon_full_names[] = {
  31. "January", "February", "March", "April",
  32. "May", "June", "July", "August",
  33. "September", "October", "November", "December"
  34. };
  35. char *mon_short_names[] = {
  36. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  37. };
  38. char *day_full_names[] = {
  39. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  40. };
  41. char *day_short_names[] = {
  42. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  43. };
  44. /* {{{ PHPAPI char *php_std_date(time_t t TSRMLS_DC)
  45. Return date string in standard format for http headers */
  46. PHPAPI char *php_std_date(time_t t TSRMLS_DC)
  47. {
  48. struct tm *tm1, tmbuf;
  49. char *str;
  50. tm1 = php_gmtime_r(&t, &tmbuf);
  51. str = emalloc(81);
  52. str[0] = '\0';
  53. if (!tm1) {
  54. return str;
  55. }
  56. snprintf(str, 80, "%s, %02d %s %04d %02d:%02d:%02d GMT",
  57. day_short_names[tm1->tm_wday],
  58. tm1->tm_mday,
  59. mon_short_names[tm1->tm_mon],
  60. tm1->tm_year + 1900,
  61. tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
  62. str[79] = 0;
  63. return (str);
  64. }
  65. /* }}} */
  66. #if HAVE_STRPTIME
  67. #ifndef HAVE_STRPTIME_DECL_FAILS
  68. char *strptime(const char *s, const char *format, struct tm *tm);
  69. #endif
  70. /* {{{ proto string strptime(string timestamp, string format)
  71. Parse a time/date generated with strftime() */
  72. PHP_FUNCTION(strptime)
  73. {
  74. char *ts;
  75. int ts_length;
  76. char *format;
  77. int format_length;
  78. struct tm parsed_time;
  79. char *unparsed_part;
  80. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ts, &ts_length, &format, &format_length) == FAILURE) {
  81. return;
  82. }
  83. memset(&parsed_time, 0, sizeof(parsed_time));
  84. unparsed_part = strptime(ts, format, &parsed_time);
  85. if (unparsed_part == NULL) {
  86. RETURN_FALSE;
  87. }
  88. array_init(return_value);
  89. add_assoc_long(return_value, "tm_sec", parsed_time.tm_sec);
  90. add_assoc_long(return_value, "tm_min", parsed_time.tm_min);
  91. add_assoc_long(return_value, "tm_hour", parsed_time.tm_hour);
  92. add_assoc_long(return_value, "tm_mday", parsed_time.tm_mday);
  93. add_assoc_long(return_value, "tm_mon", parsed_time.tm_mon);
  94. add_assoc_long(return_value, "tm_year", parsed_time.tm_year);
  95. add_assoc_long(return_value, "tm_wday", parsed_time.tm_wday);
  96. add_assoc_long(return_value, "tm_yday", parsed_time.tm_yday);
  97. add_assoc_string(return_value, "unparsed", unparsed_part, 1);
  98. }
  99. /* }}} */
  100. #endif
  101. /*
  102. * Local variables:
  103. * tab-width: 4
  104. * c-basic-offset: 4
  105. * End:
  106. * vim600: sw=4 ts=4 fdm=marker
  107. * vim<600: sw=4 ts=4
  108. */