⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 time_format_functions.h

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 H
📖 第 1 页 / 共 2 页
字号:
    ws_char_a_t         seconds_[]  =   "00.000";

    ws_char_a_t const   *hours      =   stlsoft_ns_qual(integer_to_string)(&hours_[0], STLSOFT_NUM_ELEMENTS(hours_), lpTime->wHour);
    ws_char_a_t const   *minutes    =   stlsoft_ns_qual(integer_to_string)(&minutes_[0], STLSOFT_NUM_ELEMENTS(minutes_), lpTime->wMinute);
                                        stlsoft_ns_qual(integer_to_string)(&seconds_[3], 4, lpTime->wMilliseconds);
    ws_char_a_t const   *seconds    =   stlsoft_ns_qual(integer_to_string)(&seconds_[0], 3, lpTime->wSecond);

    seconds_[2] = '.';

    // Get the time markers
    HKEY        hkey;
    LONG        res =   ::RegOpenKey(HKEY_CURRENT_USER, "Control Panel\\International", &hkey);
    buffer_t    am(0);
    buffer_t    pm(0);

    if(ERROR_SUCCESS == res)
    {
        size_t  cchAM   =   0;
        size_t  cchPM   =   0;
        LONG    r;

        if( ERROR_SUCCESS != (r = reg_get_string_value(hkey, "s1159", static_cast<ws_char_a_t*>(NULL), cchAM)) ||
            ERROR_SUCCESS != (r = (am.resize(cchAM), cchAM = am.size(), reg_get_string_value(hkey, "s1159", &am[0], cchAM))))
        {
            res = r;
        }
        else if(ERROR_SUCCESS != (r = reg_get_string_value(hkey, "s2359", static_cast<ws_char_a_t*>(NULL), cchPM)) ||
                ERROR_SUCCESS != (r = (pm.resize(cchPM), cchPM = pm.size(), reg_get_string_value(hkey, "s2359", &pm[0], cchPM))))
        {
            res = r;
        }

        ::RegCloseKey(hkey);
    }

    if(ERROR_SUCCESS != res)
    {
        am.resize(3);
        pm.resize(3);

        ::lstrcpyA(&am[0], "AM");
        ::lstrcpyA(&pm[0], "PM");
    }

    ws_char_a_t const   *timeMarker =   (lpTime->wHour < 12) ? &am[0] : &pm[0];
    const size_t        cchMarker   =   (am.size() < pm.size()) ? pm.size() : am.size();
    const size_t        cchTimeMax  =   (cchPicture - 1) + (2 - 1) + (2 - 1) + (6 - 1) + 1 + cchMarker;
    buffer_t            buffer(1 + cchTimeMax);
    size_t              len         =   0;

    if(!buffer.empty())
    {
        ws_char_a_t const   *r;
        ws_char_a_t         *w          =   &buffer[0];
        ws_char_a_t         prev        =   '\0';
        ws_bool_t           bMarker1    =   true;

        for(r = lpFormat; r != lpFormat + cchPicture; ++r)
        {
            const ws_char_a_t   ch  =   *r;

            switch(ch)
            {
                case    'h':
                    if( 'h' == prev &&
                        hours != &hours_[0])
                    {
                        --hours;
                    }
                    break;
                case    'm':
                    if( 'm' == prev &&
                        minutes != &minutes_[0])
                    {
                        --minutes;
                    }
                    break;
                case    's':
                    if( 's' == prev &&
                        seconds != &seconds_[0])
                    {
                        --seconds;
                    }
                    break;
                case    't':
                    if('t' == prev)
                    {
                        bMarker1 = false;
                    }
                    break;
                default:
                    {
                        ws_char_a_t const *p;

                        switch(prev)
                        {
                            case    'h':    p = hours;          break;
                            case    'm':    p = minutes;        break;
                            case    's':    p = seconds;        break;
                            case    't':
                                if(0 == (dwFlags & TIME_NOTIMEMARKER))
                                {
                                    if(!bMarker1)
                                    {
                                        p = timeMarker;
                                        break;
                                    }
                                    else
                                    {
                                        // Fall through
                                        *w++ = *timeMarker;
                                        ++len;
                                    }
                                }
                                // Fall through
                            default:        p = "";             break;
                        }

                        for(; '\0' != *p; *w++ = *p++, ++len)
                        {}
                    }
                    *w++ = ch;
                    ++len;
                    break;
            }

            if('\0' == ch)
            {
                break;
            }

            prev = ch;
        }
    }

    // If 0 was specified, or

    if( 0 == cchTime ||
        len <= size_t(cchTime))
    {
        if(0 != cchTime)
        {
            ::lstrcpyA(lpTimeStr, &buffer[0]);
        }

        return static_cast<int>(len);
    }
    else
    {
        return 0;
    }
}

