BlackLib::BlackTime Class Reference

Interacts with end user, to do basic time operations. More...

#include <BlackTime.h>

Public Member Functions

 BlackTime ()
 Constructor of BlackTime class. More...
 
 BlackTime (int h, int m, int s)
 Constructor of BlackTime class. More...
 
virtual ~BlackTime ()
 Destructor of BlackTime class. More...
 
int getHour ()
 Exports hour value. More...
 
int getMinute ()
 Exports minute value. More...
 
int getSecond ()
 Exports second value. More...
 
bool setTime (int h, int m, int s)
 Sets new time values if they are valid. More...
 
void start ()
 Starts the time measurement. More...
 
BlackTimeElapsed restart ()
 Restarts the time measurement. More...
 
BlackTimeElapsed elapsed ()
 Exports the elapsed time. More...
 
BlackTimeaddHour (int h)
 Adds hours to current hour value. More...
 
BlackTimeaddMinute (int m)
 Adds minutes to current minute value. More...
 
BlackTimeaddSecond (int s)
 Adds seconds to current second value. More...
 
std::string toString (std::string format)
 Exports readable and formatted version of the time. More...
 
BlackTime operator- (BlackTime t)
 Calculates difference of two BlackTime. More...
 
BlackTime operator+ (BlackTime t)
 Calculates sum of two BlackTime. More...
 
BlackTimeoperator= (BlackTime t)
 Assigns one BlackTime to other one. More...
 
bool operator== (BlackTime t)
 Checks equality of two BlackTime. More...
 
bool operator!= (BlackTime t)
 Checks not equality of two BlackTime. More...
 
bool operator< (BlackTime t)
 Compares two BlackTime. More...
 
bool operator> (BlackTime t)
 Compares two BlackTime. More...
 
bool operator<= (BlackTime t)
 Compares two BlackTime. More...
 
bool operator>= (BlackTime t)
 Compares two BlackTime. More...
 

Static Public Member Functions

static bool isValid (int h, int m, int s)
 Checks the validation of input parameters. More...
 
static BlackTime getCurrentTime ()
 Exports the current system time. More...
 
static BlackTime fromSecondToTime (long int s)
 Converts seconds to BlackTime. More...
 
static long int fromTimeToSecond (BlackTime t)
 Converts BlackTime to seconds. More...
 

Private Attributes

int hour
 is used to hold the hour value More...
 
int minute
 is used to hold the minute value More...
 
int second
 is used to hold the second value More...
 
timeval measureStartTime
 is used to hold the start point for measuring elapsed time More...
 

Detailed Description

Interacts with end user, to do basic time operations.

This class is end node to do basic time operations, to get current time and to measure the elapsed time.

Example
EXAMPLE PROJECT FILE TREE:

   myTimeProject
   |-> src
       |-> BlackLib
           |-> BlackDirectory
               |-> BlackDirectory.cpp
               |-> BlackDirectory.h
           |-> BlackThread
               |-> BlackThread.cpp
               |-> BlackThread.h
           |-> BlackMutex
               |-> BlackMutex.cpp
               |-> BlackMutex.h
           |-> BlackTime
               |-> BlackTime.cpp
               |-> BlackTime.h
           |-> BlackADC
               |-> BlackADC.cpp
               |-> BlackADC.h
           |-> BlackGPIO
               |-> BlackGPIO.cpp
               |-> BlackGPIO.h
           |-> BlackI2C
               |-> BlackI2C.cpp
               |-> BlackI2C.h
           |-> BlackPWM
               |-> BlackPWM.cpp
               |-> BlackPWM.h
           |-> BlackSPI
               |-> BlackSPI.cpp
               |-> BlackSPI.h
           |-> BlackUART
               |-> BlackUART.cpp
               |-> BlackUART.h
           |-> BlackCore.cpp
           |-> BlackCore.h
           |-> BlackDef.h
           |-> BlackErr.h
           |-> BlackLib.h
       |-> myTimeProject.cpp


