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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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
|
||||
#define LIBSOCKTHREAD_PLATFORM_HPP
|
||||
|
||||
/*
|
||||
* Operating system
|
||||
*/
|
||||
#define FREEBSD 0 // FreeBSD (untested)
|
||||
#define FREEBSD 0 // FreeBSD
|
||||
#define MINGW 1 // Windows native (Mingw)
|
||||
#define LINUX 2 // Linux
|
||||
#define CYGWIN 3 // Cygwin
|
||||
@@ -60,4 +66,9 @@
|
||||
#define NO_INET_PTON
|
||||
#endif
|
||||
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
#endif // LIBSOCKTHREAD_PLATFORM_HPP
|
||||
|
@@ -28,66 +28,55 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
#include "platform.hpp"
|
||||
#include "time.hpp"
|
||||
using namespace Libsockthread;
|
||||
|
||||
/*
|
||||
* Converts the time to an ISO 8601 standard time and date and puts it in a
|
||||
* string
|
||||
* Converts the time to an ISO 8601 standard date and time
|
||||
* Example: 2004-07-01T19:03:47Z
|
||||
*/
|
||||
string& Time::utc(string &s) const
|
||||
string& Time::utc()
|
||||
{
|
||||
struct tm* tm;
|
||||
|
||||
tm = gmtime(&unixtime);
|
||||
struct tm* tm = gmtime(&unixtime);
|
||||
char t[21];
|
||||
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
|
||||
*/
|
||||
string& Time::utc_date(string &s) const
|
||||
string& Time::utc_date()
|
||||
{
|
||||
struct tm* tm;
|
||||
|
||||
tm = gmtime(&unixtime);
|
||||
struct tm* tm = gmtime(&unixtime);
|
||||
char t[12];
|
||||
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
|
||||
*/
|
||||
string& Time::utc_time(string &s) const
|
||||
string& Time::utc_time()
|
||||
{
|
||||
struct tm* tm;
|
||||
|
||||
tm = gmtime(&unixtime);
|
||||
struct tm* tm = gmtime(&unixtime);
|
||||
char t[10];
|
||||
strftime(t, sizeof t, "%H:%M:%SZ", tm);
|
||||
return s = t;
|
||||
return formatted = t;
|
||||
}
|
||||
|
||||
#ifdef UNIT_TEST
|
||||
// g++ -Wall -DUNIT_TEST time.cpp -o time
|
||||
#include <iostream>
|
||||
|
||||
int main(void)
|
||||
int main()
|
||||
{
|
||||
Time t;
|
||||
string s;
|
||||
cout << "Current date and time is " << t.utc(s) << '\n';
|
||||
cout << "Current date is " << t.utc_date(s) << '\n';
|
||||
cout << "Current time is " << t.utc_time(s) << '\n';
|
||||
cout << "Current date and time is " << t.utc() << '\n';
|
||||
cout << "Current date is " << t.utc_date() << '\n';
|
||||
cout << "Current time is " << t.utc_time() << '\n';
|
||||
cout << "Formatted time is " << t.get_formatted() << " (should be the same)\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -34,13 +34,19 @@
|
||||
namespace Libsockthread {
|
||||
class Time {
|
||||
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:
|
||||
string formatted;
|
||||
time_t unixtime;
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user