summaryrefslogtreecommitdiff
path: root/protocols/MRA/src/Sdk/InternetTime.h
blob: 865d02a3e53cc5a17b6954fe0515f50b7ef5576f (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
/*
 * Copyright (c) 2003 Rozhuk Ivan <rozhuk.im@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */



#if !defined(AFX_INTERNET_TIME_H__F58D13FF_F6F2_476C_B8F0_7B9E9357CF48__INCLUDED_)
#define AFX_INTERNET_TIME_H__F58D13FF_F6F2_476C_B8F0_7B9E9357CF48__INCLUDED_


#pragma once

#include "Sdk/StrToNum.h"
#include "Sdk/MemoryFind.h"
#include "Sdk/MemoryFindByte.h"

typedef struct
{
	LONG		lTimeZone;
	SYSTEMTIME	stTime;
} INTERNET_TIME;


static LPCSTR lpcszenmMonthEnum[13]=	{"---","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
static LPCSTR lpcszenmDayOfWeakEnum[7]=	{"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

#define CRLF "\r\n"

__inline BOOL SkeepSPWSP(LPCSTR lpszBuff,size_t dwBuffSize,LPSTR *plpszRetBuff,size_t *pdwRetBuffSize)
{
	BOOL bRet=TRUE;

	if (lpszBuff && dwBuffSize && (plpszRetBuff || pdwRetBuffSize))
	{
		while ((*lpszBuff)<33 && dwBuffSize)
		{
			dwBuffSize--;
			lpszBuff++;
		}

		if (plpszRetBuff)	(*plpszRetBuff)=(LPSTR)lpszBuff;
		if (pdwRetBuffSize)	(*pdwRetBuffSize)=dwBuffSize;
	}else{
		bRet=FALSE;
	}
return(bRet);
}


__inline BOOL WSP2SP(LPCSTR lpszBuff,size_t dwBuffSize,LPSTR lpszRetBuff,size_t *pdwRetBuffSize)
{//	WSP->SP
	BOOL bRet=TRUE;

	if (lpszBuff && dwBuffSize && lpszRetBuff)
	{
		LPSTR lpszCurReadPos,pCRLF,lpszCurWritePos;
		size_t dwToCopy,dwRetBuffSize;

		pCRLF=(LPSTR)lpszBuff;
		lpszCurReadPos=(LPSTR)lpszBuff;
		lpszCurWritePos=lpszRetBuff;
		dwRetBuffSize=0;

		while(pCRLF)
		{
			pCRLF=(LPSTR)MemoryFind((pCRLF-lpszBuff),lpszBuff,(dwBuffSize-1),CRLF,2);
			if (pCRLF)
			{
				pCRLF+=2;
				if ((*pCRLF)==9 || (*pCRLF)==32)// LWS: <US-ASCII HT, horizontal-tab (9)> || <US-ASCII SP, space (32)>
				{
					dwToCopy=((pCRLF-2)-lpszCurReadPos);
					pCRLF++;

					memmove((LPVOID)lpszCurWritePos,(CONST VOID*)lpszCurReadPos,dwToCopy);
					dwRetBuffSize+=(dwToCopy+1);
					lpszCurWritePos+=dwToCopy;
					lpszCurWritePos[0]=32;
					lpszCurWritePos++;
					lpszCurReadPos=pCRLF;
				}
			}else{
				dwToCopy=((lpszBuff+dwBuffSize)-lpszCurReadPos);
				dwRetBuffSize+=dwToCopy;
				memmove((LPVOID)lpszCurWritePos,(CONST VOID*)lpszCurReadPos,dwToCopy);
			}
		}

		if (pdwRetBuffSize) (*pdwRetBuffSize)=dwRetBuffSize;
	}else{
		bRet=FALSE;
	}
return(bRet);
}

__inline void InternetTimeGetCurrentTime(INTERNET_TIME *pitTime)
{
	TIME_ZONE_INFORMATION tzi={0};
	GetTimeZoneInformation(&tzi);
	pitTime->lTimeZone=tzi.Bias;
	GetSystemTime(&pitTime->stTime);
}

//	Переводит время из MAILTIME в строковое
__inline CMStringA InternetTimeGetString(INTERNET_TIME *pitTime)
{
	char lpszBuff[100];
	LPSTR lpszCurPos = lpszBuff;
	size_t dwTimeLen = 0, dwtm;

	// day of weak// date of mounth// mounth name// year// hours // minutes// seconds
	dwtm = wsprintfA(lpszCurPos, "%s, %02lu %s %04lu %02lu:%02lu:%02lu ",
		lpcszenmDayOfWeakEnum[pitTime->stTime.wDayOfWeek],
		pitTime->stTime.wDay,
		lpcszenmMonthEnum[pitTime->stTime.wMonth],
		pitTime->stTime.wYear,
		pitTime->stTime.wHour,
		pitTime->stTime.wMinute,
		pitTime->stTime.wSecond);
	lpszCurPos += dwtm;
	dwTimeLen += dwtm;

	// time zone
	if (pitTime->lTimeZone) {
		if (pitTime->lTimeZone < 0) { // нужно добавить плюсик, минус добавляется автоматом
			(*((BYTE*)lpszCurPos)) = '+';
			lpszCurPos ++;
			dwTimeLen ++;
		}

		dwtm = wsprintfA(lpszCurPos, "%04ld", -(((pitTime->lTimeZone / 60) * 100) + pitTime->lTimeZone % 60));
		lpszCurPos += dwtm;
		dwTimeLen += dwtm;
	} else {
		dwtm = wsprintfA(lpszCurPos, "GMT");
		lpszCurPos += dwtm;
		dwTimeLen += dwtm;
	}

	return lpszBuff;
}

//	Переводит время из строкового в INTERNET_TIME
__inline DWORD InternetTimeGetTime(const CMStringA &lpszTime, INTERNET_TIME &pitTime)
{
	if (lpszTime.IsEmpty())
		return ERROR_INVALID_HANDLE;
	
	// = Thu, 21 May 1998 05:33:29 -0700 =
	char sztmBuff[4096];
	LPSTR lpszCurPos=sztmBuff,lpszTemp;
	size_t dwCurSize=4096,dwTemp;

	memset(&pitTime, 0, sizeof(INTERNET_TIME));
	WSP2SP(lpszTime, lpszTime.GetLength(), lpszCurPos, &dwCurSize);

	if (dwCurSize > 3) { // день недели
		if (lpszCurPos[3] == ',') {
			for (unsigned short i=0; i < 7; i++) {
				if ( !_memicmp(lpcszenmDayOfWeakEnum[i], lpszCurPos, 3)) {
					pitTime.stTime.wDayOfWeek = i;
					break;
				}
			}

			lpszCurPos+=4;
			dwCurSize-=4;
		}

		if (dwCurSize>2) { // день месяца
			SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);
			if ((lpszTemp = (LPSTR)MemoryFindByte(0, lpszCurPos, dwCurSize,' '))) {
				dwTemp=(lpszTemp-lpszCurPos);
				pitTime.stTime.wDay=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

				lpszCurPos=(lpszTemp+1);
				dwCurSize-=(dwTemp+1);

				if (dwCurSize > 3) { // месяц
					SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);

					for (unsigned short i=1; i < 13; i++)
						if ( !_memicmp(lpcszenmMonthEnum[i], lpszCurPos, 3)) {
							pitTime.stTime.wMonth = i;
							break;
						}

					lpszCurPos += 3;
					dwCurSize -= 3;

					if (dwCurSize > 3) { // год
						SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);
						if ((lpszTemp = (LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,' '))) {
							dwTemp=(lpszTemp-lpszCurPos);
							pitTime.stTime.wYear=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

							lpszCurPos=(lpszTemp+1);
							dwCurSize-=(dwTemp+1);

							if (dwCurSize > 2) { // часы
								SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);
								if ((lpszTemp = (LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,':'))) {
									dwTemp=(lpszTemp-lpszCurPos);
									pitTime.stTime.wHour=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

									lpszCurPos=(lpszTemp+1);
									dwCurSize-=(dwTemp+1);

									if (dwCurSize > 2) { // минуты
										SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);
										if ((lpszTemp=(LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,':')))
										{
											dwTemp=(lpszTemp-lpszCurPos);
											pitTime.stTime.wMinute=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

											lpszCurPos = (lpszTemp+1);
											dwCurSize -= (dwTemp+1);

											if (dwCurSize > 2) { // секунды, они есть
												if ((lpszTemp = (LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,' '))) {
													dwTemp=(lpszTemp-lpszCurPos);
													pitTime.stTime.wSecond=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

													lpszCurPos=(lpszTemp+1);
													dwCurSize-=(dwTemp+1);
												}
											}
											else {// зоны нет
												if (dwCurSize) {
													pitTime.stTime.wSecond=(unsigned short)StrToUNum32(lpszCurPos,dwCurSize);
													lpszCurPos+=dwCurSize;
													dwCurSize=0;
												}
											}
										}
										else {
											if ((lpszTemp = (LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,' '))) {
												dwTemp = (lpszTemp-lpszCurPos);
												pitTime.stTime.wMinute = (unsigned short)StrToUNum32(lpszCurPos,dwTemp);

												lpszCurPos = (lpszTemp+1);
												dwCurSize -= (dwTemp+1);
											}
										}

										if (dwCurSize) { // часовой пояс
											SkeepSPWSP(lpszCurPos, dwCurSize, &lpszCurPos, &dwCurSize);
											pitTime.lTimeZone = (LONG)StrToNum(lpszCurPos, dwCurSize);
											if (pitTime.lTimeZone > 1300 || pitTime.lTimeZone < -1200)
												pitTime.lTimeZone = 2400;
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	return NO_ERROR;
}



#endif // !defined(AFX_INTERNET_TIME_H__F58D13FF_F6F2_476C_B8F0_7B9E9357CF48__INCLUDED_)