time_string_access.hpp

来自「用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、W」· HPP 代码 · 共 522 行 · 第 1/2 页

HPP
522
字号
/* /////////////////////////////////////////////////////////////////////////
 * File:        winstl/time_string_access.hpp
 *
 * Purpose:     Helper functions for the SYSTEMTIME and FILETIME structures.
 *
 * Created:     2nd December 2004
 * Updated:     10th June 2006
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 2004-2006, Matthew Wilson and Synesis Software
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
 *   any contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * ////////////////////////////////////////////////////////////////////// */


/// \file winstl/time_string_access.hpp
///
/// Helper functions for the SYSTEMTIME and FILETIME structures.

#ifndef WINSTL_INCL_WINSTL_HPP_TIME_STRING_ACCESS
#define WINSTL_INCL_WINSTL_HPP_TIME_STRING_ACCESS

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_HPP_TIME_STRING_ACCESS_MAJOR     1
# define WINSTL_VER_WINSTL_HPP_TIME_STRING_ACCESS_MINOR     3
# define WINSTL_VER_WINSTL_HPP_TIME_STRING_ACCESS_REVISION  5
# define WINSTL_VER_WINSTL_HPP_TIME_STRING_ACCESS_EDIT      28
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SHIM_STRING
# include <stlsoft/string/shim_string.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_SHIM_STRING */
#ifndef WINSTL_INCL_WINSTL_H_TIME_FORMAT_FUNCTIONS
# include <winstl/time_format_functions.h>
#endif /* !WINSTL_INCL_WINSTL_H_TIME_FORMAT_FUNCTIONS */
#ifndef STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS
# include <stlsoft/string_access.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS */

/* /////////////////////////////////////////////////////////////////////////
 * Namespace
 */

#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
     defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */

