Logo
~Apps~
~Projects~
~Contact~


Clock.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2005  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #include <stdio.h>
00024 
00025 #include "Clock.h"
00026 
00027 
00028 
00029 
00030 Clock::Clock()
00031 {
00032 #ifdef _WIN32
00033         ::GetLocalTime(&m_time);
00034         ::GetLocalTime(&m_time0);
00035 #else
00036         gettimeofday(&m_tv, &m_tz);
00037         gettimeofday(&m_tv0, &m_tz0);
00038 #endif
00039 }
00040 
00041 
00042 Clock::~Clock()
00043 {
00044 }
00045 
00046 
00047 void Clock::PrintDiff(const std::string& str)
00048 {
00049 #ifdef _WIN32
00050         SYSTEMTIME time;
00051         SYSTEMTIME time0;
00052         ::GetLocalTime(&time);
00053         ::GetLocalTime(&time0);
00054         time.wSecond -= m_time.wSecond;
00055         if (time.wSecond < 0)
00056                 time.wSecond += 60;
00057         {
00058                 time_t t = time.wSecond * 1000 + time.wMilliseconds - m_time.wMilliseconds;
00059                 time_t s = t / 1000;
00060                 time_t m = t % 1000;
00061                 if (!str.size())
00062                         printf("Time: %d.%03d", s, m);
00063                 else
00064                         printf("Time %s: %d.%03d", str.c_str(), s, m);
00065         }
00066         time0.wSecond -= m_time0.wSecond;
00067         if (time0.wSecond < 0)
00068                 time0.wSecond += 60;
00069         {
00070                 time_t t = time0.wSecond * 1000 + time0.wMilliseconds - m_time0.wMilliseconds;
00071                 time_t s = t / 1000;
00072                 time_t m = t % 1000;
00073                 if (!str.size())
00074                         printf("Time: %d.%03d", s, m);
00075                 else
00076                         printf("Time %s: %d.%03d", str.c_str(), s, m);
00077         }
00078 #else
00079         struct timeval tv;
00080         struct timezone tz;
00081         struct timeval tv0;
00082         struct timezone tz0;
00083         gettimeofday(&tv, &tz);
00084         gettimeofday(&tv0, &tz0);
00085         tv.tv_sec -= m_tv.tv_sec;
00086         {
00087                 time_t t = tv.tv_sec * 1000000 + tv.tv_usec - m_tv.tv_usec;
00088                 time_t s = t / 1000000;
00089                 time_t u = t % 1000000;
00090                 if (!str.size())
00091                         printf("Time: %d.%06d", s, u);
00092                 else
00093                         printf("Time %s: %d.%06d", str.c_str(), s, u);
00094         }
00095         tv0.tv_sec -= m_tv0.tv_sec;
00096         {
00097                 time_t t = tv0.tv_sec * 1000000 + tv0.tv_usec - m_tv0.tv_usec;
00098                 time_t s = t / 1000000;
00099                 time_t u = t % 1000000;
00100                 if (!str.size())
00101                         printf(" / %d.%06d", s, u);
00102                 else
00103                         printf(" / %d.%06d", s, u);
00104         }
00105         printf("\n");
00106 #endif
00107         Reset();
00108 }
00109 
00110 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4