1*f8e2c85aSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*f8e2c85aSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*f8e2c85aSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*f8e2c85aSAndrew Rist * distributed with this work for additional information 6*f8e2c85aSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*f8e2c85aSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*f8e2c85aSAndrew Rist * "License"); you may not use this file except in compliance 9*f8e2c85aSAndrew Rist * with the License. You may obtain a copy of the License at 10*f8e2c85aSAndrew Rist * 11*f8e2c85aSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*f8e2c85aSAndrew Rist * 13*f8e2c85aSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*f8e2c85aSAndrew Rist * software distributed under the License is distributed on an 15*f8e2c85aSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*f8e2c85aSAndrew Rist * KIND, either express or implied. See the License for the 17*f8e2c85aSAndrew Rist * specific language governing permissions and limitations 18*f8e2c85aSAndrew Rist * under the License. 19*f8e2c85aSAndrew Rist * 20*f8e2c85aSAndrew Rist *************************************************************/ 21*f8e2c85aSAndrew Rist 22*f8e2c85aSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_shell.hxx" 26cdf0e10cSrcweir #include "internal/iso8601_converter.hxx" 27cdf0e10cSrcweir #include "internal/utilities.hxx" 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include <sstream> 30cdf0e10cSrcweir #include <iomanip> 31cdf0e10cSrcweir 32cdf0e10cSrcweir //----------------------------------- 33cdf0e10cSrcweir /* Converts ISO 8601 conform date/time 34cdf0e10cSrcweir represenation to the representation 35cdf0e10cSrcweir conforming to the current locale 36cdf0e10cSrcweir */ 37cdf0e10cSrcweir std::wstring iso8601_date_to_local_date(const std::wstring& isoDate ) 38cdf0e10cSrcweir { 39cdf0e10cSrcweir const std::wstring CONST_SPACE(L" "); 40cdf0e10cSrcweir ::std::wstring ws8601DateTime(isoDate); 41cdf0e10cSrcweir 42cdf0e10cSrcweir if ( ws8601DateTime.length() == 19 ) 43cdf0e10cSrcweir { 44cdf0e10cSrcweir //fill in the SYSTEMTIME structure; 45cdf0e10cSrcweir std::string asDateTime = WStringToString( ws8601DateTime ); 46cdf0e10cSrcweir SYSTEMTIME DateTime; 47cdf0e10cSrcweir DateTime.wYear = ( unsigned short )strtol( asDateTime.substr( 0, 4 ).c_str(), NULL, 10 ); 48cdf0e10cSrcweir DateTime.wMonth = ( unsigned short )strtol( asDateTime.substr( 5, 2 ).c_str(), NULL, 10 ); 49cdf0e10cSrcweir DateTime.wDayOfWeek = 0; 50cdf0e10cSrcweir DateTime.wDay = ( unsigned short )strtol( asDateTime.substr( 8, 2 ).c_str(), NULL, 10 ); 51cdf0e10cSrcweir DateTime.wHour = ( unsigned short )strtol( asDateTime.substr( 11,2 ).c_str(), NULL, 10 ); 52cdf0e10cSrcweir DateTime.wMinute = ( unsigned short )strtol( asDateTime.substr( 14,2 ).c_str(), NULL, 10 ); 53cdf0e10cSrcweir DateTime.wSecond = ( unsigned short )strtol( asDateTime.substr( 17,2 ).c_str(), NULL, 10 ); 54cdf0e10cSrcweir DateTime.wMilliseconds = 0; 55cdf0e10cSrcweir 56cdf0e10cSrcweir //get Date info from structure 57cdf0e10cSrcweir WCHAR DateBuffer[ MAX_PATH ]; 58cdf0e10cSrcweir int DateSize = GetDateFormatW( 59cdf0e10cSrcweir LOCALE_SYSTEM_DEFAULT, 60cdf0e10cSrcweir 0, 61cdf0e10cSrcweir &DateTime, 62cdf0e10cSrcweir NULL, 63cdf0e10cSrcweir DateBuffer, 64cdf0e10cSrcweir MAX_PATH ); 65cdf0e10cSrcweir 66cdf0e10cSrcweir if ( DateSize ) 67cdf0e10cSrcweir ws8601DateTime.assign(DateBuffer); 68cdf0e10cSrcweir else 69cdf0e10cSrcweir ws8601DateTime = StringToWString( asDateTime ); 70cdf0e10cSrcweir 71cdf0e10cSrcweir //get Time info from structure 72cdf0e10cSrcweir WCHAR TimeBuffer[ MAX_PATH ]; 73cdf0e10cSrcweir 74cdf0e10cSrcweir int TimeSize = GetTimeFormatW( 75cdf0e10cSrcweir LOCALE_SYSTEM_DEFAULT, 76cdf0e10cSrcweir 0, 77cdf0e10cSrcweir &DateTime, 78cdf0e10cSrcweir NULL, 79cdf0e10cSrcweir TimeBuffer, 80cdf0e10cSrcweir MAX_PATH ); 81cdf0e10cSrcweir 82cdf0e10cSrcweir if ( TimeSize ) 83cdf0e10cSrcweir { 84cdf0e10cSrcweir ws8601DateTime.append(L" "); 85cdf0e10cSrcweir ws8601DateTime.append(TimeBuffer); 86cdf0e10cSrcweir } 87cdf0e10cSrcweir else 88cdf0e10cSrcweir ws8601DateTime = StringToWString( asDateTime ); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir return ws8601DateTime; 92cdf0e10cSrcweir } 93cdf0e10cSrcweir 94cdf0e10cSrcweir //------------------------------------ 95cdf0e10cSrcweir /* Converts ISO 8601 conform duration 96cdf0e10cSrcweir representation to the representation 97cdf0e10cSrcweir conforming to the current locale 98cdf0e10cSrcweir 99cdf0e10cSrcweir Expect format PTnHnMnS according to 100cdf0e10cSrcweir ISO 8601 where n is abitrary number 101cdf0e10cSrcweir of digits 102cdf0e10cSrcweir */ 103cdf0e10cSrcweir 104cdf0e10cSrcweir std::wstring iso8601_duration_to_local_duration(const std::wstring& iso8601duration) 105cdf0e10cSrcweir { 106cdf0e10cSrcweir std::wstring days; 107cdf0e10cSrcweir std::wstring hours; 108cdf0e10cSrcweir std::wstring minutes; 109cdf0e10cSrcweir std::wstring seconds; 110cdf0e10cSrcweir 111cdf0e10cSrcweir std::wstring::const_iterator iter = iso8601duration.begin(); 112cdf0e10cSrcweir std::wstring::const_iterator iter_end = iso8601duration.end(); 113cdf0e10cSrcweir 114cdf0e10cSrcweir std::wstring num; 115cdf0e10cSrcweir 116cdf0e10cSrcweir for (/**/; iter != iter_end; ++iter) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir if (isdigit(*iter)) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir num += *iter; 121cdf0e10cSrcweir } 122cdf0e10cSrcweir else 123cdf0e10cSrcweir { 124cdf0e10cSrcweir if (*iter == L'D' || *iter == L'd') 125cdf0e10cSrcweir days = num; 126cdf0e10cSrcweir else if (*iter == L'H' || *iter == L'h') 127cdf0e10cSrcweir hours = num; 128cdf0e10cSrcweir else if (*iter == L'M' || *iter == L'm') 129cdf0e10cSrcweir minutes = num; 130cdf0e10cSrcweir else if (*iter == L'S' || *iter == L's') 131cdf0e10cSrcweir seconds = num; 132cdf0e10cSrcweir 133cdf0e10cSrcweir num.clear(); 134cdf0e10cSrcweir } 135cdf0e10cSrcweir } 136cdf0e10cSrcweir 137cdf0e10cSrcweir if (days.length() > 0) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir int h = ((_wtoi(days.c_str()) * 24) + _wtoi(hours.c_str())); 140cdf0e10cSrcweir wchar_t buff[10]; 141cdf0e10cSrcweir _itow(h, buff, 10); 142cdf0e10cSrcweir hours = buff; 143cdf0e10cSrcweir } 144cdf0e10cSrcweir 145cdf0e10cSrcweir #if defined(_MSC_VER) //&& defined(_M_X64) 146cdf0e10cSrcweir std::wostringstream oss; 147cdf0e10cSrcweir oss << std::setw(2) << std::setfill(wchar_t('0')) << hours << L":" << 148cdf0e10cSrcweir std::setw(2) << std::setfill(wchar_t('0')) << minutes << L":" << 149cdf0e10cSrcweir std::setw(2) << std::setfill(wchar_t('0')) << seconds; 150cdf0e10cSrcweir return oss.str(); 151cdf0e10cSrcweir #elif defined( __MINGW32__ ) 152cdf0e10cSrcweir #define ADD_AS_PREFILLED( st, out ) \ 153cdf0e10cSrcweir if ( st.length() == 0 ) \ 154cdf0e10cSrcweir out += L"00"; \ 155cdf0e10cSrcweir else if ( st.length() == 1 ) \ 156cdf0e10cSrcweir out += L"0"; \ 157cdf0e10cSrcweir out += st; 158cdf0e10cSrcweir 159cdf0e10cSrcweir std::wstring result; 160cdf0e10cSrcweir ADD_AS_PREFILLED( hours, result ) 161cdf0e10cSrcweir result += L":"; 162cdf0e10cSrcweir ADD_AS_PREFILLED( minutes, result ) 163cdf0e10cSrcweir result += L":"; 164cdf0e10cSrcweir ADD_AS_PREFILLED( seconds, result ) 165cdf0e10cSrcweir 166cdf0e10cSrcweir return result; 167cdf0e10cSrcweir #undef ADD_AS_PREFILLED 168cdf0e10cSrcweir /* 169cdf0e10cSrcweir #else 170cdf0e10cSrcweir std::wostringstream oss; 171cdf0e10cSrcweir oss << std::setw(2) << std::setfill('0') << hours << L":" << 172cdf0e10cSrcweir std::setw(2) << std::setfill('0') << minutes << L":" << 173cdf0e10cSrcweir std::setw(2) << std::setfill('0') << seconds; 174cdf0e10cSrcweir return oss.str(); 175cdf0e10cSrcweir */ 176cdf0e10cSrcweir #endif 177cdf0e10cSrcweir } 178cdf0e10cSrcweir 179