namespace stlsoft
{

namespace winstl_project
{

# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * Helper Functions
 */

inline SYSTEMTIME FILETIME2SYSTEMTIME(FILETIME const &ft)
{
    SYSTEMTIME  st;

    FileTimeToSystemTime(&ft, &st);

    return st;
}

template <ss_typename_param_k S>
inline void stream_insert(S &stm, SYSTEMTIME const &t)
{
    typedef stlsoft_ns_qual(basic_shim_string)<ws_char_a_t>     string_t;

    const int       cchDate     =   ::GetDateFormatA(LOCALE_USER_DEFAULT, 0, &t, NULL, NULL, 0);
    const int       cchTime     =   ::GetTimeFormatA(LOCALE_USER_DEFAULT, 0, &t, NULL, NULL, 0);
    const size_t    cchTotal    =   static_cast<size_t>((cchDate - 1) + 1 + (cchTime - 1));  // GetDateFormat() + space + GetTimeFormat() (subtracting 1 for terminating NUL in each count returned)
    string_t        s(cchTotal);

    if(cchTotal == s.size())
    {
        ::GetDateFormatA(LOCALE_USER_DEFAULT, 0, &t, NULL, &s.data()[0], cchDate);
        s.data()[cchDate - 1] = ' ';
        ::GetTimeFormatA(LOCALE_USER_DEFAULT, 0, &t, NULL, &s.data()[0] + cchDate, cchTime);
    }

    stm << s.data();
}

template <ss_typename_param_k S>
inline void stream_insert(S &s, FILETIME const &ft)
{
    stream_insert(s, FILETIME2SYSTEMTIME(ft));
}

/* /////////////////////////////////////////////////////////////////////////
 * Shims
 */

/* /////////////////////////////////////////////////////////////////////////
 * c_str_ptr
 */

inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_ptr_a(SYSTEMTIME const &t, ws_bool_t bMilliseconds = false)
{
    typedef stlsoft_ns_qual(basic_shim_string)<ws_char_a_t>     string_t;

    int (STLSOFT_STDCALL *pfnGetTimeFormatA)(   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

                                            =   bMilliseconds ? GetTimeFormat_msA : ::GetTimeFormatA;

    const int       cchDate     =   ::GetDateFormatA(LOCALE_USER_DEFAULT, 0, &t, NULL, NULL, 0);
    const int       cchTime     =   pfnGetTimeFormatA(LOCALE_USER_DEFAULT, 0, &t, NULL, NULL, 0);
    const size_t    cchTotal    =   static_cast<size_t>((cchDate - 1) + 1 + (cchTime - 1));  // GetDateFormat() + space + GetTimeFormat() (subtracting 1 for terminating NUL in each count returned)
    string_t        s(cchTotal);

    if(cchTotal == s.size())
    {
        ::GetDateFormatA(LOCALE_USER_DEFAULT, 0, &t, NULL, &s.data()[0], cchDate);
        s.data()[cchDate - 1] = ' ';
        pfnGetTimeFormatA(LOCALE_USER_DEFAULT, 0, &t, NULL, &s.data()[0] + cchDate, cchTime);
    }

    return s;
}

inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_ptr_w(SYSTEMTIME const &t, ws_bool_t bMilliseconds = false)
{
    typedef stlsoft_ns_qual(basic_shim_string)<ws_char_w_t>     string_t;

    int (STLSOFT_STDCALL *pfnGetTimeFormatW)(   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

                                            =   bMilliseconds ? GetTimeFormat_msW : ::GetTimeFormatW;

    const int       cchDate     =   ::GetDateFormatW(LOCALE_USER_DEFAULT, 0, &t, NULL, NULL, 0);
    const int       cchTime     =   pfnGetTimeFormatW(LOCALE_USER_DEFAULT, 0, &t, NULL, NULL, 0);
    const size_t    cchTotal    =   static_cast<size_t>((cchDate - 1) + 1 + (cchTime - 1));  // GetDateFormat() + space + GetTimeFormat() (subtracting 1 for terminating NUL in each count returned)
    string_t        s(cchTotal);

    if(cchTotal == s.size())
    {
        ::GetDateFormatW(LOCALE_USER_DEFAULT, 0, &t, NULL, &s.data()[0], cchDate);
        s.data()[cchDate - 1] = ' ';
        pfnGetTimeFormatW(LOCALE_USER_DEFAULT, 0, &t, NULL, &s.data()[0] + cchDate, cchTime);
    }

    return s;
}

inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_ptr(SYSTEMTIME const &t, ws_bool_t bMilliseconds = false)
{
#ifdef UNICODE
    return c_str_ptr_w(t, bMilliseconds);
#else /* ? UNICODD */
    return c_str_ptr_a(t, bMilliseconds);
#endif /* UNICODD */
}

inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_ptr_a(FILETIME const &t, ws_bool_t bMilliseconds = false)
{
    return c_str_ptr_a(FILETIME2SYSTEMTIME(t), bMilliseconds);
}

inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_ptr_w(FILETIME const &t, ws_bool_t bMilliseconds = false)
{
    return c_str_ptr_w(FILETIME2SYSTEMTIME(t), bMilliseconds);
}

inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_ptr(FILETIME const &t, ws_bool_t bMilliseconds = false)
{
    return c_str_ptr(FILETIME2SYSTEMTIME(t), bMilliseconds);
}

/* /////////////////////////////////////////////////////////////////////////
 * c_str_data
 */

inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_data_a(SYSTEMTIME const &t, ws_bool_t bMilliseconds = false)
{
    return c_str_ptr_a(t, bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_data_w(SYSTEMTIME const &t, ws_bool_t bMilliseconds = false)
{
    return c_str_ptr_w(t, bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_data(SYSTEMTIME const &t, ws_bool_t bMilliseconds = false)
{
#ifdef UNICODE
    return c_str_data_w(t, bMilliseconds);
#else /* ? UNICODD */
    return c_str_data_a(t, bMilliseconds);
#endif /* UNICODD */
}

inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_data_a(FILETIME const &t, ws_bool_t bMilliseconds = false)
{
    return c_str_ptr_a(FILETIME2SYSTEMTIME(t), bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_data_w(FILETIME const &t, ws_bool_t bMilliseconds = false)
{
    return c_str_ptr_w(FILETIME2SYSTEMTIME(t), bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_data(FILETIME const &t, ws_bool_t bMilliseconds = false)
{
    return c_str_ptr(FILETIME2SYSTEMTIME(t), bMilliseconds);
}

/* /////////////////////////////////////////////////////////////////////////
 * c_str_ptr_null
 */

inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_ptr_null_a(SYSTEMTIME const &t, ws_bool_t bMilliseconds = false)
{
    return c_str_ptr_a(t, bMilliseconds);

⌨️ 快捷键说明

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