All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ProgressTimer.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/progress/ProgressTimer.cpp
22 
23  \brief The ProgressTimer is a utility class that can be used to calculate the estimated time to finish a task.
24 */
25 
26 // TerraLib
27 #include "../StringUtils.h"
28 #include "../Translator.h"
29 #include "ProgressTimer.h"
30 
31 te::common::ProgressTimer::ProgressTimer(int totalSteps, std::string message)
32  : m_totalSteps(totalSteps),
33  m_count(0),
34  m_startTime(0),
35  m_remainingTime(0.0),
36  m_speedTime(0.0),
37  m_message(message)
38 {
39 }
40 
42 {
43 }
44 
46 {
47 // start time
48  time(&m_startTime);
49 
50  m_count = 0;
51 }
52 
54 {
55  ++m_count;
56 
57  time_t curtime;
58 
59  // current time
60  time(&curtime);
61 
62  // difference in seconds
63  double diffInSec = difftime(curtime, m_startTime);
64 
65  m_speedTime = (static_cast<double>(m_count) / diffInSec);
66 
67  int remainingSteps = m_totalSteps - m_count;
68 
69  double remainingTimeInSec = (static_cast<double>(remainingSteps) / m_speedTime);
70 
71  m_remainingTime = remainingTimeInSec / 60.0;
72 }
73 
75 {
76  m_totalSteps = totalSteps;
77 }
78 
79 void te::common::ProgressTimer::setMessage(std::string message)
80 {
81  m_message = message;
82 }
83 
85 {
86  return m_remainingTime;
87 }
88 
90 {
91  return m_speedTime;
92 }
93 
95 {
96  std::string strTime = m_message + "\n";
97 
98  if(m_remainingTime < 1.0)
99  {
100  strTime += TE_TR("Remaining Time: Less than one minute");
101  }
102  else
103  {
104  strTime += TE_TR("Remaining Time: ") + te::common::Convert2String(m_remainingTime, 1);
105  strTime += TE_TR(" minute(s) - Speed: ") + te::common::Convert2String(m_speedTime, 2);
106  strTime += TE_TR(" Steps/Second");
107  }
108 
109  return strTime;
110 }
void setMessage(std::string message)
Set the message used by task progress.
void tick()
Define a new step process evolution.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
double getRemainingTimeInMin() const
Function used to get the remaining time to end the process.
double getSpeedTimeInSec() const
Function used to get the speed time.
void start()
Start the internal timer.
ProgressTimer(int totalSteps, std::string message)
It initializes a ProgressTimer.
std::string getMessage()
Get the information about the evolution of the process.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:51
void setTotalSteps(int totalSteps)
Set the total steps.
The ProgressTimer is a utility class that can be used to calculate the estimated time to finish a tas...