const string Clock::ToString() const { wchar_t buffer[256]; _wasctime_s(buffer, 256, &m_date); buffer[24] = 0; return string(buffer); }
BOOL CDebugLog::Log(TCHAR *fmt, ...) { if(m_hLogFile == INVALID_HANDLE_VALUE) { return FALSE; } size_t nLen; TCHAR szOutBuffer[1024] = {0}; TCHAR szInBuffer[1024] = {0}; DWORD dwInBuffer = _countof(szInBuffer); DWORD dwOutBuffer = _countof(szOutBuffer); DWORD dwBytesWritten; auto va_list argptr; time_t rawtime = 0; struct tm timeinfo = {0}; time(&rawtime); localtime_s(&timeinfo, &rawtime); _wasctime_s(szInBuffer, _countof(szInBuffer), &timeinfo); nLen = wcslen(szInBuffer); szInBuffer[nLen - 1] = ' '; va_start(argptr, fmt); vswprintf_s(szInBuffer + nLen, _countof(szInBuffer) - nLen - 1, fmt, argptr); va_end(argptr); nLen = wcslen(szInBuffer); if(nLen == 0) { return TRUE; } wcscat_s(szInBuffer, _countof(szInBuffer), _T("\n")); dwInBuffer = (DWORD)wcslen(szInBuffer); char *pszOutBuffer = new char[dwInBuffer + 4]; WideCharToMultiByte(CP_ACP, 0, szInBuffer, -1, pszOutBuffer, dwInBuffer + 1, NULL, NULL); if(m_hLogFile == INVALID_HANDLE_VALUE) { delete [] pszOutBuffer; return FALSE; } OutputDebugString(szInBuffer); WriteFile(m_hLogFile, pszOutBuffer, dwInBuffer, &dwBytesWritten, NULL); delete [] pszOutBuffer; return TRUE; }
void LeakReport::DumpUrl(DWORD tid) { AutoCriticalSection autocs(&s_cs); if (!EnsureLeakReportFile()) { return; } UrlRecord * prev = nullptr; UrlRecord ** pprev = &LeakReport::urlRecordHead; UrlRecord * curr = *pprev; while (curr != nullptr) { if (curr->tid == tid) { char16 timeStr[26] = _u("00:00"); // xplat-todo: Need to implement _wasctime_s in the PAL #if _MSC_VER struct tm local_time; _localtime64_s(&local_time, &curr->time); _wasctime_s(timeStr, &local_time); #endif timeStr[wcslen(timeStr) - 1] = 0; Print(_u("%s - (%p, %p) %s\n"), timeStr, curr->scriptEngine, curr->globalObject, curr->url); *pprev = curr->next; NoCheckHeapDeleteArray(wcslen(curr->url) + 1, curr->url); NoCheckHeapDelete(curr); } else { pprev = &curr->next; prev = curr; } curr = *pprev; } if (prev == nullptr) { LeakReport::urlRecordTail = nullptr; } else if (prev->next == nullptr) { LeakReport::urlRecordTail = prev; } }
std::wstring GetBuildTimeFromAddress(_In_ const void* const codeAddress) { // Get the base of the address reservation. This lets this // function be passed any function or global variable address // in a DLL or EXE. MEMORY_BASIC_INFORMATION memoryInfo; if (::VirtualQuery(codeAddress, &memoryInfo, sizeof(memoryInfo)) != sizeof(memoryInfo)) { UIETWASSERT(0); return L""; } const void* const ModuleHandle = memoryInfo.AllocationBase; // Walk the PE data structures to find the link time stamp. const IMAGE_DOS_HEADER* const DosHeader = reinterpret_cast<const IMAGE_DOS_HEADER*>(ModuleHandle); if (IMAGE_DOS_SIGNATURE != DosHeader->e_magic) { UIETWASSERT(0); return L""; } const IMAGE_NT_HEADERS* const NTHeader = reinterpret_cast<const IMAGE_NT_HEADERS*>( reinterpret_cast<const char*>(DosHeader) + DosHeader->e_lfanew); if (IMAGE_NT_SIGNATURE != NTHeader->Signature) { UIETWASSERT(0); return L""; } // TimeDateStamp is 32 bits and time_t is 64 bits. That will have to be dealt // with when TimeDateStamp wraps in February 2106. const time_t timeDateStamp = NTHeader->FileHeader.TimeDateStamp; tm linkTime; gmtime_s(&linkTime, &timeDateStamp); // Print out the module information. The %.24s is necessary to trim // the new line character off of the date string returned by asctime(). // _wasctime_s requires a 26-character buffer. wchar_t ascTimeBuf[26]; _wasctime_s(ascTimeBuf, &linkTime); wchar_t buffer[100]; swprintf_s(buffer, L"%.24s GMT (%08lx)", ascTimeBuf, NTHeader->FileHeader.TimeDateStamp); // Return buffer+4 because we don't need the day of the week. return buffer + 4; }
std::wstring GetBuildTimeFromAddress(void* codeAddress) { // Get the base of the address reservation. This lets this // function be passed any function or global variable address // in a DLL or EXE. MEMORY_BASIC_INFORMATION memoryInfo; if (VirtualQuery(codeAddress, &memoryInfo, sizeof(memoryInfo)) != sizeof(memoryInfo)) { UIETWASSERT(0); return L""; } void* ModuleHandle = memoryInfo.AllocationBase; // Walk the PE data structures to find the link time stamp. IMAGE_DOS_HEADER *DosHeader = (IMAGE_DOS_HEADER*)ModuleHandle; if (IMAGE_DOS_SIGNATURE != DosHeader->e_magic) { UIETWASSERT(0); return L""; } IMAGE_NT_HEADERS *NTHeader = (IMAGE_NT_HEADERS*)((char *)DosHeader + DosHeader->e_lfanew); if (IMAGE_NT_SIGNATURE != NTHeader->Signature) { UIETWASSERT(0); return L""; } tm linkTime = {}; gmtime_s(&linkTime, (time_t*)&NTHeader->FileHeader.TimeDateStamp); // Print out the module information. The %.24s is necessary to trim // the new line character off of the date string returned by asctime(). // _wasctime_s requires a 26-character buffer. wchar_t ascTimeBuf[26]; _wasctime_s(ascTimeBuf, &linkTime); wchar_t buffer[100]; swprintf_s(buffer, L"%.24s GMT (%08lx)", ascTimeBuf, NTHeader->FileHeader.TimeDateStamp); // Return buffer+4 because we don't need the day of the week. return buffer + 4; }
static errno_t __cdecl _int_wctime32_s (wchar_t *d, size_t dn, const __time32_t *pt) { struct tm ltm; errno_t e; if (!d || !dn) { errno = EINVAL; return EINVAL; } d[0] = 0; if (!pt) { errno = EINVAL; return EINVAL; } if ((e = _localtime32_s (<m, pt)) != 0) return e; return _wasctime_s (d, dn, <m); }
void testtime_s( void ) { int violations = NumViolations; time_t tt1; time_t tt2; struct tm tm1; struct tm tm2; wchar_t const * const datestr = L"Wed Aug 14 17:23:31 2002\n"; wchar_t buf[64]; /*** Test various functions ***/ tm1.tm_sec = 31; tm1.tm_min = 23; tm1.tm_hour = 17; tm1.tm_mday = 14; tm1.tm_mon = 7; tm1.tm_year = 102; tm1.tm_isdst = -1; tt1 = mktime( &tm1 ); tm1.tm_sec++; tt2 = mktime( &tm1 ); /* Test localtime_s() and _wasctime_s() */ VERIFY( localtime_s( &tt1, &tm2 ) == &tm2 ); VERIFY( _wasctime_s( buf, sizeof(buf), &tm2 ) == 0 ); VERIFY( NumViolations == violations ); VERIFY( wcscmp( buf, datestr ) == 0 ); VERIFY( _wasctime_s( NULL, sizeof(buf), &tm2 ) != 0 ); VERIFY( NumViolations == ++violations ); buf[0] = L'X'; VERIFY( _wasctime_s( buf, 25, &tm2 ) != 0 ); VERIFY( NumViolations == ++violations ); VERIFY( L'\0' == buf[0] ); #if RSIZE_MAX != SIZE_MAX buf[0] = L'X'; VERIFY( _wasctime_s( buf, ~0, &tm2 ) != 0 ); VERIFY( NumViolations == ++violations ); VERIFY( L'X' == buf[0] ); #endif tm2.tm_year = -1; buf[0] = L'X'; VERIFY( _wasctime_s( buf, sizeof(buf), &tm2 ) != 0 ); VERIFY( NumViolations == ++violations ); VERIFY( L'\0' == buf[0] ); tm2.tm_year = 9999+1; buf[0] = L'X'; VERIFY( _wasctime_s( buf, sizeof(buf), &tm2 ) != 0 ); VERIFY( NumViolations == ++violations ); VERIFY( L'\0' == buf[0] ); /* _wctime_s() */ VERIFY( _wctime_s( buf, sizeof(buf), &tt1 ) == 0 ); VERIFY( NumViolations == violations ); VERIFY( wcscmp( buf, datestr ) == 0 ); VERIFY( _wctime_s( NULL, sizeof(buf), &tt1 ) != 0 ); VERIFY( NumViolations == ++violations ); buf[0] = L'X'; VERIFY( _wctime_s( buf, 25, &tt1 ) != 0 ); VERIFY( NumViolations == ++violations ); VERIFY( L'\0' == buf[0] ); #if RSIZE_MAX != SIZE_MAX buf[0] = L'X'; VERIFY( _wctime_s( buf, ~0, &tt1 ) != 0 ); VERIFY( NumViolations == ++violations ); VERIFY( L'X' == buf[0] ); #endif }