inline int STLSOFT_STDCALL GetTimeFormat_msW_(  LCID                locale      // locale
                                            ,   DWORD               dwFlags     // options
                                            ,   CONST SYSTEMTIME    *lpTime     // time
                                            ,   ws_char_w_t const   *lpFormat   // time format string
                                            ,   ws_char_w_t         *lpTimeStr  // formatted string buffer
                                            ,   const int           cchTime)    // size of string buffer
{
    typedef stlsoft_ns_qual(auto_buffer_old)<   ws_char_w_t
                                            ,   processheap_allocator<ws_char_w_t>
                                            >                               buffer_t;

    if(dwFlags & (TIME_NOMINUTESORSECONDS | TIME_NOSECONDS))
    {
        return ::GetTimeFormatW(locale, dwFlags, lpTime, lpFormat, lpTimeStr, cchTime);
    }

    if(dwFlags & LOCALE_NOUSEROVERRIDE)
    {
        locale = LOCALE_SYSTEM_DEFAULT;
    }

    buffer_t            timePicture(1 + (NULL == lpFormat ? static_cast<ss_size_t>(::GetLocaleInfoW(locale, LOCALE_STIMEFORMAT, NULL, 0)) : 0));
    ss_size_t           cchPicture;

    if(NULL == lpFormat)
    {
        ::GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, &timePicture[0], static_cast<int>(timePicture.size()));
        lpFormat = &timePicture[0];
        cchPicture = timePicture.size() - 1;
    }
    else
    {
        cchPicture = static_cast<ss_size_t>(::lstrlenW(lpFormat));
    }

    ws_char_w_t         hours_[]    =   L"00";
    ws_char_w_t         minutes_[]  =   L"00";
    ws_char_w_t         seconds_[]  =   L"00.000";

    ws_char_w_t const   *hours      =   stlsoft_ns_qual(integer_to_string)(&hours_[0], STLSOFT_NUM_ELEMENTS(hours_), lpTime->wHour);
    ws_char_w_t const   *minutes    =   stlsoft_ns_qual(integer_to_string)(&minutes_[0], STLSOFT_NUM_ELEMENTS(minutes_), lpTime->wMinute);
                                        stlsoft_ns_qual(integer_to_string)(&seconds_[3], 4, lpTime->wMilliseconds);
    ws_char_w_t const   *seconds    =   stlsoft_ns_qual(integer_to_string)(&seconds_[0], 3, lpTime->wSecond);

    seconds_[2] = '.';

    // Get the time markers
    HKEY        hkey;
    LONG        res =   ::RegOpenKey(HKEY_CURRENT_USER, "Control Panel\\International", &hkey);
    buffer_t    am(0);
    buffer_t    pm(0);

    if(ERROR_SUCCESS == res)
    {
        size_t  cchAM;
        size_t  cchPM;
        LONG    r;

        if( ERROR_SUCCESS != (r = reg_get_string_value(hkey, L"s1159", static_cast<ws_char_w_t*>(NULL), cchAM)) ||
            ERROR_SUCCESS != (r = (am.resize(cchAM), cchAM = am.size(), reg_get_string_value(hkey, L"s1159", &am[0], cchAM))))
        {
            res = r;
        }
        else if(ERROR_SUCCESS != (r = reg_get_string_value(hkey, L"s2359", static_cast<ws_char_w_t*>(NULL), cchPM)) ||
                ERROR_SUCCESS != (r = (pm.resize(cchPM), cchPM = pm.size(), reg_get_string_value(hkey, L"s2359", &pm[0], cchPM))))
        {
            res = r;
        }

        ::RegCloseKey(hkey);
    }

    if(ERROR_SUCCESS != res)
    {
        am.resize(3);
        pm.resize(3);

        ::lstrcpyW(&am[0], L"AM");
        ::lstrcpyW(&pm[0], L"PM");
    }

    ws_char_w_t const   *timeMarker =   (lpTime->wHour < 12) ? &am[0] : &pm[0];
    const size_t        cchMarker   =   (am.size() < pm.size()) ? pm.size() : am.size();
    const size_t        cchTimeMax  =   (cchPicture - 1) + (2 - 1) + (2 - 1) + (6 - 1) + 1 + cchMarker;
    buffer_t            buffer(1 + cchTimeMax);

    if(!buffer.empty())
    {
        ws_char_w_t const   *r;
        ws_char_w_t         *w          =   &buffer[0];
        ws_char_w_t         prev        =   '\0';
        ws_bool_t           bMarker1    =   true;

        for(r = lpFormat; r != lpFormat + cchPicture; ++r)
        {
            const ws_char_w_t   ch  =   *r;

            switch(ch)
            {
                case    'h':
                    if( 'h' == prev &&
                        hours != &hours_[0])
                    {
                        --hours;
                    }
                    break;
                case    'm':
                    if( 'm' == prev &&
                        minutes != &minutes_[0])
                    {
                        --minutes;
                    }
                    break;
                case    's':
                    if( 's' == prev &&
                        seconds != &seconds_[0])
                    {
                        --seconds;
                    }
                    break;
                case    't':
                    if('t' == prev)
                    {
                        bMarker1 = false;
                    }
                    break;
                default:
                    {
                        ws_char_w_t const *p;

                        switch(prev)
                        {
                            case    'h':    p = hours;          break;
                            case    'm':    p = minutes;        break;
                            case    's':    p = seconds;        break;
                            case    't':
                                if(0 == (dwFlags & TIME_NOTIMEMARKER))
                                {
                                    if(!bMarker1)
                                    {
                                        p = timeMarker;
                                        break;
                                    }
                                    else
                                    {
                                        // Fall through
                                        *w++ = *timeMarker;
                                    }
                                }
                                // Fall through
                            default:        p = L"";             break;
                        }

                        for(; '\0' != *p; *w++ = *p++)
                        {}
                    }
                    *w++ = ch;
                    break;
            }

            if('\0' == ch)
            {
                break;
            }

            prev = ch;
        }
    }

    const size_t    len =   static_cast<ss_size_t>(::lstrlenW(&buffer[0]));

    if( 0 == cchTime ||
        size_t(cchTime) <= len + 1)
    {
        if(0 != cchTime)
        {
            ::lstrcpyW(lpTimeStr, &buffer[0]);
        }

        return static_cast<int>(len + 1);
    }
    else
    {
        return 0;
    }
}
#endif /* 0 */

