timemac.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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. * file: timemac.c
  7. * description: test time and date routines on the Mac
  8. */
  9. #include <stdio.h>
  10. #include "prinit.h"
  11. #include "prtime.h"
  12. static char *dayOfWeek[] =
  13. { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" };
  14. static char *month[] =
  15. { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  16. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???"
  17. };
  18. static void printExplodedTime(const PRExplodedTime *et) {
  19. PRInt32 totalOffset;
  20. PRInt32 hourOffset, minOffset;
  21. const char *sign;
  22. /* Print day of the week, month, day, hour, minute, and second */
  23. printf( "%s %s %ld %02ld:%02ld:%02ld ",
  24. dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,
  25. et->tm_hour, et->tm_min, et->tm_sec);
  26. /* Print time zone */
  27. totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset;
  28. if (totalOffset == 0) {
  29. printf("UTC ");
  30. } else {
  31. sign = "";
  32. if (totalOffset < 0) {
  33. totalOffset = -totalOffset;
  34. sign = "-";
  35. }
  36. hourOffset = totalOffset / 3600;
  37. minOffset = (totalOffset % 3600) / 60;
  38. printf("%s%02ld%02ld ", sign, hourOffset, minOffset);
  39. }
  40. /* Print year */
  41. printf("%d", et->tm_year);
  42. }
  43. int main(int argc, char** argv)
  44. {
  45. PR_STDIO_INIT();
  46. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  47. /*
  48. *************************************************************
  49. **
  50. ** Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime
  51. ** on the current time
  52. **
  53. *************************************************************
  54. */
  55. {
  56. PRTime t1, t2;
  57. PRExplodedTime et;
  58. printf("*********************************************\n");
  59. printf("** **\n");
  60. printf("** Testing PR_Now(), PR_ExplodeTime, and **\n");
  61. printf("** PR_ImplodeTime on the current time **\n");
  62. printf("** **\n");
  63. printf("*********************************************\n\n");
  64. t1 = PR_Now();
  65. /* First try converting to UTC */
  66. PR_ExplodeTime(t1, PR_GMTParameters, &et);
  67. if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) {
  68. printf("ERROR: UTC has nonzero gmt or dst offset.\n");
  69. return 1;
  70. }
  71. printf("Current UTC is ");
  72. printExplodedTime(&et);
  73. printf("\n");
  74. t2 = PR_ImplodeTime(&et);
  75. if (LL_NE(t1, t2)) {
  76. printf("ERROR: Explode and implode are NOT inverse.\n");
  77. return 1;
  78. }
  79. /* Next, try converting to local (US Pacific) time */
  80. PR_ExplodeTime(t1, PR_LocalTimeParameters, &et);
  81. printf("Current local time is ");
  82. printExplodedTime(&et);
  83. printf("\n");
  84. printf("GMT offset is %ld, DST offset is %ld\n",
  85. et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset);
  86. t2 = PR_ImplodeTime(&et);
  87. if (LL_NE(t1, t2)) {
  88. printf("ERROR: Explode and implode are NOT inverse.\n");
  89. return 1;
  90. }
  91. }
  92. printf("Please examine the results\n");
  93. return 0;
  94. }