/////////////////////////////////////////////////////////////////////// // SysTime.cpp - Win32 API-based helper functions for system time // // ver 1.0 // // Platform: Dell Precision T7400, Win-7 Pro // // Application: CSE687 Project #3, Spring 2010 // // Author: Jim Fawcett, Syracuse University CST 4-187 // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////// #include "SysTime.h" #include #include using namespace Win32Tools; template std::string ToString(const T& t) { std::ostringstream temp; temp.fill('0'); temp.width(2); temp << t; return temp.str(); } std::string SystemTime::getSystemTime() { // Note: // Milliseconds may have up to three characters // Year has four characters // However, the iostream width(int) function sets // the field width to the specified OR to the number // of characters required to represent the value, // using whichever is larger, so this just works. SYSTEMTIME st; ::GetLocalTime(&st); std::string time = ToString(st.wHour); time.append(":"); time.append(ToString(st.wMinute)); time.append(":"); time.append(ToString(st.wSecond)); time.append(":"); time.append(ToString(st.wMilliseconds)); return time; } std::string SystemTime::getSystemDate() { SYSTEMTIME st; ::GetLocalTime(&st); std::string time = ToString(st.wMonth); time.append("/"); time.append(ToString(st.wDay)); time.append("/"); time.append(ToString(st.wYear)); return time; } void main() { std::cout << "\n Demonstrating SystemTime operations"; std::cout << "\n =====================================\n"; std::cout << "\n System time = " << SystemTime::getSystemTime(); std::cout << "\n System date = " << SystemTime::getSystemDate(); std::cout << "\n\n"; }