CivCommonUtils.cpp 2.72 KB
Newer Older
刘乐's avatar
刘乐 committed
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
#include "CivCommonUtils.h"
#include <Windows.h>
#include <wchar.h>
#include <time.h> 


std::string string_To_UTF8(const std::string& str)
{
    int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
    wchar_t* pwBuf = new wchar_t[nwLen + 1];    //一定要加1,不然会出现尾巴 

    ZeroMemory(pwBuf, nwLen * 2 + 2);
    ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
    int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

    char* pBuf = new char[nLen + 1];
    ZeroMemory(pBuf, nLen + 1);
    ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

    std::string strRet(pBuf);

    delete[]pwBuf;
    delete[]pBuf;
    pwBuf = NULL;
    pBuf = NULL;

    return strRet;
}


std::string UTF8_To_string(const std::string& str)
{
    int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
    wchar_t* pwBuf = new wchar_t[nwLen + 1];    //一定要加1,不然会出现尾巴 
    memset(pwBuf, 0, nwLen * 2 + 2);
    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
    int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
    char* pBuf = new char[nLen + 1];
    memset(pBuf, 0, nLen + 1);
    WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

    std::string strRet = pBuf;

    delete[]pBuf;
    delete[]pwBuf;
    pBuf = NULL;
    pwBuf = NULL;

    return strRet;
}

//std::map<std::string, std::string>  CivCommonUtils::parseJson(std::string jsonStr)
//{
//    std::map<std::string, std::string> map;
//
//    return map;
//}

std::string  currentTime()
{
    time_t rawtime;
    struct tm* timeinfo;
    char s[100];
    time(&rawtime);

    timeinfo = localtime(&rawtime);
    time_t tick = mktime(timeinfo);
    strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", timeinfo);

    return s;
}

std::string  currentDate()
{
    time_t rawtime;
    struct tm* timeinfo;
    char s[100];
    time(&rawtime);

    timeinfo = localtime(&rawtime);
    time_t tick = mktime(timeinfo);
    strftime(s, sizeof(s), "%Y-%m-%d", timeinfo);

    return s;
}

std::string currentHour()
{
    time_t rawtime;
    struct tm* timeinfo;
    char s[100];
    time(&rawtime);

    timeinfo = localtime(&rawtime);
    time_t tick = mktime(timeinfo);
    strftime(s, sizeof(s), "%H", timeinfo);

    std::string res = lstrip(s,'0');

    return res;
}

std::string lstrip(char aa[], char c)
{
    int i = 0, j = 0, k = 0, z = 0;

    k = strlen(aa);
    z = k - 1;
    while (aa[z] == c)
    {
        --z;
    }
    int x = 0, y = 0;
    while (aa[y] == c)
    {
        y++;
    }

    char bb[256];
    int  leng = 0;
    for (x = y; x < z + 1; x++)
    {
        bb[leng] = aa[x];
    }
    std::string res = bb;
    return res;
}