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

📄 format_functions.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 3 页
字号:
        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)
    {
        ws_size_t   cchAM;
        ws_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 ws_size_t     cchMarker   =   (am.size() < pm.size()) ? pm.size() : am.size();
    const ws_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':
                case    'H':
                    if( (   'h' == prev ||
                            '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':
                            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 ws_size_t len =   static_cast<ss_size_t>(::lstrlenW(&buffer[0]));

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

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


/** \brief Analogue to the Win32 API <code>GetTimeFormat()</code>, but also
 *    provides milliseconds as part of the time picture.
 *
 * \param locale 
 * \param dwFlags 
 * \param lpTime 
 * \param lpFormat 
 * \param lpTimeStr The buffer into which the result will be written
 * \param cchTime Number of character spaces available in
 *   <code>lpTimeStr</code>. If 0, the required length is returned, and
 *   <code>lpTimeStr</code> is ignored.
 *
 * \return The number of characters written to <code>lpTimeStr</code>
 *    (if <code>0 != cchTime</code>), or required
 *    (if <code>0 == cchTime</code>).
 */
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
{
    WINSTL_ASSERT(0 == cchTime || NULL != lpTimeStr);

#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
{
    WINSTL_ASSERT(0 == cchTime || NULL != lpTimeStr);

#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/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_TIME_HPP_FORMAT_FUNCTIONS */

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

⌨️ 快捷键说明

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