1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/**
* @file viewertime.cpp
* @brief sets the viewer time and time format
*
* $LicenseInfo:firstyear=2009&license=viewergpl$
*
* Copyright (c) 2010, McCabe Maxsted
*
* Imprudence Viewer Source Code
* The source code in this file ("Source Code") is provided to you
* under the terms of the GNU General Public License, version 2.0
* ("GPL"). Terms of the GPL can be found in doc/GPL-license.txt in
* this distribution, or online at
* http://secondlifegrid.net/programs/open_source/licensing/gplv2
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution, or
* online at http://secondlifegrid.net/programs/open_source/licensing/flossexception
*
* By copying, modifying or distributing this software, you acknowledge
* that you have read and understood your obligations described above,
* and agree to abide by those obligations.
*
* ALL SOURCE CODE IS PROVIDED "AS IS." THE AUTHOR MAKES NO
* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
* COMPLETENESS OR PERFORMANCE.
* $/LicenseInfo$
*/
#include "llviewerprecompiledheaders.h"
#include "llappviewer.h" // for gPacificDaylightTime
#include "lltrans.h"
#include "viewertime.h"
// system includes
#include <iomanip>
// WIP -- MC
// TODO: localize time
// TODO: accept time_t and return the appropriate format for timestamps
// TODO: support multiple date formats
ViewerTime* gViewerTime = 0;
bool ViewerTime::sUse24HourTime = false;
bool ViewerTime::sUseUTCTime = false;
std::vector<std::string> ViewerTime::sDays;
std::vector<std::string> ViewerTime::sMonths;
const U32 ViewerTime::MAX_DATE_STRING_LENGTH = 2000;
ViewerTime::ViewerTime() :
mHour(0),
mMinute(0),
mSecond(0),
mTZ(LLStringUtil::null),
mTimeStr(LLStringUtil::null),
mAMPM(" AM"),
mDay(LLStringUtil::null),
mDayNum(1),
mMonth(LLStringUtil::null),
mYear(1900),
mDateStr(LLStringUtil::null)
{
// size of day of the weeks and year
sDays.reserve(7);
sMonths.reserve(12);
setupDate();
refresh();
}
ViewerTime::~ViewerTime()
{
}
void ViewerTime::refresh()
{
// Get current UTC time, adjusted for the user's clock
// being off.
time_t utc_time;
utc_time = time_corrected();
// There's only one internal tm buffer.
struct tm* internal_time;
if (!sUseUTCTime)
{
// Convert to Pacific, based on server's opinion of whether
// it's daylight savings time there.
internal_time = utc_to_pacific_time(utc_time, gPacificDaylightTime);
}
else
{
time(&utc_time);
internal_time = gmtime(&utc_time);
}
mMinute = internal_time->tm_min;
mSecond = internal_time->tm_sec;
S32 hour = internal_time->tm_hour;
mAMPM = "";
char leading = '0';
if (!sUseUTCTime)
{
if (!sUse24HourTime)
{
leading = ' ';
mAMPM = " AM";
if (hour > 11)
{
hour -= 12;
mAMPM = " PM";
}
// Zero hour is 12 AM
if (hour == 0) hour = 12;
}
mTZ = "PST";
if (gPacificDaylightTime)
{
mTZ = "PDT";
}
}
else // just UTC
{
mTZ = "UTC";
}
mHour = hour;
std::ostringstream t;
t << std::setfill(leading) << std::setw(2) << mHour << ":"
<< std::setfill('0') << std::setw(2) << mMinute
<< mAMPM << " " << mTZ;
mTimeStr = t.str();
// Year starts at 1900
mDay = sDays[internal_time->tm_wday];
mDayNum = internal_time->tm_mday;
mMonth = sMonths[internal_time->tm_mon];
mYear = internal_time->tm_year + 1900;
std::ostringstream date;
date << mDay << ", "
<< std::setfill('0') << std::setw(2) << mDayNum << " "
<< mMonth << " "
<< mYear;
mDateStr = date.str();
}
// sets the static variables necessary for the date
void ViewerTime::setupDate()
{
// fill the day array with what's in the xui
std::string day_list = LLTrans::getString("StatBarDaysOfWeek");
size_t length = day_list.size();
// quick input check
if(length < MAX_DATE_STRING_LENGTH)
{
// tokenize it and put it in the array
std::string cur_word;
for(size_t i = 0; i < length; ++i)
{
if(day_list[i] == ':')
{
sDays.push_back(cur_word);
cur_word.clear();
}
else
{
cur_word.append(1, day_list[i]);
}
}
sDays.push_back(cur_word);
}
// fill the day array with what's in the xui
std::string month_list = LLTrans::getString("StatBarMonthsOfYear");
length = month_list.size();
// quick input check
if(length < MAX_DATE_STRING_LENGTH)
{
// tokenize it and put it in the array
std::string cur_word;
for(size_t i = 0; i < length; ++i)
{
if(month_list[i] == ':')
{
sMonths.push_back(cur_word);
cur_word.clear();
}
else
{
cur_word.append(1, month_list[i]);
}
}
sMonths.push_back(cur_word);
}
// make sure we have at least 7 days and 12 months
if(sDays.size() < 7)
{
sDays.resize(7);
}
if(sMonths.size() < 12)
{
sMonths.resize(12);
}
}
std::string ViewerTime::getDayOfWeek(const U32& day_num)
{
if (day_num < 7 && !sDays.empty())
{
return sDays[day_num];
}
return LLStringUtil::null;
}
std::string ViewerTime::getMonth(const U32& month_num)
{
if (month_num < 12 && !sMonths.empty())
{
return sMonths[month_num];
}
return LLStringUtil::null;
}
void ViewerTime::updateTimeFormat(const U32& index)
{
/*
Time Format:
0 - sim 12 hour time
1 - sim 24 hour time
2 - UTC time
*/
switch (index)
{
case 2:
sUse24HourTime = false;
sUseUTCTime = true;
break;
case 1:
sUse24HourTime = true;
sUseUTCTime = false;
break;
case 0:
default:
sUse24HourTime = false;
sUseUTCTime = false;
break;
}
}
|