Move RelativeTime to chrono, add ostream support

This commit is contained in:
Vinnie Falco committed 2013-09-28 15:09:12 -07:00
1 parent 3f6e7aa05a
commit d37dd46f65
10 files changed
+283 -51

No files matched your search

+16 -1
View File
@@ -221,7 +221,6 @@ namespace beast
#include "time/PerformanceCounter.cpp"
#include "time/AtExitHook.cpp"
#include "time/RelativeTime.cpp"
#include "time/Time.cpp"
#include "xml/XmlDocument.cpp"
@@ -321,5 +320,21 @@ void beast_reportFatalError (char const* message, char const* fileName, int line
//------------------------------------------------------------------------------
// When we compile the static library, which is really just for browsing the
// sources in the Visual Studio IDE, we want to compile each individual unity
// .cpp in order to make sure that it builds by itself. An application that
// uses beast will typically include all of these .cpp files in one of its
// own unity .cpp
#if ! BEAST_COMPILING_STATIC_LIBARARY
/*
#include "../../beast/chrono/Chrono.cpp"
#include "../../beast/crypto/Crypto.cpp"
#include "../../beast/http/HTTP.cpp"
#include "../../beast/net/Net.cpp"
#include "../../beast/strings/Strings.cpp"
#include "../../beast/utility/Utility.cpp"
*/
#endif
// Must be outside the namespace
#include "system/BoostPlaceholdersFix.cpp"
+1 -1
View File
@@ -58,6 +58,7 @@
#include "../../beast/TypeTraits.h"
#include "../../beast/Thread.h"
#include "../../beast/Utility.h"
#include "../../beast/Chrono.h"
#include "system/StandardIncludes.h"
@@ -82,7 +83,6 @@ class FileOutputStream;
#include "time/AtExitHook.h"
#include "diagnostic/LeakChecked.h"
#include "time/RelativeTime.h"
#include "time/Time.h"
#include "threads/ScopedLock.h"
#include "threads/CriticalSection.h"
-134
View File
@@ -1,134 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Portions of this file are from JUCE.
Copyright (c) 2013 - Raw Material Software Ltd.
Please visit http://www.juce.com
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
RelativeTime::RelativeTime (const double secs) noexcept : numSeconds (secs) {}
RelativeTime::RelativeTime (const RelativeTime& other) noexcept : numSeconds (other.numSeconds) {}
RelativeTime::~RelativeTime() noexcept {}
//==============================================================================
RelativeTime RelativeTime::milliseconds (const int milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
RelativeTime RelativeTime::milliseconds (const int64 milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
RelativeTime RelativeTime::seconds (double s) noexcept { return RelativeTime (s); }
RelativeTime RelativeTime::minutes (const double numberOfMinutes) noexcept { return RelativeTime (numberOfMinutes * 60.0); }
RelativeTime RelativeTime::hours (const double numberOfHours) noexcept { return RelativeTime (numberOfHours * (60.0 * 60.0)); }
RelativeTime RelativeTime::days (const double numberOfDays) noexcept { return RelativeTime (numberOfDays * (60.0 * 60.0 * 24.0)); }
RelativeTime RelativeTime::weeks (const double numberOfWeeks) noexcept { return RelativeTime (numberOfWeeks * (60.0 * 60.0 * 24.0 * 7.0)); }
//==============================================================================
int64 RelativeTime::inMilliseconds() const noexcept { return (int64) (numSeconds * 1000.0); }
double RelativeTime::inMinutes() const noexcept { return numSeconds / 60.0; }
double RelativeTime::inHours() const noexcept { return numSeconds / (60.0 * 60.0); }
double RelativeTime::inDays() const noexcept { return numSeconds / (60.0 * 60.0 * 24.0); }
double RelativeTime::inWeeks() const noexcept { return numSeconds / (60.0 * 60.0 * 24.0 * 7.0); }
//==============================================================================
RelativeTime& RelativeTime::operator= (const RelativeTime& other) noexcept { numSeconds = other.numSeconds; return *this; }
RelativeTime RelativeTime::operator+= (RelativeTime t) noexcept { numSeconds += t.numSeconds; return *this; }
RelativeTime RelativeTime::operator-= (RelativeTime t) noexcept { numSeconds -= t.numSeconds; return *this; }
RelativeTime RelativeTime::operator+= (const double secs) noexcept { numSeconds += secs; return *this; }
RelativeTime RelativeTime::operator-= (const double secs) noexcept { numSeconds -= secs; return *this; }
RelativeTime operator+ (RelativeTime t1, RelativeTime t2) noexcept { return t1 += t2; }
RelativeTime operator- (RelativeTime t1, RelativeTime t2) noexcept { return t1 -= t2; }
bool operator== (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() == t2.inSeconds(); }
bool operator!= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() != t2.inSeconds(); }
bool operator> (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() > t2.inSeconds(); }
bool operator< (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() < t2.inSeconds(); }
bool operator>= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() >= t2.inSeconds(); }
bool operator<= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() <= t2.inSeconds(); }
//==============================================================================
static void translateTimeField (String& result, int n, const char* singular, const char* plural)
{
result << TRANS (n == 1 ? singular : plural)
.replace (n == 1 ? "1" : "2", String (n))
<< ' ';
}
String RelativeTime::getDescription (const String& returnValueForZeroTime) const
{
if (numSeconds < 0.001 && numSeconds > -0.001)
return returnValueForZeroTime;
String result;
result.preallocateBytes (32);
if (numSeconds < 0)
result << '-';
int fieldsShown = 0;
int n = std::abs ((int) inWeeks());
if (n > 0)
{
translateTimeField (result, n, NEEDS_TRANS("1 week"), NEEDS_TRANS("2 weeks"));
++fieldsShown;
}
n = std::abs ((int) inDays()) % 7;
if (n > 0)
{
translateTimeField (result, n, NEEDS_TRANS("1 day"), NEEDS_TRANS("2 days"));
++fieldsShown;
}
if (fieldsShown < 2)
{
n = std::abs ((int) inHours()) % 24;
if (n > 0)
{
translateTimeField (result, n, NEEDS_TRANS("1 hr"), NEEDS_TRANS("2 hrs"));
++fieldsShown;
}
if (fieldsShown < 2)
{
n = std::abs ((int) inMinutes()) % 60;
if (n > 0)
{
translateTimeField (result, n, NEEDS_TRANS("1 min"), NEEDS_TRANS("2 mins"));
++fieldsShown;
}
if (fieldsShown < 2)
{
n = std::abs ((int) inSeconds()) % 60;
if (n > 0)
{
translateTimeField (result, n, NEEDS_TRANS("1 sec"), NEEDS_TRANS("2 secs"));
++fieldsShown;
}
if (fieldsShown == 0)
{
n = std::abs ((int) inMilliseconds()) % 1000;
if (n > 0)
result << n << ' ' << TRANS ("ms");
}
}
}
}
return result.trimEnd();
}
-176
View File
@@ -1,176 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Portions of this file are from JUCE.
Copyright (c) 2013 - Raw Material Software Ltd.
Please visit http://www.juce.com
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_RELATIVETIME_H_INCLUDED
#define BEAST_RELATIVETIME_H_INCLUDED
//==============================================================================
/** A relative measure of time.
The time is stored as a number of seconds, at double-precision floating
point accuracy, and may be positive or negative.
If you need an absolute time, (i.e. a date + time), see the Time class.
*/
class BEAST_API RelativeTime
{
public:
//==============================================================================
/** Creates a RelativeTime.
@param seconds the number of seconds, which may be +ve or -ve.
@see milliseconds, minutes, hours, days, weeks
*/
explicit RelativeTime (double seconds = 0.0) noexcept;
/** Copies another relative time. */
RelativeTime (const RelativeTime& other) noexcept;
/** Copies another relative time. */
RelativeTime& operator= (const RelativeTime& other) noexcept;
/** Destructor. */
~RelativeTime() noexcept;
//==============================================================================
/** Creates a new RelativeTime object representing a number of milliseconds.
@see seconds, minutes, hours, days, weeks
*/
static RelativeTime milliseconds (int milliseconds) noexcept;
/** Creates a new RelativeTime object representing a number of milliseconds.
@see seconds, minutes, hours, days, weeks
*/
static RelativeTime milliseconds (int64 milliseconds) noexcept;
/** Creates a new RelativeTime object representing a number of seconds.
@see milliseconds, minutes, hours, days, weeks
*/
static RelativeTime seconds (double seconds) noexcept;
/** Creates a new RelativeTime object representing a number of minutes.
@see milliseconds, hours, days, weeks
*/
static RelativeTime minutes (double numberOfMinutes) noexcept;
/** Creates a new RelativeTime object representing a number of hours.
@see milliseconds, minutes, days, weeks
*/
static RelativeTime hours (double numberOfHours) noexcept;
/** Creates a new RelativeTime object representing a number of days.
@see milliseconds, minutes, hours, weeks
*/
static RelativeTime days (double numberOfDays) noexcept;
/** Creates a new RelativeTime object representing a number of weeks.
@see milliseconds, minutes, hours, days
*/
static RelativeTime weeks (double numberOfWeeks) noexcept;
//==============================================================================
/** Returns the number of milliseconds this time represents.
@see milliseconds, inSeconds, inMinutes, inHours, inDays, inWeeks
*/
int64 inMilliseconds() const noexcept;
/** Returns the number of seconds this time represents.
@see inMilliseconds, inMinutes, inHours, inDays, inWeeks
*/
double inSeconds() const noexcept { return numSeconds; }
/** Returns the number of minutes this time represents.
@see inMilliseconds, inSeconds, inHours, inDays, inWeeks
*/
double inMinutes() const noexcept;
/** Returns the number of hours this time represents.
@see inMilliseconds, inSeconds, inMinutes, inDays, inWeeks
*/
double inHours() const noexcept;
/** Returns the number of days this time represents.
@see inMilliseconds, inSeconds, inMinutes, inHours, inWeeks
*/
double inDays() const noexcept;
/** Returns the number of weeks this time represents.
@see inMilliseconds, inSeconds, inMinutes, inHours, inDays
*/
double inWeeks() const noexcept;
/** Returns a readable textual description of the time.
The exact format of the string returned will depend on
the magnitude of the time - e.g.
"1 min 4 secs", "1 hr 45 mins", "2 weeks 5 days", "140 ms"
so that only the two most significant units are printed.
The returnValueForZeroTime value is the result that is returned if the
length is zero. Depending on your application you might want to use this
to return something more relevant like "empty" or "0 secs", etc.
@see inMilliseconds, inSeconds, inMinutes, inHours, inDays, inWeeks
*/
String getDescription (const String& returnValueForZeroTime = "0") const;
//==============================================================================
/** Adds another RelativeTime to this one. */
RelativeTime operator+= (RelativeTime timeToAdd) noexcept;
/** Subtracts another RelativeTime from this one. */
RelativeTime operator-= (RelativeTime timeToSubtract) noexcept;
/** Adds a number of seconds to this time. */
RelativeTime operator+= (double secondsToAdd) noexcept;
/** Subtracts a number of seconds from this time. */
RelativeTime operator-= (double secondsToSubtract) noexcept;
private:
//==============================================================================
double numSeconds;
};
//==============================================================================
/** Compares two RelativeTimes. */
bool operator== (RelativeTime t1, RelativeTime t2) noexcept;
/** Compares two RelativeTimes. */
bool operator!= (RelativeTime t1, RelativeTime t2) noexcept;
/** Compares two RelativeTimes. */
bool operator> (RelativeTime t1, RelativeTime t2) noexcept;
/** Compares two RelativeTimes. */
bool operator< (RelativeTime t1, RelativeTime t2) noexcept;
/** Compares two RelativeTimes. */
bool operator>= (RelativeTime t1, RelativeTime t2) noexcept;
/** Compares two RelativeTimes. */
bool operator<= (RelativeTime t1, RelativeTime t2) noexcept;
//==============================================================================
/** Adds two RelativeTimes together. */
RelativeTime operator+ (RelativeTime t1, RelativeTime t2) noexcept;
/** Subtracts two RelativeTimes. */
RelativeTime operator- (RelativeTime t1, RelativeTime t2) noexcept;
#endif // BEAST_RELATIVETIME_H_INCLUDED