summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/classMTime.cpp
blob: a615733c2103cd953c3f1210920c2e8fc6b63ef1 (plain)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
UserinfoEx plugin for Miranda IM

Copyright:
© 2006-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

/**
 * System Includes:
 **/
#include "stdafx.h"

/******************************************************************************************
 * class MTime
 *
 *****************************************************************************************/

/*********************************************
 * construction
 *********************************************/

MTime::MTime() 
{
	ZeroDate();
}

MTime::MTime(SYSTEMTIME &st, bool bIsLocal)
{
	_SysTime = st;
	_isLocal = bIsLocal != FALSE;
}

MTime::MTime(FILETIME &ft, bool bIsLocal)
{
	ZeroDate();
	Set(ft, bIsLocal);
}

MTime::MTime(LARGE_INTEGER &li, bool bIsLocal)
{
	ZeroDate();
	Set(li, bIsLocal);
}

MTime::MTime(DWORD dwStamp)
{
	ZeroDate();
	FromStampAsUTC(dwStamp);
}

MTime::MTime(const MTime& mtime)
{
	Set(mtime);
}

void	MTime::ZeroDate() 
{
	_isLocal = FALSE;
	memset(&_SysTime, 0, sizeof(_SysTime));
}

/*********************************************
 * validation / checks
 *********************************************/

BYTE	MTime::IsValid() const
{
	return (
		_SysTime.wYear > 1600 &&
		_SysTime.wMonth > 0 && _SysTime.wMonth < 13 &&
		_SysTime.wDay > 0 && _SysTime.wDay <= DaysInMonth(_SysTime.wMonth) &&
		_SysTime.wHour < 25 && 
		_SysTime.wMinute < 60 && 
		_SysTime.wSecond < 60 );
}

BYTE	MTime::IsLeapYear() const
{
	return (!(((_SysTime.wYear) % 4 != 0) || (((_SysTime.wYear) % 100 == 0) && ((_SysTime.wYear) % 400 != 0))));
}

LONG	MTime::Compare(const DWORD dwTimeStamp) const
{
	return (LONG)(TimeStamp() - dwTimeStamp);
}

/**
 * name:	Compare
 * desc:	compare a filetime with the value of current object and return difference as number of seconds
 * param:	ft	- FILETIME to compare with
 * return:	number of seconds the ft differs from the class value
 **/ 
LONG	MTime::Compare(const FILETIME &ft) const
{
	const FILETIME ft1 = FileTime();
	return (LONG)((*(__int64*)&ft1 - *(__int64*)&ft) / 10000000i64);
}

/**
 * name:	Compare
 * desc:	compare a systemtime with the value of current object and return difference as number of seconds it handles some strange, too.
 * param:	st	- SYSTEMTIME to compare with
 * return:	number of seconds the st differs from the class value
 **/ 
LONG	MTime::Compare(SYSTEMTIME st) const
{
	FILETIME ft2;

	//strange day-in-month thing
	if (st.wYear == 0) {
		if (Month() < st.wMonth) return -1;
		if (Month() > st.wMonth) return 1;

		MTime mtTmp(st, FALSE);

		mtTmp.Year(Year());
		mtTmp.Day(1);
		mtTmp.Set(mtTmp.FileTime(), FALSE);	//gets the day of week of the first of the month
		mtTmp.Day(1 + (7 + st.wDayOfWeek - mtTmp.DayOfWeek()) % 7);

		//last wDayOfWeek in month
		if (st.wDay == 5) {
			mtTmp.Day(mtTmp.Day() + 7 * 3);	//can't be less than 4 of that day in the month
			if (mtTmp.Day() + 7 <= mtTmp.DaysInMonth(st.wMonth - 1))
				mtTmp.Day(mtTmp.Day() + 7);
		}
		else
			mtTmp.Day(7 * (st.wDay - 1)); //nth of month

		ft2 = mtTmp.FileTime();
	}
	else {
		SystemTimeToFileTime(&st, &ft2);
	}
	return Compare(ft2);
}

/**
 * name:	Compare
 * desc:	compare a MTime with the value of current object and return difference as number of seconds it handles some strange, too.
 * param:	mt	- MTime to compare with
 * return:	number of seconds the st differs from the class value
 **/ 
LONG	MTime::Compare(const MTime &mt) const
{
	return Compare(mt.SystemTime());
}

/*********************************************
 * conversions
 *********************************************/

void	MTime::UTCToLocal()
{
	if (!IsLocal()) {
		TIME_ZONE_INFORMATION tzInfo;
		
		memset(&tzInfo, 0, sizeof(TIME_ZONE_INFORMATION));
		GetTimeZoneInformation(&tzInfo);
		UTCToTzSpecificLocal(&tzInfo);
	}
}

