📄 time.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: winstl/shims/access/string/time.hpp
*
* Purpose: Helper functions for the SYSTEMTIME and FILETIME structures.
*
* Created: 2nd December 2004
* Updated: 22nd March 2007
*
* Thanks to: David Wang, for spotting an error in one of the shim
* functions.
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2004-2007, 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/shims/access/string/time.hpp
*
* \brief [C++] Definition of the string access shims for
* <code>FILETIME</code> and <code>SYSTEMTIME</code>
* (\ref group__concept__shim__string_access "String Access Shims" Concept).
*/
#ifndef WINSTL_INCL_WINSTL_SHIMS_ACCESS_STRING_HPP_TIME
#define WINSTL_INCL_WINSTL_SHIMS_ACCESS_STRING_HPP_TIME
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_SHIMS_ACCESS_STRING_HPP_TIME_MAJOR 2
# define WINSTL_VER_WINSTL_SHIMS_ACCESS_STRING_HPP_TIME_MINOR 3
# define WINSTL_VER_WINSTL_SHIMS_ACCESS_STRING_HPP_TIME_REVISION 2
# define WINSTL_VER_WINSTL_SHIMS_ACCESS_STRING_HPP_TIME_EDIT 48
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#ifndef WINSTL_INCL_WINSTL_SHIMS_CONVERSION_TO_SYSTEMTIME_HPP_FILETIME
# include <winstl/shims/conversion/to_SYSTEMTIME/FILETIME.hpp>
#endif /* !WINSTL_INCL_WINSTL_SHIMS_CONVERSION_TO_SYSTEMTIME_HPP_FILETIME */
#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_TIME_HPP_FORMAT_FUNCTIONS
# include <winstl/time/format_functions.hpp>
#endif /* !WINSTL_INCL_WINSTL_TIME_HPP_FORMAT_FUNCTIONS */
#ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
# include <stlsoft/shims/access/string.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
#ifdef WINSTL_UDATE_DEFINED
# include <objbase.h>
# include <oleauto.h>
#endif /* WINSTL_UDATE_DEFINED */
/* /////////////////////////////////////////////////////////////////////////
* 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
*/
/** \brief [DEPRECATED] Converts a <code>FILETIME</code> value to a
* <code>SYSTEMTIME</code> value.
*
* \deprecated Instead use winstl::to_SYSTEMTIME(FILETIME const&)
*
* \ingroup group__library__shims__string_access
*
* \param ft The <code>FILETIME</code> instance whose value is to be converted
*
* \note If <code>ft</code> does not represent a valid time value, the
* return value is undefined. The caller may check
* <code>GetLastError()</code> to determine whether the conversion has been
* successful.
*/
inline SYSTEMTIME FILETIME2SYSTEMTIME(FILETIME const& ft)
{
return winstl_ns_qual(to_SYSTEMTIME)(ft);
}
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 ws_size_t cchTotal = static_cast<ws_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 &stm, FILETIME const& ft)
{
stream_insert(stm, FILETIME2SYSTEMTIME(ft));
}
#ifdef WINSTL_UDATE_DEFINED
template <ss_typename_param_k S>
inline void stream_insert(S &stm, UDATE const& ud)
{
stream_insert(stm, ud.st);
}
#endif /* WINSTL_UDATE_DEFINED */
/* /////////////////////////////////////////////////////////////////////////
* Shims
*/
/* /////////////////////////////////////////////////////////////////////////
* c_str_ptr
*/
// SYSTEMTIME
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_ptr_a(SYSTEMTIME const& t, ws_bool_t bMilliseconds)
{
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 ws_size_t cchTotal = static_cast<ws_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_a_t> c_str_ptr_a(SYSTEMTIME const& t)
{
return c_str_ptr_a(t, ws_false_v);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_ptr_w(SYSTEMTIME const& t, ws_bool_t bMilliseconds)
{
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 ws_size_t cchTotal = static_cast<ws_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)<ws_char_w_t> c_str_ptr_w(SYSTEMTIME const& t)
{
return c_str_ptr_w(t, ws_false_v);
}
inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_ptr(SYSTEMTIME const& t, ws_bool_t bMilliseconds)
{
#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)<TCHAR> c_str_ptr(SYSTEMTIME const& t)
{
return c_str_ptr(t, ws_false_v);
}
// FILETIME
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_ptr_a(FILETIME const& t, ws_bool_t bMilliseconds)
{
return c_str_ptr_a(FILETIME2SYSTEMTIME(t), bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_ptr_a(FILETIME const& t)
{
return c_str_ptr_a(t, ws_false_v);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_ptr_w(FILETIME const& t, ws_bool_t bMilliseconds)
{
return c_str_ptr_w(FILETIME2SYSTEMTIME(t), bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_ptr_w(FILETIME const& t)
{
return c_str_ptr_w(t, ws_false_v);
}
inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_ptr(FILETIME const& t, ws_bool_t bMilliseconds)
{
return c_str_ptr(FILETIME2SYSTEMTIME(t), bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_ptr(FILETIME const& t)
{
return c_str_ptr(t, ws_false_v);
}
#ifdef WINSTL_UDATE_DEFINED
// UDATE
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_ptr_a(UDATE const& ud, ws_bool_t bMilliseconds)
{
return c_str_ptr_a(ud.st, bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_ptr_a(UDATE const& ud)
{
return c_str_ptr_a(ud.st);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_ptr_w(UDATE const& ud, ws_bool_t bMilliseconds)
{
return c_str_ptr_w(ud.st, bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_ptr_w(UDATE const& ud)
{
return c_str_ptr_w(ud.st);
}
inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_ptr(UDATE const& ud, ws_bool_t bMilliseconds)
{
return c_str_ptr(ud.st, bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_ptr(UDATE const& ud)
{
return c_str_ptr(ud.st);
}
#endif /* WINSTL_UDATE_DEFINED */
/* /////////////////////////////////////////////////////////////////////////
* c_str_data
*/
// SYSTEMTIME
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_data_a(SYSTEMTIME const& t, ws_bool_t bMilliseconds)
{
return c_str_ptr_a(t, bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_data_a(SYSTEMTIME const& t)
{
return c_str_data_a(t, ws_false_v);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_data_w(SYSTEMTIME const& t, ws_bool_t bMilliseconds)
{
return c_str_ptr_w(t, bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_data_w(SYSTEMTIME const& t)
{
return c_str_data_w(t, ws_false_v);
}
inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_data(SYSTEMTIME const& t, ws_bool_t bMilliseconds)
{
#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)<TCHAR> c_str_data(SYSTEMTIME const& t)
{
return c_str_data(t, ws_false_v);
}
// FILETIME
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_data_a(FILETIME const& t, ws_bool_t bMilliseconds)
{
return c_str_ptr_a(FILETIME2SYSTEMTIME(t), bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_data_a(FILETIME const& t)
{
return c_str_data_a(FILETIME2SYSTEMTIME(t), ws_false_v);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_data_w(FILETIME const& t, ws_bool_t bMilliseconds)
{
return c_str_ptr_w(FILETIME2SYSTEMTIME(t), bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_data_w(FILETIME const& t)
{
return c_str_data_w(FILETIME2SYSTEMTIME(t), ws_false_v);
}
inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_data(FILETIME const& t, ws_bool_t bMilliseconds)
{
return c_str_ptr(FILETIME2SYSTEMTIME(t), bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<TCHAR> c_str_data(FILETIME const& t)
{
return c_str_data(FILETIME2SYSTEMTIME(t), ws_false_v);
}
// UDATE
#ifdef WINSTL_UDATE_DEFINED
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_data_a(UDATE const& t, ws_bool_t bMilliseconds)
{
return c_str_ptr_a(t.st, bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_a_t> c_str_data_a(UDATE const& t)
{
return c_str_data_a(t.st, ws_false_v);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_data_w(UDATE const& t, ws_bool_t bMilliseconds)
{
return c_str_ptr_w(t.st, bMilliseconds);
}
inline stlsoft_ns_qual(basic_shim_string)<ws_char_w_t> c_str_data_w(UDATE const& t)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -