//*************************************************************************** // // (c) 2000-2001 by Microsoft Corp. All Rights Reserved. // // datepart.cpp // // a-davcoo 28-Feb-00 Implements the SQL datepart operation. // //*************************************************************************** #include "precomp.h" #include "datepart.h" #include #include "wbemcli.h" CDatePart::CDatePart () { m_date=NULL; } CDatePart::~CDatePart (void) { delete m_date; } HRESULT CDatePart::SetDate (LPCWSTR lpDate) { HRESULT hr = 0; delete m_date; m_date=NULL; m_date = new CDMTFParser(lpDate); if (!m_date) hr = WBEM_E_OUT_OF_MEMORY; return hr; } HRESULT CDatePart::SetDate (LPCSTR lpDate) { HRESULT hr = 0; delete m_date; m_date=NULL; wchar_t *pNew = new wchar_t [(strlen(lpDate)*4)+1]; if (pNew) { swprintf(pNew, L"%S", lpDate); m_date = new CDMTFParser(pNew); delete pNew; } if (!m_date) hr = WBEM_E_OUT_OF_MEMORY; return hr; } HRESULT CDatePart::GetPart (int datepart, int *value) { HRESULT hr=WBEM_S_NO_ERROR; int part; switch (datepart) { case DATEPART_YEAR: { part=CDMTFParser::YEAR; break; } case DATEPART_MONTH: { part=CDMTFParser::MONTH; break; } case DATEPART_DAY: { part=CDMTFParser::DAY; break; } case DATEPART_HOUR: { part=CDMTFParser::HOUR; break; } case DATEPART_MINUTE: { part=CDMTFParser::MINUTE; break; } case DATEPART_SECOND: { part=CDMTFParser::SECOND; break; } case DATEPART_MILLISECOND: { part=CDMTFParser::MICROSECOND; break; } default: { hr=WBEM_E_NOT_AVAILABLE; *value=0; break; } } if (SUCCEEDED(hr)) { if (!m_date->IsValid()) { hr=WBEM_E_INVALID_PARAMETER; } else if (!m_date->IsUsed (part) || m_date->IsWildcard (part)) { hr=WBEM_E_NOT_AVAILABLE; } else { *value=m_date->GetValue (part); if (datepart==DATEPART_MILLISECOND) *value/=1000; } } return hr; } CDMTFParser::CDMTFParser (LPCWSTR date) { ParseDate (date); } CDMTFParser::~CDMTFParser (void) { } void CDMTFParser::ParseDate (LPCWSTR date) { m_valid=true; for (int index=0; indexmax)) return INVALID; return VALID; } bool CDMTFParser::IsValid (void) { return m_valid; } bool CDMTFParser::IsInterval (void) { return m_interval; } bool CDMTFParser::IsUsed (int part) { return m_valid && !(m_status[part] & NOTUSED); } bool CDMTFParser::IsWildcard (int part) { return m_valid && (m_status[part] & NOTSUPPLIED); } int CDMTFParser::GetValue (int part) { return m_valid ? m_part[part] : 0; }