|
/* /////////////////////////////////////////////////////////////////////////
|
|
* File: winstl/time/format_functions.hpp
|
|
*
|
|
* Purpose: Comparison functions for Windows time structures.
|
|
*
|
|
* Created: 21st November 2003
|
|
* Updated: 7th August 2012
|
|
*
|
|
* Thanks to: Mikael Pahmp, for spotting the failure to handle 24-hour
|
|
* time pictures.
|
|
*
|
|
* Home: http://stlsoft.org/
|
|
*
|
|
* Copyright (c) 2003-2012, 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/format_functions.hpp
|
|
*
|
|
* \brief [C, C++] Formatting functions for Windows time types
|
|
* (\ref group__library__time "Time" Library).
|
|
*/
|
|
|
|
#ifndef WINSTL_INCL_WINSTL_TIME_HPP_FORMAT_FUNCTIONS
|
|
#define WINSTL_INCL_WINSTL_TIME_HPP_FORMAT_FUNCTIONS
|
|
|
|
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
|
|
# define WINSTL_VER_WINSTL_TIME_HPP_FORMAT_FUNCTIONS_MAJOR 5
|
|
# define WINSTL_VER_WINSTL_TIME_HPP_FORMAT_FUNCTIONS_MINOR 1
|
|
# define WINSTL_VER_WINSTL_TIME_HPP_FORMAT_FUNCTIONS_REVISION 2
|
|
# define WINSTL_VER_WINSTL_TIME_HPP_FORMAT_FUNCTIONS_EDIT 62
|
|
#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_HPP_MEMORY_AUTO_BUFFER
|
|
# include <stlsoft/memory/auto_buffer.hpp>
|
|
#endif /* !STLSOFT_INCL_STLSOFT_HPP_MEMORY_AUTO_BUFFER */
|
|
#ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_INTEGER_TO_STRING
|
|
# include <stlsoft/conversion/integer_to_string.hpp>
|
|
#endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_INTEGER_TO_STRING */
|
|
#ifndef WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR
|
|
# include <winstl/memory/processheap_allocator.hpp>
|
|
#endif /* !WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR */
|
|
#ifndef WINSTL_INCL_WINSTL_REGISTRY_HPP_FUNCTIONS
|
|
# include <winstl/registry/functions.hpp> // for reg_get_string_value()
|
|
#endif /* !WINSTL_INCL_WINSTL_REGISTRY_HPP_FUNCTIONS */
|
|
|
|
/* /////////////////////////////////////////////////////////////////////////
|
|
* 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 classes
|
|
*/
|
|
|
|
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
|
|
|
|
template <ss_typename_param_k C>
|
|
struct time_format_functions_traits;
|
|
|
|
STLSOFT_TEMPLATE_SPECIALISATION
|
|
struct time_format_functions_traits<ws_char_a_t>
|
|
{
|
|
typedef ws_char_a_t char_type;
|
|
|
|
static int GetTimeFormat(LCID Locale, DWORD dwFlags, SYSTEMTIME const* lpTime, char_type const* lpFormat, char_type* lpTimeStr, int cchTime)
|
|
{
|
|
return ::GetTimeFormatA(Locale, dwFlags, lpTime, lpFormat, lpTimeStr, cchTime);
|
|
}
|
|
static int GetLocaleInfo(LCID Locale, LCTYPE LCType, char_type* lpLCData, int cchData)
|
|
{
|
|
return ::GetLocaleInfoA(Locale, LCType, lpLCData, cchData);
|
|
}
|
|
static ss_size_t lstrlen(char_type const* s)
|
|
{
|
|
return static_cast<ss_size_t>(::lstrlenA(s));
|
|
}
|
|
static char_type* lstrcpy(char_type* dest, char_type const* src)
|
|
{
|
|
return ::lstrcpyA(dest, src);
|
|
}
|
|
};
|
|
|
|
STLSOFT_TEMPLATE_SPECIALISATION
|
|
struct time_format_functions_traits<ws_char_w_t>
|
|
{
|
|
typedef ws_char_w_t char_type;
|
|
|
|
static int GetTimeFormat(LCID Locale, DWORD dwFlags, SYSTEMTIME const* lpTime, char_type const* lpFormat, char_type* lpTimeStr, int cchTime)
|
|
{
|
|
return ::GetTimeFormatW(Locale, dwFlags, lpTime, lpFormat, lpTimeStr, cchTime);
|
|
}
|
|
static int GetLocaleInfo(LCID Locale, LCTYPE LCType, char_type* lpLCData, int cchData)
|
|
{
|
|
return ::GetLocaleInfoW(Locale, LCType, lpLCData, cchData);
|
|
}
|
|
static ss_size_t lstrlen(char_type const* s)
|
|
{
|
|
return static_cast<ss_size_t>(::lstrlenW(s));
|
|
}
|
|
static char_type* lstrcpy(char_type* dest, char_type const* src)
|
|
{
|
|
return ::lstrcpyW(dest, src);
|
|
}
|
|
};
|
|
|
|
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
|
|
|
|
/* /////////////////////////////////////////////////////////////////////////
|
|
* Functions
|
|
*/
|
|
|
|
template <ss_typename_param_k C>
|
|
inline
|
|
int
|
|
STLSOFT_STDCALL GetTimeFormat_ms_(
|
|
LCID locale // locale
|
|
, DWORD dwFlags // options
|
|
, CONST SYSTEMTIME* lpTime // time
|
|
, C const* lpFormat // time format string
|
|
, C const* const* timeMarkers // pointer to array of two pointers to time markers
|
|
, C* lpTimeStr // formatted string buffer
|
|
, int const cchTime // size of string buffer
|
|
)
|
|
{
|
|
typedef C char_t;
|
|
typedef time_format_functions_traits<char_t> traits_t;
|
|
typedef stlsoft_ns_qual(auto_buffer_old)<
|
|
char_t
|
|
, processheap_allocator<char_t>
|
|
> buffer_t;
|
|
|
|
if(dwFlags & (TIME_NOMINUTESORSECONDS | TIME_NOSECONDS))
|
|
{
|
|
return traits_t::GetTimeFormat(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>(::GetLocaleInfoA(locale, LOCALE_STIMEFORMAT, NULL, 0)) : 0));
|
|
|
|
if(NULL == lpFormat)
|
|
{
|
|
ss_size_t n = static_cast<ss_size_t>(traits_t::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, &timePicture[0], static_cast<int>(timePicture.size())));
|
|
lpFormat = &timePicture[0];
|
|
if(n < timePicture.size())
|
|
{
|
|
timePicture[n] = static_cast<C>('\0');
|
|
}
|
|
}
|
|
|
|
ss_size_t const cchPicture = 1 + traits_t::lstrlen(lpFormat);
|
|
|
|
// Following need to be front-padded to be forward compatible with STLSoft 1.10 (which uses
|
|
// 'safer' i2s functions
|
|
|
|
char_t hours12_[] = { '\0', '\0', '\0', '0', '0', '\0' }; // "...00"
|
|
char_t hours24_[] = { '\0', '\0', '\0', '0', '0', '\0' }; // "...00"
|
|
char_t minutes_[] = { '\0', '\0', '\0', '0', '0', '\0' }; // "...00"
|
|
char_t seconds_[] = { '\0', '\0', '\0', '0', '0', '.', '0', '0', '0', '\0' }; // "...00.000"
|
|
|
|
uint16_t const hour12 = (lpTime->wHour > 12) ? uint16_t(lpTime->wHour - 12) : lpTime->wHour;
|
|
|
|
#if defined(STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT)
|
|
char_t const* hours12 = stlsoft_ns_qual(integer_to_string)(hours12_, hour12);
|
|
char_t const* hours24 = stlsoft_ns_qual(integer_to_string)(hours24_, lpTime->wHour);
|
|
char_t const* minutes = stlsoft_ns_qual(integer_to_string)(minutes_, lpTime->wMinute);
|
|
#else /* ? STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */
|
|
char_t const* hours12 = stlsoft_ns_qual(integer_to_string)(&hours12_[0], STLSOFT_NUM_ELEMENTS(hours12_), hour12);
|
|
char_t const* hours24 = stlsoft_ns_qual(integer_to_string)(&hours24_[0], STLSOFT_NUM_ELEMENTS(hours24_), lpTime->wHour);
|
|
char_t const* minutes = stlsoft_ns_qual(integer_to_string)(&minutes_[0], STLSOFT_NUM_ELEMENTS(minutes_), lpTime->wMinute);
|
|
#endif /* STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */
|
|
stlsoft_ns_qual(integer_to_string)(&seconds_[3], STLSOFT_NUM_ELEMENTS(seconds_) - 3, lpTime->wMilliseconds);
|
|
char_t const* seconds = stlsoft_ns_qual(integer_to_string)(&seconds_[0], 6, lpTime->wSecond);
|
|
|
|
seconds_[5] = '.';
|
|
|
|
// Get the time markers
|
|
char_t const* amMarker = (NULL != timeMarkers && NULL != timeMarkers[0]) |