parsetm.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * This test program should eventually become a full-blown test for
  7. * PR_ParseTimeString. Right now it just verifies that PR_ParseTimeString
  8. * doesn't crash on an out-of-range time string (bug 480740).
  9. */
  10. #include "prtime.h"
  11. #include <time.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. PRBool debug_mode = PR_TRUE;
  16. static char *dayOfWeek[] =
  17. { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" };
  18. static char *month[] =
  19. { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  20. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???"
  21. };
  22. static void PrintExplodedTime(const PRExplodedTime *et) {
  23. PRInt32 totalOffset;
  24. PRInt32 hourOffset, minOffset;
  25. const char *sign;
  26. /* Print day of the week, month, day, hour, minute, and second */
  27. if (debug_mode) printf("%s %s %ld %02ld:%02ld:%02ld ",
  28. dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,
  29. et->tm_hour, et->tm_min, et->tm_sec);
  30. /* Print time zone */
  31. totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset;
  32. if (totalOffset == 0) {
  33. if (debug_mode) {
  34. printf("UTC ");
  35. }
  36. } else {
  37. sign = "+";
  38. if (totalOffset < 0) {
  39. totalOffset = -totalOffset;
  40. sign = "-";
  41. }
  42. hourOffset = totalOffset / 3600;
  43. minOffset = (totalOffset % 3600) / 60;
  44. if (debug_mode) {
  45. printf("%s%02ld%02ld ", sign, hourOffset, minOffset);
  46. }
  47. }
  48. /* Print year */
  49. if (debug_mode) {
  50. printf("%hd", et->tm_year);
  51. }
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. PRTime ct;
  56. PRExplodedTime et;
  57. PRStatus rv;
  58. char *sp1 = "Sat, 1 Jan 3001 00:00:00"; /* no time zone */
  59. char *sp2 = "Fri, 31 Dec 3000 23:59:60"; /* no time zone, not normalized */
  60. #if _MSC_VER >= 1400 && !defined(WINCE)
  61. /* Run this test in the US Pacific Time timezone. */
  62. _putenv_s("TZ", "PST8PDT");
  63. _tzset();
  64. #endif
  65. rv = PR_ParseTimeString(sp1, PR_FALSE, &ct);
  66. printf("rv = %d\n", rv);
  67. PR_ExplodeTime(ct, PR_GMTParameters, &et);
  68. PrintExplodedTime(&et);
  69. printf("\n");
  70. rv = PR_ParseTimeString(sp2, PR_FALSE, &ct);
  71. printf("rv = %d\n", rv);
  72. PR_ExplodeTime(ct, PR_GMTParameters, &et);
  73. PrintExplodedTime(&et);
  74. printf("\n");
  75. return 0;
  76. }