|
26 | 26 | #include "StdAfx.h"
|
27 | 27 | #include "format.h"
|
28 | 28 |
|
29 |
| -/*! 日時をフォーマット |
| 29 | +/*! 書式文字列に従い日時を変換 |
30 | 30 |
|
31 |
| - @param[out] 書式変換後の文字列 |
32 |
| - @param[in] バッファサイズ |
33 |
| - @param[in] format 書式 |
| 31 | + @param[in] format 書式文字列 |
34 | 32 | @param[in] systime 書式化したい日時
|
35 |
| - @return bool true |
36 |
| -
|
37 |
| - @note %Y %y %m %d %H %M %S の変換に対応 |
38 |
| -
|
39 |
| - @author aroka |
40 |
| - @date 2005.11.21 新規 |
41 |
| - |
42 |
| - @todo 出力バッファのサイズチェックを行う |
| 33 | + @return 書式変換後の文字列 |
| 34 | +
|
| 35 | + @note 書式文字列では以下の変換指定子が使用できます。 |
| 36 | + @li "%Y" 西暦 |
| 37 | + @li "%y" 下2桁の西暦 |
| 38 | + @li "%m" 2桁の月 |
| 39 | + @li "%d" 2桁の日 |
| 40 | + @li "%H" 2桁の時 |
| 41 | + @li "%M" 2桁の分 |
| 42 | + @li "%S" 2桁の秒 |
| 43 | + @note 書式文字列は末尾または最初のnull文字までを変換対象とします。 |
43 | 44 | */
|
44 |
| -bool GetDateTimeFormat( WCHAR* szResult, int size, const WCHAR* format, const SYSTEMTIME& systime ) |
| 45 | +std::wstring GetDateTimeFormat( std::wstring_view format, const SYSTEMTIME& systime ) |
45 | 46 | {
|
46 |
| - WCHAR szTime[10]; |
47 |
| - const WCHAR *p = format; |
48 |
| - WCHAR *q = szResult; |
49 |
| - int len; |
50 |
| - |
51 |
| - while( *p ){ |
52 |
| - if( *p == L'%' ){ |
53 |
| - ++p; |
54 |
| - switch(*p){ |
55 |
| - case L'Y': |
56 |
| - len = wsprintf(szTime,L"%d",systime.wYear); |
57 |
| - wcscpy( q, szTime ); |
58 |
| - break; |
59 |
| - case L'y': |
60 |
| - len = wsprintf(szTime,L"%02d",(systime.wYear%100)); |
61 |
| - wcscpy( q, szTime ); |
62 |
| - break; |
63 |
| - case L'm': |
64 |
| - len = wsprintf(szTime,L"%02d",systime.wMonth); |
65 |
| - wcscpy( q, szTime ); |
66 |
| - break; |
67 |
| - case L'd': |
68 |
| - len = wsprintf(szTime,L"%02d",systime.wDay); |
69 |
| - wcscpy( q, szTime ); |
70 |
| - break; |
71 |
| - case L'H': |
72 |
| - len = wsprintf(szTime,L"%02d",systime.wHour); |
73 |
| - wcscpy( q, szTime ); |
74 |
| - break; |
75 |
| - case L'M': |
76 |
| - len = wsprintf(szTime,L"%02d",systime.wMinute); |
77 |
| - wcscpy( q, szTime ); |
78 |
| - break; |
79 |
| - case L'S': |
80 |
| - len = wsprintf(szTime,L"%02d",systime.wSecond); |
81 |
| - wcscpy( q, szTime ); |
82 |
| - break; |
83 |
| - // A Z |
84 |
| - case L'%': |
85 |
| - default: |
86 |
| - *q = *p; |
87 |
| - len = 1; |
88 |
| - break; |
| 47 | + std::wstring result; |
| 48 | + wchar_t str[6] = {}; |
| 49 | + bool inSpecifier = false; |
| 50 | + |
| 51 | + result.reserve( format.length() * 2 ); |
| 52 | + |
| 53 | + for( const auto f : format ){ |
| 54 | + if( inSpecifier ){ |
| 55 | + inSpecifier = false; |
| 56 | + if( f == L'Y' ){ |
| 57 | + swprintf( str, _countof(str), L"%d", systime.wYear ); |
| 58 | + }else if( f == L'y' ){ |
| 59 | + swprintf( str, _countof(str), L"%02d", systime.wYear % 100 ); |
| 60 | + }else if( f == L'm' ){ |
| 61 | + swprintf( str, _countof(str), L"%02d", systime.wMonth ); |
| 62 | + }else if( f == L'd' ){ |
| 63 | + swprintf( str, _countof(str), L"%02d", systime.wDay ); |
| 64 | + }else if( f == L'H' ){ |
| 65 | + swprintf( str, _countof(str), L"%02d", systime.wHour ); |
| 66 | + }else if( f == L'M' ){ |
| 67 | + swprintf( str, _countof(str), L"%02d", systime.wMinute ); |
| 68 | + }else if( f == L'S' ){ |
| 69 | + swprintf( str, _countof(str), L"%02d", systime.wSecond ); |
| 70 | + }else{ |
| 71 | + swprintf( str, _countof(str), L"%c", f ); |
89 | 72 | }
|
90 |
| - q+=len;//q += strlen(szTime); |
91 |
| - ++p; |
92 |
| - } |
93 |
| - else{ |
94 |
| - *q = *p; |
95 |
| - q++; |
96 |
| - p++; |
| 73 | + result.append( str ); |
| 74 | + }else if( f == L'%' ){ |
| 75 | + inSpecifier = true; |
| 76 | + }else if( f == L'\0' ){ |
| 77 | + break; |
| 78 | + }else{ |
| 79 | + result.push_back( f ); |
97 | 80 | }
|
98 | 81 | }
|
99 |
| - *q = *p; |
100 |
| - return true; |
| 82 | + |
| 83 | + return result; |
101 | 84 | }
|
102 | 85 |
|
103 | 86 | /*! バージョン番号の解析
|
|
0 commit comments