~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/NTime.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "NKernel.h"
 
2
 
 
3
#if defined INL_PS3
 
4
    // RealTimeClock
 
5
    #include <cell/rtc.h>
 
6
    // System Utilities Common API
 
7
    #include <sysutil/sysutil_common.h>
 
8
    #include <sysutil/sysutil_sysparam.h>
 
9
#endif
 
10
 
 
11
NAMESPACE_BEGIN
 
12
 
 
13
NTimeStamp::NTimeStamp()
 
14
{
 
15
 
 
16
}
 
17
 
 
18
NTimeStamp::~NTimeStamp()
 
19
{
 
20
    m_Year          = 1970;
 
21
    m_Month         = 1;
 
22
    m_Day           = 1;
 
23
    m_Hour          = 0;
 
24
    m_Minute        = 0;
 
25
    m_Second        = 0;
 
26
    m_MicroSecond   = 0;
 
27
}
 
28
 
 
29
 
 
30
t_s64 NTimeStamp::GetJulianDayNumber(void) const
 
31
{
 
32
    t_s64 JDN =  m_Day - 32075L +
 
33
        1461L * (m_Year  + 4800L + (m_Month  - 14L) / 12L) / 4L +
 
34
        367L * (m_Month - 2L - ((m_Month - 14L) / 12L) * 12L) / 12L -
 
35
        3L * ((m_Year + 4900L - (m_Month  - 14L) / 12L) / 100L) / 4L;
 
36
    return JDN;
 
37
}
 
38
 
 
39
t_f64 NTimeStamp::GetJulianDate() const
 
40
{
 
41
    t_f64 JD = GetJulianDayNumber() + (m_Hour - 12)/1440.0f + m_Minute/1440.0f + m_Second/86400.0f;
 
42
    return JD;
 
43
}
 
44
 
 
45
unsigned int NTimeStamp::GetSecondOfDay(void) const
 
46
{
 
47
        return m_Hour * 60 * 60 + m_Minute * 60 + m_Second;
 
48
}
 
49
 
 
50
bool NTimeStamp::operator == (NTimeStamp& Other) const
 
51
{
 
52
    bool b = (m_Year  ==  Other.m_Year)       &&
 
53
        (m_Day        ==  Other.m_Day)      &&
 
54
        (m_Month      ==  Other.m_Month)    &&
 
55
        (m_Hour       ==  Other.m_Hour)     &&
 
56
        (m_Minute     ==  Other.m_Minute)   &&
 
57
        (m_Second     ==  Other.m_Second);
 
58
    return b;
 
59
}
 
60
 
 
61
bool NTimeStamp::operator != (NTimeStamp& Other) const
 
62
{
 
63
    if(*this == Other)
 
64
        return false;
 
65
    return true;
 
66
}
 
67
 
 
68
bool NTimeStamp::operator < (NTimeStamp& Other) const
 
69
{
 
70
    t_f64 JD = GetJulianDate();
 
71
    if(JD < Other.GetJulianDate())
 
72
        return true;
 
73
    return false;
 
74
}
 
75
 
 
76
bool NTimeStamp::operator >  (NTimeStamp& Other) const
 
77
{
 
78
    t_f64 JD = GetJulianDate();
 
79
    if(JD > Other.GetJulianDate())
 
80
        return true;
 
81
    return false;
 
82
}
 
83
 
 
84
bool NTimeStamp::operator >= (NTimeStamp& Other) const
 
85
{
 
86
    t_f64 JD = GetJulianDate();
 
87
    if(JD >= Other.GetJulianDate())
 
88
        return true;
 
89
    return false;
 
90
}
 
91
 
 
92
bool NTimeStamp::operator <= (NTimeStamp& Other) const
 
93
{
 
94
    t_f64 JD = GetJulianDate();
 
95
    if(JD <= Other.GetJulianDate())
 
96
        return true;
 
97
    return false;
 
98
}
 
99
 
 
100
void NTimeStamp::GetTime()
 
101
{
 
102
    GetLocalTime(m_Year, m_Month, m_Day, m_Hour, m_Minute, m_Second, m_MicroSecond);
 
103
}
 
104
 
 
105
/*!
 
106
    Returns the time formatted in a string.
 
107
*/
 
108
const TCHAR* GetFormattedLocalTime()
 
109
{
 
110
    static TCHAR Result[1024];
 
111
    *Result = 0;
 
112
 
 
113
    unsigned int Year;
 
114
    unsigned int Month;
 
115
    unsigned int Day;
 
116
    unsigned int Hour;
 
117
    unsigned int Minute;
 
118
    unsigned int Second;
 
119
    unsigned int MicroSec;
 
120
 
 
121
    GetLocalTime(Year, Month, Day, Hour, Minute, Second, MicroSec);
 
122
#ifdef _WIN32
 
123
    _stprintf_s(Result, 1024, TEXT("%d:%d:%d: %d/%d/%d"), Hour, Minute, Second, Day, Month, Year);
 
124
#else
 
125
    _stprintf(Result, TEXT("%d:%d:%d: %d/%d/%d"), Hour, Minute, Second, Day, Month, Year);
 
126
#endif
 
127
 
 
128
    return Result;
 
129
}
 
130
 
 
131
void SleepSeconds(float Seconds)
 
132
{
 
133
#if (defined _WIN32)
 
134
    ::Sleep((DWORD)(Seconds * 1000.0));
 
135
#endif
 
136
}
 
137
 
 
138
void SleepMilliSeconds(float MilliSeconds)
 
