httpdate.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*-
  2. * Copyright 1997 Massachusetts Institute of Technology
  3. *
  4. * Permission to use, copy, modify, and distribute this software and
  5. * its documentation for any purpose and without fee is hereby
  6. * granted, provided that both the above copyright notice and this
  7. * permission notice appear in all copies, that both the above
  8. * copyright notice and this permission notice appear in all
  9. * supporting documentation, and that the name of M.I.T. not be used
  10. * in advertising or publicity pertaining to distribution of the
  11. * software without specific, written prior permission. M.I.T. makes
  12. * no representations about the suitability of this software for any
  13. * purpose. It is provided "as is" without express or implied
  14. * warranty.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
  17. * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
  18. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
  20. * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  23. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  26. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <util/system/defaults.h>
  30. #include <sys/types.h>
  31. #include <cctype>
  32. #include <cstdio>
  33. #include <cstdlib>
  34. #include <cstring>
  35. #include <ctime>
  36. #include <util/system/compat.h> /* stricmp */
  37. #include <util/system/yassert.h>
  38. #include "httpdate.h"
  39. #include <util/datetime/base.h>
  40. static const char *wkdays[] = {
  41. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  42. };
  43. static const char *months[] = {
  44. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
  45. "Nov", "Dec"
  46. };
  47. int format_http_date(char buf[], size_t size, time_t when) {
  48. struct tm tms;
  49. GmTimeR(&when, &tms);
  50. #ifndef HTTP_DATE_ISO_8601
  51. return snprintf(buf, size, "%s, %02d %s %04d %02d:%02d:%02d GMT",
  52. wkdays[tms.tm_wday], tms.tm_mday, months[tms.tm_mon],
  53. tms.tm_year + 1900, tms.tm_hour, tms.tm_min, tms.tm_sec);
  54. #else /* ISO 8601 */
  55. return snprintf(buf, size, "%04d%02d%02dT%02d%02d%02d+0000",
  56. tms.tm_year + 1900, tms.tm_mon + 1, tms.tm_mday,
  57. tms.tm_hour, tms.tm_min, tms.tm_sec);
  58. #endif
  59. }
  60. char* format_http_date(time_t when, char* buf, size_t buflen) {
  61. const int len = format_http_date(buf, buflen, when);
  62. if (len == 0) {
  63. return nullptr;
  64. }
  65. Y_ASSERT(len > 0 && size_t(len) < buflen);
  66. return buf;
  67. }
  68. TString FormatHttpDate(time_t when) {
  69. char str[64] = {0};
  70. format_http_date(str, Y_ARRAY_SIZE(str), when);
  71. return TString(str);
  72. }