void	MTime::UTCToTzSpecificLocal(int tzh)
{

	TIME_ZONE_INFORMATION tzInfo;

	if (IsLocal()) LocalToUTC();
	memset(&tzInfo, 0, sizeof(TIME_ZONE_INFORMATION));

	if (tzh > 24) tzh = 24;
	if (tzh < -24)tzh = -24;

	GetTimeZoneInformation(&tzInfo);
	tzInfo.Bias = tzh * 30i64;
	UTCToTzSpecificLocal(&tzInfo);
}

LONG	MTime::_Offset(TIME_ZONE_INFORMATION *tzi)
{
	LONG offset = tzi->Bias;
	// daylight saving times
	if (tzi->StandardDate.wMonth != 0) {
		if (tzi->DaylightDate.wMonth < tzi->StandardDate.wMonth) {
			//northern hemisphere
			if (Compare(tzi->DaylightDate) < 0 || Compare(tzi->StandardDate) > 0) 
				offset += tzi->StandardBias;
			else
				offset += tzi->DaylightBias;
		 }
		 else {
			//southern hemisphere
			if (Compare(tzi->StandardDate) < 0 || Compare(tzi->DaylightDate) > 0)
				offset += tzi->DaylightBias;
			else
				offset += tzi->StandardBias;
		 }
	}
	return offset;
}

void	MTime::UTCToTzSpecificLocal(TIME_ZONE_INFORMATION *tzi)
{
	LARGE_INTEGER liFiletime;

	// do not transform to local twice
	if (tzi && !_isLocal) {
		liFiletime = LargeInt();
		liFiletime.QuadPart -= _Offset(tzi) * 60 * 10000000i64;
		Set(liFiletime, TRUE);
	}
}

void	MTime::TzSpecificLocalToUTC(TIME_ZONE_INFORMATION *tzi)
{
	LARGE_INTEGER liFiletime;

	// do not transform to utc twice
	if (tzi && _isLocal) {
		liFiletime = LargeInt();
		liFiletime.QuadPart += _Offset(tzi) * 60 * 10000000i64;
		Set(liFiletime, TRUE);
	}
}

void	MTime::LocalToUTC()
{
	TIME_ZONE_INFORMATION tzInfo;
	
	GetTimeZoneInformation(&tzInfo);
	TzSpecificLocalToUTC(&tzInfo);
}

/*********************************************
 * return values
 *********************************************/

LARGE_INTEGER	MTime::LargeInt() const
{
	LARGE_INTEGER liFileTime = { 0 };

	SystemTimeToFileTime(&_SysTime, (LPFILETIME)&liFileTime);
	return liFileTime;
}

FILETIME		MTime::FileTime() const
{
	FILETIME ftFileTime;

	SystemTimeToFileTime(&_SysTime, &ftFileTime);
	return ftFileTime;
}

DWORD	MTime::TimeStamp() const
{
	LARGE_INTEGER li;

	if (IsLocal()) {
		MTime mt(*this);
		mt.LocalToUTC();
		li = mt.LargeInt();
	}
	else
		li = LargeInt();

	li.QuadPart /= 10000000i64;
	li.QuadPart -= 11644473600i64;

	if (li.QuadPart < 0)
		return 0;

	return (DWORD)li.QuadPart;
}

WORD	MTime::DaysInMonth(const WORD &wMonth)	const
{
	static const WORD wDaysInMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	if (wMonth > 12) return 0;
	return (IsLeapYear() && wMonth == 2) ? wDaysInMonth[wMonth] + 1 : wDaysInMonth[wMonth];
}

WORD	MTime::DaysInYear(BYTE bIgnoreLeap)	const
{
	return ((!bIgnoreLeap && IsLeapYear()) ? 366 : 365); 
};

WORD	MTime::DayOfYear()	const
{
	WORD daysResult = 0;
	WORD i;

	for (i = 0; i < _SysTime.wMonth; i++)
		daysResult += DaysInMonth(i);
	daysResult += _SysTime.wDay;
	return daysResult;
}

WORD	MTime::AdjustYear(const int nDiffDays)
{
	const int nDay = DayOfYear() + nDiffDays;

	if (nDay > DaysInYear())
		return _SysTime.wYear + 1;
	else if (nDay < 0)
		return _SysTime.wYear - 1;
	return _SysTime.wYear;
}

WORD	MTime::TimeFormat(LPTSTR ptszTimeFormat, WORD cchTimeFormat)
{
	if (!ptszTimeFormat || !cchTimeFormat)
		return 0;
	if ((cchTimeFormat = GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &_SysTime, nullptr, ptszTimeFormat, cchTimeFormat)) == 0) {
		*ptszTimeFormat = 0;
		return 0;
	}
	return cchTimeFormat;
}

