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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
#include "rar.hpp"
void RarTime::GetLocal(RarLocalTime *lt)
{
#ifdef _WIN_ALL
FILETIME ft;
GetWinFT(&ft);
FILETIME lft;
if (WinNT() < WNT_VISTA)
{
// SystemTimeToTzSpecificLocalTime based code produces 1 hour error on XP.
FileTimeToLocalFileTime(&ft,&lft);
}
else
{
// We use these functions instead of FileTimeToLocalFileTime according to
// MSDN recommendation: "To account for daylight saving time
// when converting a file time to a local time ..."
SYSTEMTIME st1,st2;
FileTimeToSystemTime(&ft,&st1);
SystemTimeToTzSpecificLocalTime(NULL,&st1,&st2);
SystemTimeToFileTime(&st2,&lft);
// Correct precision loss (low 4 decimal digits) in FileTimeToSystemTime.
FILETIME rft;
SystemTimeToFileTime(&st1,&rft);
uint64 Corrected=INT32TO64(ft.dwHighDateTime,ft.dwLowDateTime)-
INT32TO64(rft.dwHighDateTime,rft.dwLowDateTime)+
INT32TO64(lft.dwHighDateTime,lft.dwLowDateTime);
lft.dwLowDateTime=(DWORD)Corrected;
lft.dwHighDateTime=(DWORD)(Corrected>>32);
}
SYSTEMTIME st;
FileTimeToSystemTime(&lft,&st);
lt->Year=st.wYear;
lt->Month=st.wMonth;
lt->Day=st.wDay;
lt->Hour=st.wHour;
lt->Minute=st.wMinute;
lt->Second=st.wSecond;
lt->wDay=st.wDayOfWeek;
lt->yDay=lt->Day-1;
static int mdays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
for (uint I=1;I<lt->Month && I<=ASIZE(mdays);I++)
lt->yDay+=mdays[I-1];
if (lt->Month>2 && IsLeapYear(lt->Year))
lt->yDay++;
#else
time_t ut=GetUnix();
struct tm *t;
t=localtime(&ut);
lt->Year=t->tm_year+1900;
lt->Month=t->tm_mon+1;
lt->Day=t->tm_mday;
lt->Hour=t->tm_hour;
lt->Minute=t->tm_min;
lt->Second=t->tm_sec;
lt->wDay=t->tm_wday;
lt->yDay=t->tm_yday;
#endif
lt->Reminder=(itime % TICKS_PER_SECOND);
}
void RarTime::SetLocal(RarLocalTime *lt)
{
#ifdef _WIN_ALL
SYSTEMTIME st;
st.wYear=(WORD)lt->Year;
st.wMonth=(WORD)lt->Month;
st.wDay=(WORD)lt->Day;
st.wHour=(WORD)lt->Hour;
st.wMinute=(WORD)lt->Minute;
st.wSecond=(WORD)lt->Second;
st.wMilliseconds=0;
st.wDayOfWeek=0;
FILETIME lft;
if (SystemTimeToFileTime(&st,&lft))
{
FILETIME ft;
if (WinNT() < WNT_VISTA)
{
// TzSpecificLocalTimeToSystemTime based code produces 1 hour error on XP.
LocalFileTimeToFileTime(&lft,&ft);
}
else
{
// Reverse procedure which we do in GetLocal.
SYSTEMTIME st1,st2;
FileTimeToSystemTime(&lft,&st2); // st2 might be unequal to st, because we added lt->Reminder to lft.
TzSpecificLocalTimeToSystemTime(NULL,&st2,&st1);
SystemTimeToFileTime(&st1,&ft);
// Correct precision loss (low 4 decimal digits) in FileTimeToSystemTime.
FILETIME rft;
SystemTimeToFileTime(&st2,&rft);
uint64 Corrected=INT32TO64(lft.dwHighDateTime,lft.dwLowDateTime)-
INT32TO64(rft.dwHighDateTime,rft.dwLowDateTime)+
INT32TO64(ft.dwHighDateTime,ft.dwLowDateTime);
ft.dwLowDateTime=(DWORD)Corrected;
ft.dwHighDateTime=(DWORD)(Corrected>>32);
}
SetWinFT(&ft);
}
else
Reset();
#else
struct tm t;
t.tm_sec=lt->Second;
t.tm_min=lt->Minute;
t.tm_hour=lt->Hour;
t.tm_mday=lt->Day;
t.tm_mon=lt->Month-1;
t.tm_year=lt->Year-1900;
t.tm_isdst=-1;
SetUnix(mktime(&t));
#endif
itime+=lt->Reminder;
}
#ifdef _WIN_ALL
void RarTime::GetWinFT(FILETIME *ft)
{
_ULARGE_INTEGER ul;
ul.QuadPart=GetWin();
ft->dwLowDateTime=ul.LowPart;
ft->dwHighDateTime=ul.HighPart;
}
void RarTime::SetWinFT(FILETIME *ft)
{
_ULARGE_INTEGER ul = {ft->dwLowDateTime, ft->dwHighDateTime};
SetWin(ul.QuadPart);
}
#endif
// Get 64-bit representation of Windows FILETIME (100ns since 01.01.1601).
uint64 RarTime::GetWin()
{
return itime/(TICKS_PER_SECOND/10000000);
}
// Set 64-bit representation of Windows FILETIME (100ns since 01.01.1601).
void RarTime::SetWin(uint64 WinTime)
{
itime=WinTime*(TICKS_PER_SECOND/10000000);
}
time_t RarTime::GetUnix()
{
return time_t(GetUnixNS()/1000000000);
}
void RarTime::SetUnix(time_t ut)
{
if (sizeof(ut)>4)
SetUnixNS(uint64(ut)*1000000000);
else
{
// Convert 32-bit and possibly signed time_t to uint32 first,
// uint64 cast is not enough. Otherwise sign can expand to 64 bits.
SetUnixNS(uint64(uint32(ut))*1000000000);
}
}
// Get the high precision Unix time in nanoseconds since 01-01-1970.
uint64 RarTime::GetUnixNS()
{
const uint64 ushift=11644473600000000000ULL; // ns between 01-01-1601 and 01-01-1970.
return itime*(1000000000/TICKS_PER_SECOND)-ushift;
}
// Set the high precision Unix time in nanoseconds since 01-01-1970.
void RarTime::SetUnixNS(uint64 ns)
{
const uint64 ushift=11644473600000000000ULL; // ns between 01-01-1601 and 01-01-1970.
itime=(ns+ushift)/(1000000000/TICKS_PER_SECOND);
}
uint RarTime::GetDos()
{
RarLocalTime lt;
GetLocal(<);
uint DosTime=(lt.Second/2)|(lt.Minute<<5)|(lt.Hour<<11)|
(lt.Day<<16)|(lt.Month<<21)|((lt.Year-1980)<<25);
return DosTime;
}
void RarTime::SetDos(uint DosTime)
{
RarLocalTime lt;
lt.Second=(DosTime & 0x1f)*2;
lt.Minute=(DosTime>>5) & 0x3f;
lt.Hour=(DosTime>>11) & 0x1f;
lt.Day=(DosTime>>16) & 0x1f;
lt.Month=(DosTime>>21) & 0x0f;
lt.Year=(DosTime>>25)+1980;
lt.Reminder=0;
SetLocal(<);
}
void RarTime::GetText(wchar *DateStr,size_t MaxSize,bool FullMS)
{
if (IsSet())
{
RarLocalTime lt;
GetLocal(<);
if (FullMS)
swprintf(DateStr,MaxSize,L"%u-%02u-%02u %02u:%02u:%02u,%09u",lt.Year,lt.Month,lt.Day,lt.Hour,lt.Minute,lt.Second,lt.Reminder*(1000000000/TICKS_PER_SECOND));
else
swprintf(DateStr,MaxSize,L"%u-%02u-%02u %02u:%02u",lt.Year,lt.Month,lt.Day,lt.Hour,lt.Minute);
}
else
{
// We use escape before '?' to avoid weird C trigraph characters.
wcsncpyz(DateStr,L"\?\?\?\?-\?\?-\?\? \?\?:\?\?",MaxSize);
}
}
#ifndef SFX_MODULE
void RarTime::SetIsoText(const wchar *TimeText)
{
int Field[6];
memset(Field,0,sizeof(Field));
for (uint DigitCount=0;*TimeText!=0;TimeText++)
if (IsDigit(*TimeText))
{
int FieldPos=DigitCount<4 ? 0:(DigitCount-4)/2+1;
if (FieldPos<ASIZE(Field))
Field[FieldPos]=Field[FieldPos]*10+*TimeText-'0';
DigitCount++;
}
RarLocalTime lt;
lt.Second=Field[5];
lt.Minute=Field[4];
lt.Hour=Field[3];
lt.Day=Field[2]==0 ? 1:Field[2];
lt.Month=Field[1]==0 ? 1:Field[1];
lt.Year=Field[0];
lt.Reminder=0;
SetLocal(<);
}
#endif
#ifndef SFX_MODULE
void RarTime::SetAgeText(const wchar *TimeText)
{
uint Seconds=0,Value=0;
for (uint I=0;TimeText[I]!=0;I++)
{
wchar Ch=TimeText[I];
if (IsDigit(Ch))
Value=Value*10+Ch-'0';
else
{
switch(etoupperw(Ch))
{
case 'D':
Seconds+=Value*24*3600;
break;
case 'H':
Seconds+=Value*3600;
break;
case 'M':
Seconds+=Value*60;
break;
case 'S':
Seconds+=Value;
break;
}
Value=0;
}
}
SetCurrentTime();
itime-=uint64(Seconds)*TICKS_PER_SECOND;
}
#endif
void RarTime::SetCurrentTime()
{
#ifdef _WIN_ALL
FILETIME ft;
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st,&ft);
SetWinFT(&ft);
#else
time_t st;
time(&st);
SetUnix(st);
#endif
}
// Add the specified signed number of nanoseconds.
void RarTime::Adjust(int64 ns)
{
ns/=1000000000/TICKS_PER_SECOND; // Convert ns to internal ticks.
itime+=(uint64)ns;
}
#ifndef SFX_MODULE
const wchar *GetMonthName(uint Month)
{
return uiGetMonthName(Month);
}
#endif
bool IsLeapYear(uint Year)
{
return (Year&3)==0 && (Year%100!=0 || Year%400==0);
}
|