datetime.c 3.6 KB

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