WORD	MTime::DateFormat(LPTSTR ptszTimeFormat, WORD cchTimeFormat)
{
	if (!ptszTimeFormat || !cchTimeFormat)
		return 0;
	if ((cchTimeFormat = GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &_SysTime, nullptr, ptszTimeFormat, cchTimeFormat)) == 0) {
		*ptszTimeFormat = 0;
		return 0;
	}
	return cchTimeFormat;
}

WORD	MTime::DateFormatAlt(LPTSTR ptszTimeFormat, WORD cchTimeFormat)
{
	if (!ptszTimeFormat || !cchTimeFormat)
		return 0;

	WORD wRes = DateFormat(ptszTimeFormat, cchTimeFormat);
	if (wRes != 0)
		return wRes;

	SYSTEMTIME AltSysTime = _SysTime;
	MTime mtNow;
	mtNow.GetLocalTime();
	AltSysTime.wYear=mtNow.Year();

	if ((cchTimeFormat = GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &AltSysTime, nullptr, ptszTimeFormat, cchTimeFormat)) == 0) {
		*ptszTimeFormat = 0;
		return 0;
	}
	
	wchar_t tszText[10];
	_itow(mtNow.Year(), tszText, 10);
	wchar_t * tszYear = wcsstr(ptszTimeFormat, tszText);

	if (tszYear && mir_wstrlen(tszYear) == 4)
		mir_wstrcpy(tszYear, L"????");
	else {
		*ptszTimeFormat = 0;
		return 0;
	}
	
	return cchTimeFormat;
}

WORD	MTime::DateFormatLong(LPTSTR ptszTimeFormat, WORD cchTimeFormat)
{
	if (!ptszTimeFormat || !cchTimeFormat)
		return 0;
	if ((cchTimeFormat = GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &_SysTime, nullptr, ptszTimeFormat, cchTimeFormat)) == 0) {
		*ptszTimeFormat = 0;
		return 0;
	}
	return cchTimeFormat;
}

/*********************************************
 * set class value
 *********************************************/

void	MTime::FromStampAsUTC(const DWORD dwTimeStamp)
{
	LARGE_INTEGER li;
	li.QuadPart = (dwTimeStamp + 11644473600i64) * 10000000i64;
	Set(li, FALSE);
}

void	MTime::FromStampAsLocal(const DWORD dwTimeStamp)
{
	FromStampAsUTC(dwTimeStamp);
	UTCToLocal();
}

void	MTime::Set(LARGE_INTEGER liFileTime, bool bIsLocal)
{
	if (liFileTime.QuadPart < 0i64) liFileTime.QuadPart = 0;
	FileTimeToSystemTime((LPFILETIME)&liFileTime, &_SysTime);
	_isLocal = bIsLocal != FALSE;
}

void	MTime::Set(const FILETIME &ftFileTime, bool bIsLocal)
{
	FileTimeToSystemTime(&ftFileTime, &_SysTime);
	_isLocal = bIsLocal != FALSE;
}

void	MTime::Set(const MTime &mt)
{
	Set(mt.SystemTime(), mt.IsLocal());
}

void	MTime::Set(const SYSTEMTIME &st, bool bIsLocal)
{
	memcpy(&_SysTime, &st, sizeof(SYSTEMTIME));
	_isLocal = bIsLocal != FALSE;
}

void	MTime::GetTimeUTC()
{
	_isLocal = FALSE;
	::GetSystemTime(&_SysTime);
}

void	MTime::GetLocalTime()
{
	_isLocal = TRUE;
	::GetLocalTime(&_SysTime);
}

void	MTime::GetLocalTime(MCONTACT hContact)
{
	TIME_ZONE_INFORMATION tzi;

	GetTimeUTC();

	if (!GetContactTimeZoneInformation(hContact, (LPARAM)&tzi)) {
		UTCToTzSpecificLocal(&tzi);
	}
}

/*********************************************
 * read and write time to miranda's database
 *********************************************/

int MTime::DBGetStamp  (MCONTACT hContact, LPCSTR pszModule, LPCSTR pszSetting)
{
	if (hContact == INVALID_CONTACT_ID || pszModule == nullptr || pszModule[0] == 0 || pszSetting == nullptr || pszSetting[0] == 0) {
		ZeroDate();
		return 1;
	}

	DWORD dwTimeStamp = db_get_dw(hContact, pszModule, pszSetting, 0);
	if (dwTimeStamp == 0) {
		ZeroDate();
		return 1;
	}
	FromStampAsUTC(dwTimeStamp);
	_isLocal = FALSE;
	return 0;
}

int MTime::DBWriteStamp(MCONTACT hContact, LPCSTR pszModule, LPCSTR pszSetting)
{
	if (hContact == INVALID_CONTACT_ID || pszModule == nullptr || pszModule[0] == 0 || pszSetting == nullptr || pszSetting[0] == 0)
		return 1;

	return db_set_dw(hContact, pszModule, pszSetting, TimeStamp());
}