If BlackLib source files are located in your project like above example project file tree, you have to include BlackTime.h or another source files with adding this line to your project file (myTimeProject.cpp at the example):
#include "BlackLib/BlackTime/BlackTime.h"


If BlackLib source files are located at same level with your project file (myTimeProject.cpp at the example), you have to include BlackTime.h or another source files with adding this line to your project file:

// Filename: myTimeProject.cpp
// Author: Yiğit Yüce - ygtyce@gmail.com
#include <iostream>
#include "BlackLib/BlackTime/BlackTime.h"
int main()
{
BlackLib::BlackTime myTime(16,41,34);
std::cout << myTime.toString("'Formatted time: ' hh:mm:ss AP");
return 0;
}


You can use "using namespace BlackLib" also. You can get rid of writing "BlackLib::", with using this method.
// Filename: myTimeProject.cpp
// Author: Yiğit Yüce - ygtyce@gmail.com
#include <iostream>
#include "BlackLib/BlackTime/BlackTime.h"
using namespace BlackLib;
int main()
{
BlackTime myTime(16,41,34);
std::cout << myTime.toString("'Formatted time: ' hh:mm:ss AP");
return 0;
}

Constructor & Destructor Documentation

BlackLib::BlackTime::BlackTime ( )

Constructor of BlackTime class.

This function initializes time values to 0.

Example
std::cout << myTime.getHour() << std::endl;
std::cout << myTimePtr->getHour();
BlackLib::BlackTime::BlackTime ( int  h,
int  m,
int  s 
)

Constructor of BlackTime class.

Parameters
[in]hhour value
[in]mminute value
[in]ssecond value

This function initializes time values to input parameters if they are valid.

Example
BlackLib::BlackTime myTime(16,41,34);
BlackLib::BlackTime *myTimePtr = new BlackLib::BlackTime(16,41,34);
std::cout << myTime.getHour() << std::endl;
std::cout << myTimePtr->getHour();
See also
BlackTime::isValid()
BlackLib::BlackTime::~BlackTime ( )
virtual

Destructor of BlackTime class.

Member Function Documentation

BlackTime & BlackLib::BlackTime::addHour ( int  h)

Adds hours to current hour value.

Users can add negative value also. If current hour is less than 0 or greater than 24 after the add operation, it will convert the 24 hours time system.

Returns
the new time value.
Example
BlackLib::BlackTime myTime(16,41,34);
myTime.addHour(5);
std::cout << myTime.toString("'Formatted time: 'hh:mm:ss") << std::endl;
myTime.addHour(-28);
std::cout << myTime.toString("'Formatted time: 'hh:mm:ss") << std::endl;
myTime.addHour(17);
std::cout << myTime.toString("'Formatted time: 'hh:mm:ss") << std::endl;
// Possible Output:
// Formatted time: 21:41:34
// Formatted time: 17:41:34
// Formatted time: 10:41:34
BlackTime & BlackLib::BlackTime::addMinute ( int  m)

Adds minutes to current minute value.

Users can add negative value also. If current minute is less than 0 or greater than 60 after the add operation, it will convert the 24 hours time system.

Returns
the new time value.
Example
BlackLib::BlackTime myTime(16,41,34);
myTime.addMinute(5);
std::cout << myTime.toString("'Formatted time: 'hh:mm:ss") << std::endl;
myTime.addMinute(-65);
std::cout << myTime.toString("'Formatted time: 'hh:mm:ss") << std::endl;
myTime.addMinute(20);
std::cout << myTime.toString("'Formatted time: 'hh:mm:ss") << std::endl;
// Possible Output:
// Formatted time: 16:46:34
// Formatted time: 15:41:34
// Formatted time: 16:01:34
BlackTime & BlackLib::BlackTime::addSecond ( int  s)

Adds seconds to current second value.

