nux-1.14.0
|
00001 /* 00002 * Copyright 2010 Inalogic® Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License, as 00006 * published by the Free Software Foundation; either version 2.1 or 3.0 00007 * of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranties of 00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00012 * PURPOSE. See the applicable version of the GNU Lesser General Public 00013 * License for more details. 00014 * 00015 * You should have received a copy of both the GNU Lesser General Public 00016 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 00023 #include "NuxCore.h" 00024 00025 namespace nux 00026 { 00027 00028 TimeStamp::TimeStamp() 00029 { 00030 GetTime(); 00031 } 00032 00033 TimeStamp::~TimeStamp() 00034 { 00035 m_Year = 1970; 00036 m_Month = 1; 00037 m_Day = 1; 00038 m_Hour = 0; 00039 m_Minute = 0; 00040 m_Second = 0; 00041 m_MicroSecond = 0; 00042 } 00043 00044 00045 t_s64 TimeStamp::GetJulianDayNumber (void) const 00046 { 00047 t_s64 JDN = m_Day - 32075L + 00048 1461L * (m_Year + 4800L + (m_Month - 14L) / 12L) / 4L + 00049 367L * (m_Month - 2L - ( (m_Month - 14L) / 12L) * 12L) / 12L - 00050 3L * ( (m_Year + 4900L - (m_Month - 14L) / 12L) / 100L) / 4L; 00051 return JDN; 00052 } 00053 00054 t_f64 TimeStamp::GetJulianDate() const 00055 { 00056 t_f64 JD = GetJulianDayNumber() + (m_Hour - 12) / 1440.0f + m_Minute / 1440.0f + m_Second / 86400.0f; 00057 return JD; 00058 } 00059 00060 unsigned int TimeStamp::GetSecondOfDay (void) const 00061 { 00062 return m_Hour * 60 * 60 + m_Minute * 60 + m_Second; 00063 } 00064 00065 bool TimeStamp::operator == (TimeStamp &Other) const 00066 { 00067 bool b = (m_Year == Other.m_Year) && 00068 (m_Day == Other.m_Day) && 00069 (m_Month == Other.m_Month) && 00070 (m_Hour == Other.m_Hour) && 00071 (m_Minute == Other.m_Minute) && 00072 (m_Second == Other.m_Second); 00073 return b; 00074 } 00075 00076 bool TimeStamp::operator != (TimeStamp &Other) const 00077 { 00078 if (*this == Other) 00079 return false; 00080 00081 return true; 00082 } 00083 00084 bool TimeStamp::operator < (TimeStamp &Other) const 00085 { 00086 t_f64 JD = GetJulianDate(); 00087 00088 if (JD < Other.GetJulianDate() ) 00089 return true; 00090 00091 return false; 00092 } 00093 00094 bool TimeStamp::operator > (TimeStamp &Other) const 00095 { 00096 t_f64 JD = GetJulianDate(); 00097 00098 if (JD > Other.GetJulianDate() ) 00099 return true; 00100 00101 return false; 00102 } 00103 00104 bool TimeStamp::operator >= (TimeStamp &Other) const 00105 { 00106 t_f64 JD = GetJulianDate(); 00107 00108 if (JD >= Other.GetJulianDate() ) 00109 return true; 00110 00111 return false; 00112 } 00113 00114 bool TimeStamp::operator <= (TimeStamp &Other) const 00115 { 00116 t_f64 JD = GetJulianDate(); 00117 00118 if (JD <= Other.GetJulianDate() ) 00119 return true; 00120 00121 return false; 00122 } 00123 00124 void TimeStamp::GetTime() 00125 { 00126 GetLocalTime (m_Year, m_Month, m_Day, m_Hour, m_Minute, m_Second, m_MicroSecond); 00127 } 00128 00129 00130 NString GetFormattedLocalTime() 00131 { 00132 TCHAR buffer[1024]; 00133 Memset(buffer, 0, 1024); 00134 00135 unsigned int Year; 00136 unsigned int Month; 00137 unsigned int Day; 00138 unsigned int Hour; 00139 unsigned int Minute; 00140 unsigned int Second; 00141 unsigned int MicroSec; 00142 00143 GetLocalTime (Year, Month, Day, Hour, Minute, Second, MicroSec); 00144 #ifdef _WIN32 00145 _stprintf_s (buffer, 1024 - 1, TEXT ("%d:%d:%d: %d/%d/%d"), Hour, Minute, Second, Day, Month, Year); 00146 NString result = buffer; 00147 #else 00148 _stprintf (buffer, TEXT ("%d:%d:%d: %d/%d/%d"), Hour, Minute, Second, Day, Month, Year); 00149 NString result = buffer; 00150 #endif 00151 00152 return result; 00153 } 00154 00155 void GetLocalTime (unsigned int &Year, 00156 unsigned int &Month, 00157 unsigned int &Day, 00158 unsigned int &Hour, 00159 unsigned int &Min, 00160 unsigned int &Sec, 00161 unsigned int &MicroSec) 00162 { 00163 #ifdef NUX_OS_WINDOWS 00164 SYSTEMTIME st; 00165 ::GetLocalTime (&st); 00166 00167 Year = st.wYear; 00168 Month = st.wMonth; 00169 Day = st.wDay; 00170 Hour = st.wHour; 00171 Min = st.wMinute; 00172 Sec = st.wSecond; 00173 MicroSec = st.wMilliseconds * 1000; 00174 00175 #elif (defined NUX_OS_LINUX) || (defined NUX_OS_MACOSX) 00176 time_t dt; 00177 struct tm dc; 00178 time (&dt); 00179 localtime_r (&dt, &dc); 00180 00181 Year = dc.tm_year - 100 + 2000; 00182 Month = dc.tm_mon + 1; 00183 Day = dc.tm_mday; 00184 Hour = dc.tm_hour; 00185 Min = dc.tm_min; 00186 Sec = dc.tm_sec; 00187 MicroSec = 0; 00188 #else 00189 nuxAssert (0); 00190 #endif 00191 } 00192 00193 void GetUTCTime (unsigned int &Year, 00194 unsigned int &Month, 00195 unsigned int &Day, 00196 unsigned int &Hour, 00197 unsigned int &Min, 00198 unsigned int &Sec, 00199 unsigned int &MicroSec) 00200 { 00201 #if (defined _WIN32) 00202 SYSTEMTIME st; 00203 ::GetSystemTime (&st); 00204 00205 Year = st.wYear; 00206 Month = st.wMonth; 00207 Day = st.wDay; 00208 Hour = st.wHour; 00209 Min = st.wMinute; 00210 Sec = st.wSecond; 00211 MicroSec = st.wMilliseconds * 1000; 00212 00213 #elif (defined NUX_OS_LINUX) || (defined NUX_OS_MACOSX) 00214 time_t dt; 00215 struct tm dc; 00216 time (&dt); 00217 gmtime_r (&dt, &dc); 00218 00219 Year = dc.tm_year - 100 + 2000; 00220 Month = dc.tm_mon + 1; 00221 Day = dc.tm_mday; 00222 Hour = dc.tm_hour; 00223 Min = dc.tm_min; 00224 Sec = dc.tm_sec; 00225 MicroSec = 0; 00226 #else 00227 nuxAssert (0); 00228 #endif 00229 } 00230 00231 t_long GetTimeZone() 00232 { 00233 #if (defined _WIN32) 00234 t_long seconds = 0; 00235 _get_timezone (&seconds); 00236 t_long hour = seconds / 3600; 00237 return hour; 00238 #else 00239 return 0; 00240 #endif 00241 } 00242 00243 void SleepForMilliseconds (unsigned int Milliseconds) 00244 { 00245 #if defined(NUX_OS_WINDOWS) 00246 Sleep (Milliseconds); 00247 00248 #elif defined(NUX_OS_LINUX) 00249 int ret = usleep (Milliseconds * 1000); 00250 00251 if (ret != 0) 00252 { 00253 nuxDebugMsg (TEXT ("[SleepForMilliseconds] usleep has failed.") ); 00254 } 00255 00256 #else 00257 #error Sleep(milliseconds) is not implemented for this platform. 00258 #endif 00259 } 00260 00261 }