datetime.c 3.3 KB

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