Users can add negative value also. If current second is less than 0 or greater than 60 after the add operation, it will convert the 24 hours time system.

Returns
the new time value.
Example
BlackLib::BlackTime myTime(16,41,34);
myTime.addSecond(5);
std::cout << myTime.toString("'Formatted time: 'hh:mm:ss") << std::endl;
myTime.addSecond( -1*(1*3600 + 10*60 + 9) );
std::cout << myTime.toString("'Formatted time: 'hh:mm:ss") << std::endl;
myTime.addSecond(50);
std::cout << myTime.toString("'Formatted time: 'hh:mm:ss") << std::endl;
// Possible Output:
// Formatted time: 16:41:39
// Formatted time: 15:31:30
// Formatted time: 15:32:20
BlackTimeElapsed BlackLib::BlackTime::elapsed ( )

Exports the elapsed time.

Returns
the elapsed time between the current and start/restart function calls
Example
Example usage is shown in BlackTime::start() function's example.
BlackTime BlackLib::BlackTime::fromSecondToTime ( long int  s)
static

Converts seconds to BlackTime.

Parameters
[in]ssecond value
Example
std::cout << "Example elapsed time in BlackTime: "
<< BlackLib::BlackTime::fromSecondToTime(37230).toString("hh 'hours, 'mm 'minutes, 'ss 'seconds.'");
// Possible Output:
// Example elapsed time in BlackTime: 10 hours, 20 minutes, 30 seconds.
long int BlackLib::BlackTime::fromTimeToSecond ( BlackTime  t)
static

Converts BlackTime to seconds.

Parameters
[in]tBlackTime value
Example
BlackLib::BlackTime myTime(10,20,30);
std::cout << "Example elapsed time in seconds: "
// Possible Output:
// Example elapsed time in seconds: 37230
BlackTime BlackLib::BlackTime::getCurrentTime ( )
static

Exports the current system time.

Example
std::cout << "Current time: " << myTime.getCurrentTime().toString("hh.mm.ss");
// Possible Output:
// Current time: 15.30.56
int BlackLib::BlackTime::getHour ( )

Exports hour value.

Example
BlackLib::BlackTime *myTimePtr = new BlackLib::BlackTime(16,41,34);
std::cout << "myTime\'s hour value : " << myTime.getHour() << std::endl;
std::cout << "myTimePtr\'s hour value: " << myTimePtr->getHour();
// Possible Output:
// myTime's hour value : 0
// myTimePtr's hour value: 16
int BlackLib::BlackTime::getMinute ( )

Exports minute value.

Example
BlackLib::BlackTime *myTimePtr = new BlackLib::BlackTime(16,41,34);
std::cout << "myTime\'s minute value : " << myTime.getMinute() << std::endl;
std::cout << "myTimePtr\'s minute value: " << myTimePtr->getMinute();
// Possible Output:
// myTime's minute value : 0
// myTimePtr's minute value: 41
int BlackLib::BlackTime::getSecond ( )

Exports second value.

Example
BlackLib::BlackTime *myTimePtr = new BlackLib::BlackTime(16,41,34);
std::cout << "myTime\'s second value : " << myTime.getSecond() << std::endl;
std::cout << "myTimePtr\'s second value: " << myTimePtr->getSecond();
// Possible Output:
// myTime's second value : 0
// myTimePtr's second value: 34
bool BlackLib::BlackTime::isValid ( int  h,
int  m,
int  s 
)
static

Checks the validation of input parameters.

Hour must be greater than 0 and less than 24, minute must be greater than 0 and less than 60, second must be greater than 0 and less than 60.

Parameters
[in]hhour value
[in]mminute value
[in]ssecond value
Returns
true if all values are valid else false.
Example
{
myTime.setTime(12,34,56);
}
else
{
myTime.setTime(0,0,0);
}
std::cout << myTime.toString("'Formatted myTime: ' hh:mm:ss") << std::endl;
// Possible Output:
// Formatted myTime: 12:34:56
bool BlackLib::BlackTime::operator!= ( BlackTime  t)

Checks not equality of two BlackTime.

Parameters
[in]tBlackTime value
Example
BlackLib::BlackTime myTime1(10,20,30);
BlackLib::BlackTime myTime2(3,6,39);
std::cout << "is not equal? : " << std::boolalpha << (myTime1 != myTime2);
// Possible Output:
// is not equal? : true
BlackTime BlackLib::BlackTime::operator+ ( BlackTime  t)

Calculates sum of two BlackTime.

Parameters
[in]tBlackTime value
Example
BlackLib::BlackTime myTime1(10,20,30);
BlackLib::BlackTime myTime2(3,6,39);
std::cout << "Sum: "
<< (myTime1+myTime2).toString("hh 'hours, 'mm 'minutes, 'ss 'seconds.'") << std::endl;
// Possible Output:
// Sum: 13 hours, 27 minutes, 09 seconds.
BlackTime BlackLib::BlackTime::operator- ( BlackTime  t)

Calculates difference of two BlackTime.

Parameters
[in]tBlackTime value
Example
BlackLib::BlackTime myTime1(10,20,30);
BlackLib::BlackTime myTime2(3,6,9);
std::cout << "Difference1: "
<< (myTime1-myTime2).toString("hh 'hours, 'mm 'minutes, 'ss 'seconds.'") << std::endl;
std::cout << "Difference2: "
<< (myTime2-myTime1).toString("hh 'hours, 'mm 'minutes, 'ss 'seconds.'") << std::endl;
// Possible Output:
// Difference1: 07 hours, 14 minutes, 21 seconds.
// Difference2: 16 hours, 45 minutes, 39 seconds.
bool BlackLib::BlackTime::operator< ( BlackTime  t)

Compares two BlackTime.

Parameters
[in]tBlackTime value
Example
BlackLib::BlackTime myTime1(10,20,30);
BlackLib::BlackTime myTime2(3,6,39);
std::cout << "is lesser than myTime2? : " << std::boolalpha << (myTime1 < myTime2);
// Possible Output:
// is lesser than myTime2? : false
bool BlackLib::BlackTime::operator<= ( BlackTime  t)

Compares two BlackTime.

Parameters
[in]tBlackTime value
Example
BlackLib::BlackTime myTime1(10,20,30);
BlackLib::BlackTime myTime2(3,6,39);
std::cout << "is lesser than or equal to myTime2? : " << std::boolalpha << (myTime1 <= myTime2);
// Possible Output:
// is lesser than or equal to myTime2? : false
BlackTime & BlackLib::BlackTime::operator= ( BlackTime  t)

Assigns one BlackTime to other one.

Parameters
[in]tBlackTime value
Example
BlackLib::BlackTime myTime1(10,20,30);
myTime2 = myTime1;
std::cout << "myTime2 = " << myTime2.toString("hh:mm:ss");
// Possible Output:
// myTime2 = 10:20:30
bool BlackLib::BlackTime::operator== ( BlackTime  t)

Checks equality of two BlackTime.

Parameters
[in]tBlackTime value
Example
BlackLib::BlackTime myTime1(10,20,30);
BlackLib::BlackTime myTime2(3,6,39);
std::cout << "is equal? : " << std::boolalpha << (myTime1 == myTime2);
// Possible Output:
// is equal? : false
bool BlackLib::BlackTime::operator> ( BlackTime  t)

Compares two BlackTime.

Parameters
[in]tBlackTime value
Example
BlackLib::BlackTime myTime1(10,20,30);
BlackLib::BlackTime myTime2(3,6,39);
std::cout << "is greater than myTime2? : " << std::boolalpha << (myTime1 > myTime2);
// Possible Output:
// is greater than myTime2? : true
bool BlackLib::BlackTime::operator>= ( BlackTime  t)

Compares two BlackTime.

Parameters
[in]tBlackTime value
Example
BlackLib::BlackTime myTime1(10,20,30);
BlackLib::BlackTime myTime2(3,6,39);
std::cout << "is greater than or equal to myTime2? : " << std::boolalpha << (myTime1 >= myTime2);
// Possible Output:
// is greater than or equal to myTime2? : true
BlackTimeElapsed BlackLib::BlackTime::restart ( )

Restarts the time measurement.

Returns
the elapsed time between the current and start/restart function calls
Example
Example usage is shown in BlackTime::start() function's example.
bool BlackLib::BlackTime::setTime ( int  h,
int  m,
int  s 
)

Sets new time values if they are valid.

Parameters
[in]hnew hour value
[in]mnew minute value
[in]snew second value
Example
BlackLib::BlackTime *myTimePtr = new BlackLib::BlackTime(16,41,34);
myTime.setTime(12,34,56);
std::cout << myTime.toString("'Formatted myTime : ' hh:mm:ss") << std::endl;
std::cout << myTimePtr->toString("'Formatted myTimePtr: ' hh.mm.ss AP");
// Possible Output:
// Formatted myTime : 12:34:56
// Formatted myTimePtr: 04.41.34
void BlackLib::BlackTime::start ( )

Starts the time measurement.

Example
myTime.start();
// do some long operations
BlackLib::BlackTimeElapsed et = tm.restart();
std::cout << "elapsed hour :" << et.hour << std::endl
<< "elapsed minute :" << et.minute << std::endl
<< "elapsed second :" << et.second << std::endl
<< "elapsed msecond:" << et.miliSecond << std::endl
<< "elapsed usecond:" << et.microSecond << std::endl << std::endl;
// do some long operations again
// this measures elapsed time between the restart and current, not start and current.
et = tm.elapsed();
std::cout << "elapsed hour :" << et.hour << std::endl
<< "elapsed minute :" << et.minute << std::endl
<< "elapsed second :" << et.second << std::endl
<< "elapsed msecond:" << et.miliSecond << std::endl
<< "elapsed usecond:" << et.microSecond << std::endl;
// Possible Output:
// elapsed hour :00
// elapsed minute :5
// elapsed second :43
// elapsed msecond:56
// elapsed usecond:29
// elapsed hour :00
// elapsed minute :12
// elapsed second :6
// elapsed msecond:37
// elapsed usecond:28
std::string BlackLib::BlackTime::toString ( std::string  format)

Exports readable and formatted version of the time.

Formatted text rules:

Expression Output
'plain text' plain text
single 'h' or 'H' hour without zero
'hh' or 'HH' or 'hH' or 'Hh' hour with zero
single 'm' or 'M' minute without zero
'mm' or 'MM' or 'mM' or 'HM' minute with zero
single 's' or 'S' second without zero
'ss' or 'SS' or 'sS' or 'Ss' second with zero
seperator allowed seperators are ':', '/', '.', '-' and ' '
'ap' or 'AP' prints "am" or "pm"
Returns
formatted time string.
Example
BlackLib::BlackTime myTime(16,5,34);
std::cout << myTime.toString("'Formatted time : 'h:m:s") << std::endl;
std::cout << myTime.toString("'12-hours system: 'hh-mm/ss ap") << std::endl;
std::cout << myTime.toString("hh 'hours,' mm 'minutes, 'ss' seconds elapsed.\n'");
// Possible Output:
// Formatted time : 16:5:34
// 12-hours system: 04-05/34 pm
// 16 hours, 05 minutes, 34 seconds elapsed.

Member Data Documentation

int BlackLib::BlackTime::hour
private

is used to hold the hour value

timeval BlackLib::BlackTime::measureStartTime
private

is used to hold the start point for measuring elapsed time

int BlackLib::BlackTime::minute
private

is used to hold the minute value

int BlackLib::BlackTime::second
private

is used to hold the second value


The documentation for this class was generated from the following files: