//------------------------------------------------------------------------------
// <copyright file="LqrSMACTimeRange.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
//   Declarations for the TimeRange class, which stores a range time timeval
//   values and has methods for comparing them.
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __LqrSMACTimeRange_h__
#define __LqrSMACTimeRange_h__

#include "LqrSMACSharedLib.h"
#include <string>
#include <sys/time.h>

namespace LiquidRobotics {
namespace SMACFramework {

class SHARED_LIB_PUBLIC TimeRange
{
public:

	typedef std::string string;

	struct GetTimevalStrings
	{
		GetTimevalStrings(const timeval& tv);
		string ToString(bool compressed = false) const;

		string dateStr;
		string compressedDateStr;
		string timeStr;
		string compressedTimeStr;
		string msStr;
	};

	static const timeval BadValue;
	static const timeval MinValue;
	static const timeval MaxValue;

	TimeRange(bool _isGood = true);
	TimeRange(const timeval& _tv, bool _isGood = true);
	TimeRange(time_t _from, time_t _to, bool _isGood = true);
	TimeRange(const timeval& _from, const timeval& _to, bool _isGood = true);
	TimeRange(const TimeRange& rhs);

	bool ExtendRange(const timeval& tv);
	bool Intersects(const TimeRange& other) const;
	bool InRange(const timeval& tv) const;

	operator bool() const;

	timeval GetMin() const;
	timeval GetMax() const;

	void SetGood(bool newValue);
	void SetMin(time_t t);
	void SetMin(const timeval& tv);
	void SetMax(time_t t);
	void SetMax(const timeval& tv);
	void SetRange(const timeval& _tv, bool _isGood = true);
	void SetRange(time_t _from, time_t _to, bool _isGood = true);
	void SetRange(const timeval& _from, const timeval& _to, bool _isGood = true);

	std::string ToString() const;

	bool ValidRange() const;

	static int Compare(const timeval& a, const timeval& b);
	static timeval TimeWithFraction(time_t sec, __suseconds_t usec);
	static bool FromString(const string& dateStr, const string& timeStr, timeval& tv, bool doThrow = false);
	static std::string ToString(const timeval& tv);

private:

	timeval from;
	timeval to;
	bool isGood;
};

}
}

#endif
