]>
Commit | Line | Data |
---|---|---|
ba379fdc | 1 | /* |
93a37866 | 2 | * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com> |
ba379fdc | 3 | * |
93a37866 A |
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. | |
ba379fdc | 12 | * |
93a37866 A |
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. | |
ba379fdc A |
23 | */ |
24 | ||
25 | #include "config.h" | |
26 | #include "DateConversion.h" | |
27 | ||
93a37866 A |
28 | #include <wtf/Assertions.h> |
29 | #include <wtf/DateMath.h> | |
30 | #include <wtf/text/StringBuilder.h> | |
31 | #include <wtf/text/WTFString.h> | |
32 | ||
33 | #if OS(WINDOWS) | |
34 | #include <windows.h> | |
35 | #endif | |
ba379fdc A |
36 | |
37 | using namespace WTF; | |
38 | ||
39 | namespace JSC { | |
40 | ||
93a37866 A |
41 | template<int width> |
42 | static inline void appendNumber(StringBuilder& builder, int value) | |
ba379fdc | 43 | { |
93a37866 A |
44 | int fillingZerosCount = width; |
45 | if (value < 0) { | |
46 | builder.append('-'); | |
47 | value = -value; | |
48 | --fillingZerosCount; | |
49 | } | |
50 | String valueString = String::number(value); | |
51 | fillingZerosCount -= valueString.length(); | |
52 | for (int i = 0; i < fillingZerosCount; ++i) | |
53 | builder.append('0'); | |
54 | builder.append(valueString); | |
ba379fdc A |
55 | } |
56 | ||
93a37866 A |
57 | template<> |
58 | void appendNumber<2>(StringBuilder& builder, int value) | |
ba379fdc | 59 | { |
93a37866 A |
60 | ASSERT(0 <= value && value <= 99); |
61 | builder.append(static_cast<char>('0' + value / 10)); | |
62 | builder.append(static_cast<char>('0' + value % 10)); | |
ba379fdc A |
63 | } |
64 | ||
93a37866 | 65 | String formatDateTime(const GregorianDateTime& t, DateTimeFormat format, bool asUTCVariant) |
ba379fdc | 66 | { |
93a37866 A |
67 | bool appendDate = format & DateTimeFormatDate; |
68 | bool appendTime = format & DateTimeFormatTime; | |
ba379fdc | 69 | |
93a37866 A |
70 | StringBuilder builder; |
71 | ||
72 | if (appendDate) { | |
73 | builder.append(weekdayName[(t.weekDay() + 6) % 7]); | |
74 | ||
75 | if (asUTCVariant) { | |
76 | builder.appendLiteral(", "); | |
77 | appendNumber<2>(builder, t.monthDay()); | |
78 | builder.append(' '); | |
79 | builder.append(monthName[t.month()]); | |
80 | } else { | |
81 | builder.append(' '); | |
82 | builder.append(monthName[t.month()]); | |
83 | builder.append(' '); | |
84 | appendNumber<2>(builder, t.monthDay()); | |
85 | } | |
86 | builder.append(' '); | |
87 | appendNumber<4>(builder, t.year()); | |
ba379fdc | 88 | } |
f9bf01c6 | 89 | |
93a37866 A |
90 | if (appendDate && appendTime) |
91 | builder.append(' '); | |
92 | ||
93 | if (appendTime) { | |
94 | appendNumber<2>(builder, t.hour()); | |
95 | builder.append(':'); | |
96 | appendNumber<2>(builder, t.minute()); | |
97 | builder.append(':'); | |
98 | appendNumber<2>(builder, t.second()); | |
99 | builder.appendLiteral(" GMT"); | |
100 | ||
101 | if (!asUTCVariant) { | |
102 | int offset = abs(t.utcOffset()) / 60; | |
103 | builder.append(t.utcOffset() < 0 ? '-' : '+'); | |
104 | appendNumber<2>(builder, offset / 60); | |
105 | appendNumber<2>(builder, offset % 60); | |
106 | ||
107 | #if OS(WINDOWS) | |
108 | TIME_ZONE_INFORMATION timeZoneInformation; | |
109 | GetTimeZoneInformation(&timeZoneInformation); | |
110 | const WCHAR* timeZoneName = t.isDST() ? timeZoneInformation.DaylightName : timeZoneInformation.StandardName; | |
111 | #else | |
112 | struct tm gtm = t; | |
113 | char timeZoneName[70]; | |
114 | strftime(timeZoneName, sizeof(timeZoneName), "%Z", >m); | |
115 | #endif | |
116 | if (timeZoneName[0]) { | |
117 | builder.appendLiteral(" ("); | |
118 | builder.append(timeZoneName); | |
119 | builder.append(')'); | |
120 | } | |
121 | } | |
122 | } | |
123 | ||
124 | return builder.toString().impl(); | |
ba379fdc A |
125 | } |
126 | ||
127 | } // namespace JSC |