Main Page | Class List | File List | Class Members | File Members

format_system_time.cpp

Go to the documentation of this file.
00001 // format_system_time.cpp
00002 
00003 #include "format_system_time.h"
00004 #include <sstream>
00005 #include <iomanip>
00006 
00007 
00008 std::ostream& operator<< (std::ostream &os, const format_system_time& fst)
00009 {
00010   bool showdate = true;
00011   if (fst.m_options & format_system_time::relative)
00012   {
00013     SYSTEMTIME systime;
00014     GetLocalTime(&systime);
00015     if (
00016       systime.wYear == fst.m_systemtime.wYear &&
00017       systime.wMonth == fst.m_systemtime.wMonth &&
00018       systime.wDay == fst.m_systemtime.wDay
00019     )
00020     showdate = false;
00021   }
00022   std::ostringstream ss;
00023   ss.fill('0');
00024   if (showdate)
00025   {
00026     char szLocale[256];
00027     szLocale[0] = '\0';
00028     int iDMYOrder = 0;
00029     if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, szLocale, sizeof(szLocale)))
00030       iDMYOrder = atoi(szLocale);
00031     char szDateSep[4];
00032     strcpy(szDateSep, "/\0");
00033     GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, szDateSep, sizeof(szDateSep));
00034     switch (iDMYOrder)
00035     {
00036     case 0:
00037     default:
00038       ss << std::setw(2) << fst.m_systemtime.wMonth;
00039       ss << szDateSep;
00040       ss << std::setw(2) << fst.m_systemtime.wDay;
00041       ss << szDateSep;
00042       ss << std::setw(4) << fst.m_systemtime.wYear;
00043       break;
00044     case 1:
00045       ss << std::setw(2) << fst.m_systemtime.wDay;
00046       ss << szDateSep;
00047       ss << std::setw(2) << fst.m_systemtime.wMonth;
00048       ss << szDateSep;
00049       ss << std::setw(4) << fst.m_systemtime.wYear;
00050       break;
00051     case 2:
00052       ss << std::setw(4) << fst.m_systemtime.wYear;
00053       ss << szDateSep;
00054       ss << std::setw(2) << fst.m_systemtime.wMonth;
00055       ss << szDateSep;
00056       ss << std::setw(2) << fst.m_systemtime.wDay;
00057       break;
00058     }
00059     ss << " ";
00060   }
00061   char szTimeSep[4];
00062   strcpy(szTimeSep, ":\0");
00063   GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, szTimeSep, sizeof(szTimeSep));
00064   ss << std::setw(2) << fst.m_systemtime.wHour;
00065   ss << szTimeSep;
00066   ss << std::setw(2) << fst.m_systemtime.wMinute;
00067   bool showseconds = false;
00068   if (fst.m_options & format_system_time::show_seconds)
00069     showseconds = true;
00070   bool showmilliseconds = false;
00071   if (fst.m_options & format_system_time::show_milliseconds)
00072   {
00073     showmilliseconds = true;
00074     showseconds = true;
00075   }
00076   if (showseconds)
00077   {
00078     ss << szTimeSep;
00079     ss << std::setw(2) << fst.m_systemtime.wSecond;
00080   }
00081   if (showmilliseconds)
00082     {
00083       ss << szTimeSep;
00084       ss << std::setw(3) << fst.m_systemtime.wMilliseconds;
00085     }
00086   os << ss.str();
00087   return os;
00088 }
00089 int format_system_time::width(int options)
00090 {
00091   int width = 16;
00092   if (options & show_seconds)
00093     width += 3;
00094   if (options & show_milliseconds)
00095   {
00096     width += 4;
00097     if ((options & show_seconds) == 0)
00098       width += 3;
00099   }
00100   return width;
00101 }

Generated on Sun Jun 26 13:43:46 2005 for pingem by  doxygen 1.4.3