forked from I2P_Developers/i2p.i2p
cleaned up time class
This commit is contained in:
@@ -28,13 +28,19 @@
|
|||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Global includes and platform configuration. This is used to compile the
|
||||||
|
* library, but is not intended for use by users of the library in their
|
||||||
|
* programs.
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef LIBSOCKTHREAD_PLATFORM_HPP
|
#ifndef LIBSOCKTHREAD_PLATFORM_HPP
|
||||||
#define LIBSOCKTHREAD_PLATFORM_HPP
|
#define LIBSOCKTHREAD_PLATFORM_HPP
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Operating system
|
* Operating system
|
||||||
*/
|
*/
|
||||||
#define FREEBSD 0 // FreeBSD (untested)
|
#define FREEBSD 0 // FreeBSD
|
||||||
#define MINGW 1 // Windows native (Mingw)
|
#define MINGW 1 // Windows native (Mingw)
|
||||||
#define LINUX 2 // Linux
|
#define LINUX 2 // Linux
|
||||||
#define CYGWIN 3 // Cygwin
|
#define CYGWIN 3 // Cygwin
|
||||||
@@ -60,4 +66,9 @@
|
|||||||
#define NO_INET_PTON
|
#define NO_INET_PTON
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <ctime>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
#endif // LIBSOCKTHREAD_PLATFORM_HPP
|
#endif // LIBSOCKTHREAD_PLATFORM_HPP
|
||||||
|
@@ -28,66 +28,55 @@
|
|||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <ctime>
|
#include "platform.hpp"
|
||||||
#include <string>
|
|
||||||
using namespace std;
|
|
||||||
#include "time.hpp"
|
#include "time.hpp"
|
||||||
using namespace Libsockthread;
|
using namespace Libsockthread;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Converts the time to an ISO 8601 standard time and date and puts it in a
|
* Converts the time to an ISO 8601 standard date and time
|
||||||
* string
|
|
||||||
* Example: 2004-07-01T19:03:47Z
|
* Example: 2004-07-01T19:03:47Z
|
||||||
*/
|
*/
|
||||||
string& Time::utc(string &s) const
|
string& Time::utc()
|
||||||
{
|
{
|
||||||
struct tm* tm;
|
struct tm* tm = gmtime(&unixtime);
|
||||||
|
|
||||||
tm = gmtime(&unixtime);
|
|
||||||
char t[21];
|
char t[21];
|
||||||
strftime(t, sizeof t, "%Y-%m-%dT%H:%M:%SZ", tm);
|
strftime(t, sizeof t, "%Y-%m-%dT%H:%M:%SZ", tm);
|
||||||
return s = t;
|
return formatted = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Converts the time to an ISO 8601 standard date and puts it in a string
|
* Converts the time to an ISO 8601 standard date
|
||||||
* Example: 2004-07-01Z
|
* Example: 2004-07-01Z
|
||||||
*/
|
*/
|
||||||
string& Time::utc_date(string &s) const
|
string& Time::utc_date()
|
||||||
{
|
{
|
||||||
struct tm* tm;
|
struct tm* tm = gmtime(&unixtime);
|
||||||
|
|
||||||
tm = gmtime(&unixtime);
|
|
||||||
char t[12];
|
char t[12];
|
||||||
strftime(t, sizeof t, "%Y-%m-%dZ", tm);
|
strftime(t, sizeof t, "%Y-%m-%dZ", tm);
|
||||||
return s = t;
|
return formatted = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Converts the time to an ISO 8601 standard time and puts it in a string
|
* Converts the time to an ISO 8601 standard time
|
||||||
* Example: 19:03:47Z
|
* Example: 19:03:47Z
|
||||||
*/
|
*/
|
||||||
string& Time::utc_time(string &s) const
|
string& Time::utc_time()
|
||||||
{
|
{
|
||||||
struct tm* tm;
|
struct tm* tm = gmtime(&unixtime);
|
||||||
|
|
||||||
tm = gmtime(&unixtime);
|
|
||||||
char t[10];
|
char t[10];
|
||||||
strftime(t, sizeof t, "%H:%M:%SZ", tm);
|
strftime(t, sizeof t, "%H:%M:%SZ", tm);
|
||||||
return s = t;
|
return formatted = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef UNIT_TEST
|
#ifdef UNIT_TEST
|
||||||
// g++ -Wall -DUNIT_TEST time.cpp -o time
|
// g++ -Wall -DUNIT_TEST time.cpp -o time
|
||||||
#include <iostream>
|
int main()
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
{
|
||||||
Time t;
|
Time t;
|
||||||
string s;
|
cout << "Current date and time is " << t.utc() << '\n';
|
||||||
cout << "Current date and time is " << t.utc(s) << '\n';
|
cout << "Current date is " << t.utc_date() << '\n';
|
||||||
cout << "Current date is " << t.utc_date(s) << '\n';
|
cout << "Current time is " << t.utc_time() << '\n';
|
||||||
cout << "Current time is " << t.utc_time(s) << '\n';
|
cout << "Formatted time is " << t.get_formatted() << " (should be the same)\n";
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@@ -34,13 +34,19 @@
|
|||||||
namespace Libsockthread {
|
namespace Libsockthread {
|
||||||
class Time {
|
class Time {
|
||||||
public:
|
public:
|
||||||
Time(void) { now(); }
|
Time()
|
||||||
|
{ now(); }
|
||||||
|
|
||||||
|
string& get_formatted()
|
||||||
|
{ return formatted; }
|
||||||
|
void now()
|
||||||
|
{ unixtime = time(0); }
|
||||||
|
string& utc();
|
||||||
|
string& utc_date();
|
||||||
|
string& utc_time();
|
||||||
|
|
||||||
void now(void) { unixtime = time(0); }
|
|
||||||
string& utc(string &s) const;
|
|
||||||
string& utc_date(string &s) const;
|
|
||||||
string& utc_time(string &s) const;
|
|
||||||
private:
|
private:
|
||||||
|
string formatted;
|
||||||
time_t unixtime;
|
time_t unixtime;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user