inline int STLSOFT_STDCALL GetTimeFormat_msA(   LCID                locale      // locale
                                            ,   DWORD               dwFlags     // options
                                            ,   CONST SYSTEMTIME    *lpTime     // time
                                            ,   ws_char_a_t const   *lpFormat   // time format string
                                            ,   ws_char_a_t         *lpTimeStr  // formatted string buffer
                                            ,   int                 cchTime)    // size of string buffer
{
#if 0
    return GetTimeFormat_msA_(locale, dwFlags, lpTime, lpFormat, lpTimeStr, cchTime);
#else /* ? 0 */
    return GetTimeFormat_ms_<ws_char_a_t>(locale, dwFlags, lpTime, lpFormat, lpTimeStr, cchTime);
#endif /* 0 */
}

inline int STLSOFT_STDCALL GetTimeFormat_msW(   LCID                locale      // locale
                                            ,   DWORD               dwFlags     // options
                                            ,   CONST SYSTEMTIME    *lpTime     // time
                                            ,   ws_char_w_t const   *lpFormat   // time format string
                                            ,   ws_char_w_t         *lpTimeStr  // formatted string buffer
                                            ,   int                 cchTime)    // size of string buffer
{
#if 0
    return GetTimeFormat_msW_(locale, dwFlags, lpTime, lpFormat, lpTimeStr, cchTime);
#else /* ? 0 */
    return GetTimeFormat_ms_<ws_char_w_t>(locale, dwFlags, lpTime, lpFormat, lpTimeStr, cchTime);
#endif /* 0 */
}

////////////////////////////////////////////////////////////////////////////
// Unit-testing

#ifdef STLSOFT_UNITTEST
# include "./unittest/time_format_functions_unittest_.h"
#endif /* STLSOFT_UNITTEST */

/* ////////////////////////////////////////////////////////////////////// */

#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} /* namespace winstl */
# else
} /* namespace winstl_project */
} /* namespace stlsoft */
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* ////////////////////////////////////////////////////////////////////// */

#endif /* !WINSTL_INCL_WINSTL_H_TIME_FORMAT_FUNCTIONS */

/* ////////////////////////////////////////////////////////////////////// */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -