Difference between revisions of "Visual Studio C++ Libraries DateTimeMillisecondSpan"

From Minor Miracle Software
Jump to: navigation, search
 
(No difference)

Latest revision as of 20:48, 24 February 2021

DateTimeMillisecondSpan.h

// ************************************************************************* //
// Filename:                                                                 //
//  DateTimeMillisecondSpan.h - Header file.                                 //
//                                                                           //
// Description:                                                              //
// Holds a time span, positive or negative, with millisecond accuracy.       //
//                                                                           //
// Authors:                                                                  //
//  JBD  Jesse B. Dooley                                                     //
//                                                                           //
// Design:                                                                   //
// 1. Use CTimeSpan for conversions.                                         //
// 2. Positive or negative is the Seconds__time64_t sign.                    //
// ************************************************************************* //


#ifndef _DateTimeMillisecondSpan_
#define _DateTimeMillisecondSpan_

#include <SDKDDKVer.h>
#include "stdafx.h"
#include <time.h>
#include <math.h>
#include <stdint.h>
#include <ATLComTime.h>


class DateTimeMillisecondSpan {

public:
	// Constructors
	DateTimeMillisecondSpan();
	DateTimeMillisecondSpan( int sec, int millisecond ); // Set time in Seconds and Milliseconds
	DateTimeMillisecondSpan( __time64_t s, int32_t mil );
	DateTimeMillisecondSpan( CTimeSpan d, int32_t mil );

	// Destuctor
	~DateTimeMillisecondSpan();

// distributions
	int GetDays();                // Get modulus days in this timespan
	int GetHours();               // Get modulus hours in this timespan 
	int GetMinutes();             // Get modulus minutes in this timespan
	int GetSeconds();             // Get modulus seconds in this timespan
	__time64_t GetTotalSeconds() const;
	int32_t    GetMilliseconds() const;

// operator overloads
	bool operator==( const DateTimeMillisecondSpan& rhs) const;
	void operator=( const DateTimeMillisecondSpan& rhs);
	bool operator>( const DateTimeMillisecondSpan& rhs) const;
	bool operator>=( const DateTimeMillisecondSpan& rhs) const;
	bool operator<( const DateTimeMillisecondSpan& rhs) const;
	bool operator<=( const DateTimeMillisecondSpan& rhs) const;
	DateTimeMillisecondSpan operator-( const DateTimeMillisecondSpan& rhs);
	DateTimeMillisecondSpan operator+( const DateTimeMillisecondSpan& rhs);

// String Formatting
	int PrintTimeSpan (char* ntxt, size_t len);
	CString LongTimeStamp();
	CString ShortTimeStamp();
	bool IsNegative();

	void Set( __time64_t, int32_t);

private:
	__time64_t Seconds__time64_t; // Seconds
	int32_t Milliseconds_int32_t; // Microseconds

	// Rollover second if milliseconds >= 1000
	void rollover();

};


#endif // _DateTimeMillisecondSpan_

DateTimeMillisecondSpan.cpp


#include "DateTimeMillisecondSpan.h"

// Constructors
DateTimeMillisecondSpan::DateTimeMillisecondSpan() {
	Seconds__time64_t = 0;
	Milliseconds_int32_t = 0;
}

 // Set time in Seconds and Milliseconds
DateTimeMillisecondSpan::DateTimeMillisecondSpan( int sec, int millisecond ) {

	Seconds__time64_t = sec;
	Milliseconds_int32_t = millisecond;
	rollover();
}

DateTimeMillisecondSpan::DateTimeMillisecondSpan( __time64_t sec, int32_t millisecond ) {

	Seconds__time64_t = sec;
	Milliseconds_int32_t = millisecond;
	rollover();
}

// CTimeSpan is never negative
DateTimeMillisecondSpan::DateTimeMillisecondSpan( CTimeSpan d, int32_t millisecond ) {

	Seconds__time64_t = d.GetTotalSeconds();
	Milliseconds_int32_t = millisecond;
	rollover();
}


// Destuctor
DateTimeMillisecondSpan::~DateTimeMillisecondSpan() {}

// distributions
int DateTimeMillisecondSpan::GetDays() {
	return (int) Seconds__time64_t /86400;
}

int DateTimeMillisecondSpan::GetHours() {
	__time64_t lamb_t = Seconds__time64_t;
	lamb_t %= 86400;      // remove days
	return (int) lamb_t / 3600; // return hours
}

int DateTimeMillisecondSpan::GetMinutes() {
	__time64_t lamb_t = Seconds__time64_t;
	lamb_t %= 86400;  // remove days
	lamb_t %= 3600;   // remove hours
	return (int) lamb_t / 60; // return minutes
}

int DateTimeMillisecondSpan::GetSeconds() {
	__time64_t lamb_t = Seconds__time64_t;
	lamb_t %= 86400;  // remove days
	lamb_t %= 3600;   // remove hours
	lamb_t %= 60;     // remove minutes
	return (int) lamb_t;    // return seconds
}

__time64_t DateTimeMillisecondSpan::GetTotalSeconds() const {
	return Seconds__time64_t;
}

int32_t DateTimeMillisecondSpan::GetMilliseconds() const {
	return Milliseconds_int32_t;
}


// operator overloads for DateTimeMillisecondSpan
bool DateTimeMillisecondSpan::operator==( const DateTimeMillisecondSpan& rhs) const {
		return 	(Seconds__time64_t == rhs.Seconds__time64_t) &&
				(Milliseconds_int32_t == rhs.Milliseconds_int32_t);
}

// Assignment operator
void DateTimeMillisecondSpan::operator=( const DateTimeMillisecondSpan& rhs) 
{
	Seconds__time64_t    = rhs.Seconds__time64_t;
	Milliseconds_int32_t = rhs.Milliseconds_int32_t;
	rollover();
}

bool DateTimeMillisecondSpan::operator>( const DateTimeMillisecondSpan& rhs) const {

	if( this->GetTotalSeconds() > rhs.GetTotalSeconds() )
			return true;
		else if( this->GetTotalSeconds() == rhs.GetTotalSeconds() &&
			this->GetMilliseconds() > rhs.GetMilliseconds() )
			return true;
	return false;
}

bool DateTimeMillisecondSpan::operator<( const DateTimeMillisecondSpan& rhs) const {

	if( this->GetTotalSeconds() < rhs.GetTotalSeconds() )
		return true;
	else if( this->GetTotalSeconds() == rhs.GetTotalSeconds() &&
		this->GetMilliseconds() < rhs.GetMilliseconds() )
		return true;

	return false;
}

bool DateTimeMillisecondSpan::operator>=( const DateTimeMillisecondSpan& rhs) const {

	if( this->GetTotalSeconds() >= rhs.GetTotalSeconds() )
			return true;
		else if( this->GetTotalSeconds() == rhs.GetTotalSeconds() &&
			this->GetMilliseconds() >= rhs.GetMilliseconds() )
			return true;

	return false;
}

bool DateTimeMillisecondSpan::operator<=( const DateTimeMillisecondSpan& rhs) const {

	if( this->GetTotalSeconds() <= rhs.GetTotalSeconds() )
			return true;
		else if( this->GetTotalSeconds() == rhs.GetTotalSeconds() &&
			this->GetMilliseconds() <= rhs.GetMilliseconds() )
			return true;

	return false;
}

DateTimeMillisecondSpan DateTimeMillisecondSpan::operator-( const DateTimeMillisecondSpan& rhs) {

	return DateTimeMillisecondSpan((*this).Seconds__time64_t - rhs.Seconds__time64_t, 
		 (*this).Milliseconds_int32_t - rhs.Milliseconds_int32_t );
}

DateTimeMillisecondSpan DateTimeMillisecondSpan::operator+( const DateTimeMillisecondSpan& rhs) {

	return DateTimeMillisecondSpan((*this).Seconds__time64_t + rhs.Seconds__time64_t, 
		 (*this).Milliseconds_int32_t + rhs.Milliseconds_int32_t );
}


// rollover milliseconds to seconds
void DateTimeMillisecondSpan::rollover() {

	// Milliseconds_int32_t >= 1000
	if( Milliseconds_int32_t >= 1000  ) {
		__time64_t seconds_int = Milliseconds_int32_t / 1000;
		int32_t rollover_int = Milliseconds_int32_t % 1000;
		Seconds__time64_t += seconds_int;
		Milliseconds_int32_t = rollover_int;
	} 
	// Milliseconds_int32_t < 0
	// miliseconds < 0 rollunder
	if( Milliseconds_int32_t < 0  ) {
		__time64_t seconds_int = Milliseconds_int32_t / 1000; // Save the negative seconds
		int32_t rollover_int = Milliseconds_int32_t % 1000; // Remove the negative seconds
		rollover_int += 1000; // miliseconds is negative so this is subtraction.
		Seconds__time64_t += seconds_int;  // since seconds_int is always negative here the line subtracts
		Milliseconds_int32_t = rollover_int; // millisecond is always positive
	}
}

int DateTimeMillisecondSpan::PrintTimeSpan (char* ntxt, size_t len) {

// From FormatClass
//  const char* const fmt="%d %02hd:%02hd:%02hd.%01hd";

	if( IsNegative() ) {
	  if ( GetDays() >0 )                  // Are we using days?
		 return sprintf_s(ntxt,len,"-%d %02d:%02d:%02d.%01d",
		  GetDays() * -1,
		  GetHours() * -1,
		  GetMinutes() * -1,
		  GetSeconds() * -1,
		  GetMilliseconds() );
	  else                                // Shorter format if less than one day.
		return sprintf_s(ntxt,len,"-%02d:%02d:%02d.%01d", // Skip over "%hu ".
		  GetHours() * -1,
		  GetMinutes() * -1,
		  GetSeconds() * -1,
		  GetMilliseconds() );
	} else { 
	  if ( GetDays() >0 )                  // Are we using days?
		 return sprintf_s(ntxt,len,"%d %02d:%02d:%02d.%01d",
		  GetDays(),
		  GetHours(),
		  GetMinutes(),
		  GetSeconds(),
		  GetMilliseconds() );
	  else                                // Shorter format if less than one day.
		return sprintf_s(ntxt,len,"%02d:%02d:%02d.%01d", // Skip over "%d ".
		  GetHours(),
		  GetMinutes(),
		  GetSeconds(),
		  GetMilliseconds() );
	}
}

// Fetch as "HH:MM:SS.sss"
CString DateTimeMillisecondSpan::LongTimeStamp() {
	CString return_cs;

	if( IsNegative() ) {
	  if ( GetDays() >0 )                  // Are we using days?
		 return_cs.Format("-%d Days %02d:%02d:%02d.%01d",
		  GetDays() * -1,
		  GetHours() * -1,
		  GetMinutes() * -1,
		  GetSeconds() * -1,
		  GetMilliseconds() );
	  else                                // Shorter format if less than one day.
		return_cs.Format("-%02d:%02d:%02d.%01d", // Skip over "%d ".
		  GetHours() * -1,
		  GetMinutes() * -1,
		  GetSeconds() * -1,
		  GetMilliseconds() );
	} else { 
	  if ( GetDays() >0 )                  // Are we using days?
		 return_cs.Format("%d Days %02d:%02d:%02d.%01d",
		  GetDays(),
		  GetHours(),
		  GetMinutes(),
		  GetSeconds(),
		  GetMilliseconds() );
	  else                                // Shorter format if less than one day.
		return_cs.Format("%02d:%02d:%02d.%01d", // Skip over "%d ".
		  GetHours(),
		  GetMinutes(),
		  GetSeconds(),
		  GetMilliseconds() );
	}

	return return_cs;
}


// Fetch as "MM:SS.sss"
CString DateTimeMillisecondSpan::ShortTimeStamp() {
	CString return_cs;

	// MM:SS.sss
	if( IsNegative() ){
		return_cs.Format( "-%02d:%02d.%03d",
			GetMinutes() * -1,
			GetSeconds() * -1,
			Milliseconds_int32_t);
	} else {
		return_cs.Format( "%02d:%02d.%03d",
			GetMinutes(),
			GetSeconds(),
			Milliseconds_int32_t);

	}

	return return_cs;
}

bool DateTimeMillisecondSpan::IsNegative() {
	return Seconds__time64_t < 0;
}

void DateTimeMillisecondSpan::Set( __time64_t a, int32_t b) {
	Seconds__time64_t = a;
	Milliseconds_int32_t = b;
}


Internal Links

Parent Article: Visual Studio C++ Libraries