yy1717
2023-03-31 4bd08f0355b6b2cf3c027202d5ad301b4e182953
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
//
// Created by YY on 2020/1/7.
//
 
#include "xconvert.h"
#include <cstdint>
#include <cstring>
#include <vector>
#include <string>
 
using namespace std;
 
#define SECONDS_PER_MINUTE  60
#define SECONDS_PER_HOUR    3600
#define SECONDS_PER_DAY     (86400L)
 
#define MINUTES_PER_HOUR    60
#define MINUTES_PER_DAY     1440
 
#define HOURS_PER_DAY       24
 
#define DAYS_PER_WEEK       7
#define DAYS_PER_YEAR       365
 
#define DAYS_UP_TO_1970 ((70*365LU) +17)
 
double ConvertKMh2Ms(int kmh)
{
    return ((double)kmh) * 1000.0 / 3600.0;
}
 
double ConvertMs2KMh(double ms)
{
    return ms * 3600.0 / 1000.0;
}
 
void ConvertPhoneNum(uint8_t *dst, int length, const char *src)
{
    int p = length - 1;
    int q = strlen(src) - 1;
 
    memset(dst, 0, length);
 
    while (q >= 0) {
        dst[p] = src[q] - '0';
        q--;
        if (q >= 0) {
            dst[p] |= (src[q] - '0')<<4;
            p--;
            q--;
        }
    }
}
 
void ConvertHex2String(char *str, const uint8_t *hex, int length)
{
    const char HEX[] = "0123456789ABCDEF";
    int j = 0;
 
    for (int i = 0; i < length; ++i) {
        str[j++] = HEX[(hex[i]>>4)&0x0F];
        str[j++] = HEX[hex[i]&0x0F];
    }
    str[j++] = 0;
}
 
void ConvertString2Hex(uint8_t *hex, int length, const char *str)
{
    if (length < strlen(str)/2) return;
 
    for (int i = 0, j = 0; i < strlen(str); ++i, ++j) {
        if (str[i] >= '0' && str[i] <= '9') {
            hex[j] = str[i] - '0';
        } else if (str[i] >= 'A' && str[i] <= 'F') {
            hex[j] = 10 + str[i] - 'A';
        } else if (str[i] >= 'a' && str[i] <= 'f') {
            hex[j] = 10 + str[i] - 'a';
        }
        hex[j] <<= 4;
        i++;
        if (str[i] >= '0' && str[i] <= '9') {
            hex[j] |= str[i] - '0';
        } else if (str[i] >= 'A' && str[i] <= 'F') {
            hex[j] |= 10 + str[i] - 'A';
        } else if (str[i] >= 'a' && str[i] <= 'f') {
            hex[j] |= 10 + str[i] - 'a';
        }
    }
}
 
static int TimeMonthOffset( int leapStatus, uint8_t mon )
{
    const int monthOffsets[2][12] =
            {
/*   jan  feb  mar  apr  may  jun  jul  aug  sep  oct  nov  dec */
                    {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},    /* regular year */
                    {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}     /* leap year    */
            };
    return monthOffsets[leapStatus][mon] ;
}
 
const int LibTimeDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 
static bool TimeYearIsLeap(int year)
{
    if ((year % 4) || year == 2100)
        return false;
    else
        return true;
}
 
uint32_t TimeMakeComposite(int year, int month, int day, int hour, int minute, int second)
{
    int i;
    uint32_t totalSeconds;
    int daysThisYear;
 
    totalSeconds = second;
    totalSeconds += minute*SECONDS_PER_MINUTE;
    totalSeconds += hour*SECONDS_PER_HOUR;
 
    daysThisYear = day - 1;
    if (TimeYearIsLeap(year) && month>2)
        daysThisYear += 1;
 
    for (i = 0; i<(month - 1); i++)
        daysThisYear += (int)LibTimeDays[i];
 
    for (i = 1970; i<year; i++) {
        daysThisYear += 365;
        if (TimeYearIsLeap(i))
            daysThisYear += 1;
    }
    totalSeconds += daysThisYear * SECONDS_PER_DAY;
    return totalSeconds;
}
 
uint32_t TimeMakeComposite(int hour, int minute, int second, int msecond)
{
    return (hour*SECONDS_PER_HOUR + minute*SECONDS_PER_MINUTE + second) * 1000 + msecond;
}
 
void TimeBreakdown(uint32_t SecondsSince1970 , struct TimeStructure *pTS) {
    uint16_t yearSince1900;
    uint32_t days;
    uint32_t daysTillYearStarted;
    uint32_t secs;
    bool isLeapYear;
    uint16_t leapDaysSince1900;
 
    secs = SecondsSince1970;
    days = SecondsSince1970 / SECONDS_PER_DAY;
 
    // 1970.1.1 is Thursday
    pTS->Wday = (days + 4) % DAYS_PER_WEEK;   /* days since Sunday        */
 
    yearSince1900 = (days / DAYS_PER_YEAR) + 70;
 
    leapDaysSince1900 = (yearSince1900 - 1) / 4;
 
    if (yearSince1900 > 200)
        leapDaysSince1900 -= 1;
 
    daysTillYearStarted = leapDaysSince1900 + (365L * yearSince1900);
 
    if (days + DAYS_UP_TO_1970 < daysTillYearStarted) {
        yearSince1900--;
 
        leapDaysSince1900 = (yearSince1900 - 1) / 4;
 
        if (yearSince1900 > 200)
            leapDaysSince1900 -= 1;
        daysTillYearStarted = leapDaysSince1900 + (365L * yearSince1900);
    }
    days = days - (daysTillYearStarted - DAYS_UP_TO_1970);
 
    pTS->Year = yearSince1900 + 1900;
    pTS->Yday = days;
 
    isLeapYear = TimeYearIsLeap(pTS->Year);
 
    pTS->Month = 12;
    do {
        pTS->Month--;
    } while (days < TimeMonthOffset(isLeapYear? 1 : 0, pTS->Month));
 
    pTS->Day = days - TimeMonthOffset(isLeapYear? 1: 0, pTS->Month) + 1;
 
    pTS->Month++;
 
    secs %= SECONDS_PER_DAY;
    pTS->Hour = secs / SECONDS_PER_HOUR;
    secs %= SECONDS_PER_HOUR;
    pTS->Minute = secs / 60;
    pTS->Second = secs % 60;
}
 
vector<string> split(string str, string pattern)
{
    string::size_type pos;
    vector<string> result;
    str += pattern;
 
    int size=str.size();
 
    for(int i=0; i<size; i++) {
        pos=str.find(pattern,i);
        if(pos<size) {
            string s=str.substr(i,pos-i);
            result.push_back(s);
            i=pos+pattern.size()-1;
        }
    }
    return result;
}