DateConversion.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "config.h"
  25. #include "DateConversion.h"
  26. #include <wtf/Assertions.h>
  27. #include <wtf/DateMath.h>
  28. #include <wtf/text/StringBuilder.h>
  29. #include <wtf/text/WTFString.h>
  30. #if OS(WINDOWS)
  31. #include <windows.h>
  32. #endif
  33. using namespace WTF;
  34. namespace JSC {
  35. template<int width>
  36. static inline void appendNumber(StringBuilder& builder, int value)
  37. {
  38. int fillingZerosCount = width;
  39. if (value < 0) {
  40. builder.append('-');
  41. value = -value;
  42. --fillingZerosCount;
  43. }
  44. String valueString = String::number(value);
  45. fillingZerosCount -= valueString.length();
  46. for (int i = 0; i < fillingZerosCount; ++i)
  47. builder.append('0');
  48. builder.append(valueString);
  49. }
  50. template<>
  51. void appendNumber<2>(StringBuilder& builder, int value)
  52. {
  53. ASSERT(0 <= value && value <= 99);
  54. builder.append(static_cast<char>('0' + value / 10));
  55. builder.append(static_cast<char>('0' + value % 10));
  56. }
  57. String formatDateTime(const GregorianDateTime& t, DateTimeFormat format, bool asUTCVariant)
  58. {
  59. bool appendDate = format & DateTimeFormatDate;
  60. bool appendTime = format & DateTimeFormatTime;
  61. StringBuilder builder;
  62. if (appendDate) {
  63. builder.append(weekdayName[(t.weekDay() + 6) % 7]);
  64. if (asUTCVariant) {
  65. builder.appendLiteral(", ");
  66. appendNumber<2>(builder, t.monthDay());
  67. builder.append(' ');
  68. builder.append(monthName[t.month()]);
  69. } else {
  70. builder.append(' ');
  71. builder.append(monthName[t.month()]);
  72. builder.append(' ');
  73. appendNumber<2>(builder, t.monthDay());
  74. }
  75. builder.append(' ');
  76. appendNumber<4>(builder, t.year());
  77. }
  78. if (appendDate && appendTime)
  79. builder.append(' ');
  80. if (appendTime) {
  81. appendNumber<2>(builder, t.hour());
  82. builder.append(':');
  83. appendNumber<2>(builder, t.minute());
  84. builder.append(':');
  85. appendNumber<2>(builder, t.second());
  86. builder.appendLiteral(" GMT");
  87. if (!asUTCVariant) {
  88. int offset = abs(t.utcOffset()) / 60;
  89. builder.append(t.utcOffset() < 0 ? '-' : '+');
  90. appendNumber<2>(builder, offset / 60);
  91. appendNumber<2>(builder, offset % 60);
  92. #if OS(WINDOWS)
  93. TIME_ZONE_INFORMATION timeZoneInformation;
  94. GetTimeZoneInformation(&timeZoneInformation);
  95. const WCHAR* timeZoneName = t.isDST() ? timeZoneInformation.DaylightName : timeZoneInformation.StandardName;
  96. #else
  97. struct tm gtm = t;
  98. char timeZoneName[70];
  99. strftime(timeZoneName, sizeof(timeZoneName), "%Z", &gtm);
  100. #endif
  101. if (timeZoneName[0]) {
  102. builder.appendLiteral(" (");
  103. builder.append(timeZoneName);
  104. builder.append(')');
  105. }
  106. }
  107. }
  108. return builder.toString().impl();
  109. }
  110. } // namespace JSC