forked from jasonweiyi/XAPI2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIDGenerator.cpp
More file actions
46 lines (40 loc) · 1.05 KB
/
IDGenerator.cpp
File metadata and controls
46 lines (40 loc) · 1.05 KB
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
#include "stdafx.h"
#include "IDGenerator.h"
#include <stdio.h>
#include <time.h>
#include <string.h>
CIDGenerator::CIDGenerator()
{
m_id = 0;
memset(m_IDString, 0, sizeof(OrderIDType));
memset(m_Prefix, 0, sizeof(OrderIDType));
}
CIDGenerator::~CIDGenerator()
{
}
void CIDGenerator::SetPrefix(const char* prefix)
{
strncpy(m_Prefix, prefix, 32);
}
unsigned int CIDGenerator::GetID()
{
return ++m_id;
}
unsigned int CIDGenerator::GetTimeID()
{
time_t lt;
lt = time(nullptr);
struct tm *ptr;
ptr = localtime(<);
// 1970年开始移动到2015年开始
// long 其实是无符号int,所以*10000会溢出
// *1000 在当天下单数超过1000时才会影响前面的秒
// 如果快速的重复登录,出现第一次已经下到1000笔,第二次登录时在1秒后又下,才会出现ID重复
unsigned int x = ((ptr->tm_wday * 24 + ptr->tm_hour) * 450 + (ptr->tm_min * 60 + ptr->tm_sec)) * 100 + GetID();
return x;
}
const char* CIDGenerator::GetIDString()
{
snprintf(m_IDString, sizeof(m_IDString), "%s:%ld", m_Prefix, ++m_id);
return m_IDString;
}