139
{
 
140
#if (defined _WIN32)
 
141
    ::Sleep((DWORD)(MilliSeconds));
 
142
#endif
 
143
}
 
144
 
 
145
//
 
146
// Return the system time.
 
147
//
 
148
void GetLocalTime(unsigned int& Year,
 
149
                  unsigned int& Month,
 
150
                  unsigned int& Day,
 
151
                  unsigned int& Hour,
 
152
                  unsigned int& Min,
 
153
                  unsigned int& Sec,
 
154
                  unsigned int& MicroSec)
 
155
{
 
156
#ifdef INL_OS_WINDOWS
 
157
    SYSTEMTIME st;
 
158
    ::GetLocalTime(&st);
 
159
 
 
160
    Year                = st.wYear;
 
161
    Month               = st.wMonth;
 
162
    Day                 = st.wDay;
 
163
    Hour                = st.wHour;
 
164
    Min                 = st.wMinute;
 
165
    Sec                 = st.wSecond;
 
166
    MicroSec    = st.wMilliseconds*1000;
 
167
 
 
168
#elif (defined INL_PS3)
 
169
    CellRtcDateTime st;
 
170
    cellRtcGetCurrentClockLocalTime(&st);
 
171
    Year                = st.year;
 
172
    Month               = st.month;
 
173
    Day                 = st.day;
 
174
    Hour                = st.hour;
 
175
    Min                 = st.minute;
 
176
    Sec                 = st.second;
 
177
    MicroSec    = st.microsecond;
 
178
 
 
179
#elif (defined INL_OS_LINUX) || (defined INL_OS_MACOSX)
 
180
    time_t dt;
 
181
    struct tm dc;
 
182
    time(&dt);
 
183
    localtime_r(&dt, &dc);
 
184
 
 
185
    Year                = dc.tm_year - 100 + 2000;
 
186
    Month               = dc.tm_mon + 1;
 
187
    Day                 = dc.tm_mday;
 
188
    Hour                = dc.tm_hour;
 
189
    Min                 = dc.tm_min;
 
190
    Sec                 = dc.tm_sec;
 
191
    MicroSec    = 0;
 
192
#else
 
193
    nuxAssert(0);
 
194
#endif
 
195
}
 
196
 
 
197
void GetUTCTime(unsigned int& Year,
 
198
                unsigned int& Month,
 
199
                unsigned int& Day,
 
200
                unsigned int& Hour,
 
201
                unsigned int& Min,
 
202
                unsigned int& Sec,
 
203
                unsigned int& MicroSec)
 
204
{
 
205
#if (defined _WIN32)
 
206
    SYSTEMTIME st;
 
207
    ::GetSystemTime(&st);
 
208
 
 
209
    Year                = st.wYear;
 
210
    Month               = st.wMonth;
 
211
    Day                 = st.wDay;
 
212
    Hour                = st.wHour;
 
213
    Min                 = st.wMinute;
 
214
    Sec                 = st.wSecond;
 
215
    MicroSec    = st.wMilliseconds*1000;
 
216
 
 
217
#elif (defined INL_PS3)
 
218
    CellRtcDateTime st;
 
219
    cellRtcGetCurrentClock(&st, 0); // 0 for UTC time(at Greenwich)
 
220
    Year                = st.year;
 
221
    Month               = st.month;
 
222
    Day                 = st.day;
 
223
    Hour                = st.hour;
 
224
    Min                 = st.minute;
 
225
    Sec                 = st.second;
 
226
    MicroSec    = st.microsecond;
 
227
 
 
228
#elif (defined INL_OS_LINUX) || (defined INL_OS_MACOSX)
 
229
    time_t dt;
 
230
    struct tm dc;
 
231
    time(&dt);
 
232
    gmtime_r(&dt, &dc);
 
233
 
 
234
    Year                = dc.tm_year - 100 + 2000;
 
235
    Month               = dc.tm_mon + 1;
 
236
    Day                 = dc.tm_mday;
 
237
    Hour                = dc.tm_hour;
 
238
    Min                 = dc.tm_min;
 
239
    Sec                 = dc.tm_sec;
 
240
    MicroSec    = 0;
 
241
#else
 
242
    nuxAssert(0);
 
243
#endif
 
244
}
 
245
 
 
246
t_long GetTimeZone()
 
247
{
 
248
#if (defined _WIN32)
 
249
    t_long seconds = 0;
 
250
    _get_timezone(&seconds);
 
251
    t_long hour = seconds / 3600;
 
252
    return hour;
 
253
 
 
254
#elif INL_PS3
 
255
    int minutetimezone = 0;
 
256
    cellSysutilGetSystemParamInt(CELL_SYSUTIL_SYSTEMPARAM_ID_TIMEZONE, &minutetimezone);
 
257
    t_long hour = minutetimezone / 60;
 
258
    return hour;
 
259
 
 
260
#else
 
261
    return 0;
 
262
#endif
 
263
}
 
264
 
 
265
void SleepForMilliseconds(unsigned int Milliseconds)
 
266
{
 
267
#if defined(INL_OS_WINDOWS)
 
268
    Sleep(Milliseconds);
 
269
 
 
270
#elif defined(INL_OS_LINUX)
 
271
    int ret = usleep(Milliseconds*1000);
 
272
    if(ret != 0)
 
273
    {
 
274
        nuxDebugMsg(TEXT("[SleepForMilliseconds] usleep has failed."));
 
275
    }
 
276
#else
 
277
    #error Sleep(milliseconds) is not implemented for this platform.
 
278
#endif
 
279
}
 
280
 
 
281
NAMESPACE_END
 
 
b'\\ No newline at end of file'