Back to index

nordugrid-arc-nox  1.1.0~rc6
Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Attributes | Static Private Attributes
Arc::Time Class Reference

A class for storing and manipulating times. More...

#include <DateTime.h>

List of all members.

Public Member Functions

 Time ()
 Default constructor.
 Time (const time_t &)
 Constructor that takes a time_t variable and stores it.
 Time (const std::string &)
 Constructor that tries to convert a string into a time_t.
Timeoperator= (const time_t &)
 Assignment operator from a time_t.
Timeoperator= (const Time &)
 Assignment operator from a Time.
Timeoperator= (const char *)
 Assignment operator from a char pointer.
Timeoperator= (const std::string &)
 Assignment operator from a string.
void SetTime (const time_t &)
 sets the time
time_t GetTime () const
 gets the time
 operator std::string () const
 Returns a string representation of the time, using the default format.
std::string str (const TimeFormat &=time_format) const
 Returns a string representation of the time, using the specified format.
bool operator< (const Time &) const
 Comparing two Time objects.
bool operator> (const Time &) const
 Comparing two Time objects.
bool operator<= (const Time &) const
 Comparing two Time objects.
bool operator>= (const Time &) const
 Comparing two Time objects.
bool operator== (const Time &) const
 Comparing two Time objects.
bool operator!= (const Time &) const
 Comparing two Time objects.
Time operator+ (const Period &) const
 Adding Time object with Period object.
Time operator- (const Period &) const
 Subtracting Period object from Time object.
Period operator- (const Time &) const
 Subtracting Time object from the other Time object.

Static Public Member Functions

static void SetFormat (const TimeFormat &)
 Sets the default format for time strings.
static TimeFormat GetFormat ()
 Gets the default format for time strings.

Static Public Attributes

static const int YEAR = 31536000
static const int MONTH = 2592000
static const int WEEK = 604800
static const int DAY = 86400
static const int HOUR = 3600

Private Attributes

time_t gtime
 The time stored -- by default it is equal to the current time.

Static Private Attributes

static TimeFormat time_format = UserTime
 The time-format stored.

Detailed Description

A class for storing and manipulating times.

Definition at line 96 of file DateTime.h.


Constructor & Destructor Documentation

Default constructor.

The time is put equal the current time.

Definition at line 61 of file DateTime.cpp.

    : gtime(time(NULL)) {}

Here is the caller graph for this function:

Arc::Time::Time ( const time_t &  time)

Constructor that takes a time_t variable and stores it.

Definition at line 65 of file DateTime.cpp.

    : gtime(time) {}
Arc::Time::Time ( const std::string &  timestring)

Constructor that tries to convert a string into a time_t.

Definition at line 69 of file DateTime.cpp.

    : gtime(-1) {

    if (timestring.empty()) {
      dateTimeLogger.msg(ERROR, "Empty string");
      return;
    }

    if (isdigit(timestring[0])) {
      tm timestr;
      std::string::size_type pos = 0;

      if (sscanf(timestring.substr(pos, 10).c_str(),
                 "%4d-%2d-%2d",
                 &timestr.tm_year,
                 &timestr.tm_mon,
                 &timestr.tm_mday) == 3)
        pos += 10;
      else if (sscanf(timestring.substr(pos, 8).c_str(),
                      "%4d%2d%2d",
                      &timestr.tm_year,
                      &timestr.tm_mon,
                      &timestr.tm_mday) == 3)
        pos += 8;
      else {
        dateTimeLogger.msg(ERROR, "Can not parse date: %s", timestring);
        return;
      }

      timestr.tm_year -= 1900;
      timestr.tm_mon--;

      if (timestring[pos] == 'T' || timestring[pos] == ' ')
        pos++;

      if (sscanf(timestring.substr(pos, 8).c_str(),
                 "%2d:%2d:%2d",
                 &timestr.tm_hour,
                 &timestr.tm_min,
                 &timestr.tm_sec) == 3)
        pos += 8;
      else if (sscanf(timestring.substr(pos, 6).c_str(),
                      "%2d%2d%2d",
                      &timestr.tm_hour,
                      &timestr.tm_min,
                      &timestr.tm_sec) == 3)
        pos += 6;
      else {
        dateTimeLogger.msg(ERROR, "Can not parse time: %s", timestring);
        return;
      }

      // skip fraction of second
      if (timestring[pos] == '.') {
        pos++;
        while (isdigit(timestring[pos]))
          pos++;
      }

      if (timestring[pos] == 'Z') {
        pos++;
        gtime = timegm(&timestr);
      }
      else if (timestring[pos] == '+' || timestring[pos] == '-') {
        bool tzplus = (timestring[pos] == '+');
        pos++;
        int tzh, tzm;
        if (sscanf(timestring.substr(pos, 5).c_str(),
                   "%2d:%2d",
                   &tzh,
                   &tzm) == 2)
          pos += 5;
        else if (sscanf(timestring.substr(pos, 4).c_str(),
                        "%2d%2d",
                        &tzh,
                        &tzm) == 2)
          pos += 4;
        else {
          dateTimeLogger.msg(ERROR, "Can not parse time zone offset: %s",
                             timestring);
          return;
        }

        gtime = timegm(&timestr);

        if (gtime != -1) {
          if (tzplus)
            gtime -= tzh * 3600 + tzm * 60;
          else
            gtime += tzh * 3600 + tzm * 60;
        }
      }
      else {
        timestr.tm_isdst = -1;
        gtime = mktime(&timestr);
      }

      if (timestring.size() != pos) {
        dateTimeLogger.msg(ERROR, "Illegal time format: %s", timestring);
        return;
      }
    }
    else if (timestring.length() == 24) {
      // C time
      tm timestr;
      char day[4];
      char month[4];

      if (sscanf(timestring.c_str(),
                 "%3s %3s %2d %2d:%2d:%2d %4d",
                 day,
                 month,
                 &timestr.tm_mday,
                 &timestr.tm_hour,
                 &timestr.tm_min,
                 &timestr.tm_sec,
                 &timestr.tm_year) != 7) {
        dateTimeLogger.msg(ERROR, "Illegal time format: %s", timestring);
        return;
      }

      timestr.tm_year -= 1900;

      if (strncmp(month, "Jan", 3) == 0)
        timestr.tm_mon = 0;
      else if (strncmp(month, "Feb", 3) == 0)
        timestr.tm_mon = 1;
      else if (strncmp(month, "Mar", 3) == 0)
        timestr.tm_mon = 2;
      else if (strncmp(month, "Apr", 3) == 0)
        timestr.tm_mon = 3;
      else if (strncmp(month, "May", 3) == 0)
        timestr.tm_mon = 4;
      else if (strncmp(month, "Jun", 3) == 0)
        timestr.tm_mon = 5;
      else if (strncmp(month, "Jul", 3) == 0)
        timestr.tm_mon = 6;
      else if (strncmp(month, "Aug", 3) == 0)
        timestr.tm_mon = 7;
      else if (strncmp(month, "Sep", 3) == 0)
        timestr.tm_mon = 8;
      else if (strncmp(month, "Oct", 3) == 0)
        timestr.tm_mon = 9;
      else if (strncmp(month, "Nov", 3) == 0)
        timestr.tm_mon = 10;
      else if (strncmp(month, "Dec", 3) == 0)
        timestr.tm_mon = 11;
      else {
        dateTimeLogger.msg(ERROR, "Can not parse month: %s", month);
        return;
      }

      timestr.tm_isdst = -1;
      gtime = mktime(&timestr);
    }
    else if (timestring.length() == 29) {
      // RFC 1123 time (used by HTTP protoccol)
      tm timestr;
      char day[4];
      char month[4];

      if (sscanf(timestring.c_str(),
                 "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
                 day,
                 &timestr.tm_mday,
                 month,
                 &timestr.tm_year,
                 &timestr.tm_hour,
                 &timestr.tm_min,
                 &timestr.tm_sec) != 7) {
        dateTimeLogger.msg(ERROR, "Illegal time format: %s", timestring);
        return;
      }

      timestr.tm_year -= 1900;

      if (strncmp(month, "Jan", 3) == 0)
        timestr.tm_mon = 0;
      else if (strncmp(month, "Feb", 3) == 0)
        timestr.tm_mon = 1;
      else if (strncmp(month, "Mar", 3) == 0)
        timestr.tm_mon = 2;
      else if (strncmp(month, "Apr", 3) == 0)
        timestr.tm_mon = 3;
      else if (strncmp(month, "May", 3) == 0)
        timestr.tm_mon = 4;
      else if (strncmp(month, "Jun", 3) == 0)
        timestr.tm_mon = 5;
      else if (strncmp(month, "Jul", 3) == 0)
        timestr.tm_mon = 6;
      else if (strncmp(month, "Aug", 3) == 0)
        timestr.tm_mon = 7;
      else if (strncmp(month, "Sep", 3) == 0)
        timestr.tm_mon = 8;
      else if (strncmp(month, "Oct", 3) == 0)
        timestr.tm_mon = 9;
      else if (strncmp(month, "Nov", 3) == 0)
        timestr.tm_mon = 10;
      else if (strncmp(month, "Dec", 3) == 0)
        timestr.tm_mon = 11;
      else {
        dateTimeLogger.msg(ERROR, "Can not parse month: %s", month);
        return;
      }

      gtime = timegm(&timestr);
    }

    if (gtime == -1)
      dateTimeLogger.msg(ERROR, "Illegal time format: %s", timestring);
  }

Here is the call graph for this function:


Member Function Documentation

Gets the default format for time strings.

Definition at line 297 of file DateTime.cpp.

                             {
    return time_format;
  }

Here is the caller graph for this function:

time_t Arc::Time::GetTime ( ) const

gets the time

Definition at line 287 of file DateTime.cpp.

                             {
    return gtime;
  }

Here is the caller graph for this function:

Arc::Time::operator std::string ( ) const

Returns a string representation of the time, using the default format.

Definition at line 302 of file DateTime.cpp.

                                 {
    return str();
  }
bool Arc::Time::operator!= ( const Time othertime) const

Comparing two Time objects.

Definition at line 469 of file DateTime.cpp.

                                                   {
    return gtime != othertime.GetTime();
  }

Here is the call graph for this function:

Time Arc::Time::operator+ ( const Period duration) const

Adding Time object with Period object.

Definition at line 473 of file DateTime.cpp.

                                                   {
    time_t t;
    t = gtime + duration.GetPeriod();
    return (Time(t));
  }

Here is the call graph for this function:

Time Arc::Time::operator- ( const Period duration) const

Subtracting Period object from Time object.

Definition at line 479 of file DateTime.cpp.

                                                   {
    time_t t;
    t = gtime - duration.GetPeriod();
    return (Time(t));
  }

Here is the call graph for this function:

Period Arc::Time::operator- ( const Time other) const

Subtracting Time object from the other Time object.

Definition at line 485 of file DateTime.cpp.

                                                {
    time_t t;
    t = gtime - other.GetTime();
    return (Period(t));
  }

Here is the call graph for this function:

bool Arc::Time::operator< ( const Time othertime) const

Comparing two Time objects.

Definition at line 444 of file DateTime.cpp.

                                                  {
    return gtime < othertime.GetTime();
  }

Here is the call graph for this function:

bool Arc::Time::operator<= ( const Time othertime) const

Comparing two Time objects.

Definition at line 454 of file DateTime.cpp.

                                                   {
    return gtime <= othertime.GetTime();
  }

Here is the call graph for this function:

Time & Arc::Time::operator= ( const time_t &  newtime)

Assignment operator from a time_t.

Definition at line 491 of file DateTime.cpp.

                                             {
    gtime = newtime;
    return *this;
  }

Here is the caller graph for this function:

Time & Arc::Time::operator= ( const Time newtime)

Assignment operator from a Time.

Definition at line 496 of file DateTime.cpp.

                                           {
    gtime = newtime.GetTime();
    return *this;
  }

Here is the call graph for this function:

Time & Arc::Time::operator= ( const char *  newtime)

Assignment operator from a char pointer.

Definition at line 501 of file DateTime.cpp.

                                           {
    return operator=(std::string(newtime));
  }

Here is the call graph for this function:

Time & Arc::Time::operator= ( const std::string &  newtime)

Assignment operator from a string.

Definition at line 505 of file DateTime.cpp.

                                                {
    return *this = Arc::Time(newtime);
  }

Here is the call graph for this function:

bool Arc::Time::operator== ( const Time othertime) const

Comparing two Time objects.

Definition at line 464 of file DateTime.cpp.

                                                   {
    return gtime == othertime.GetTime();
  }

Here is the call graph for this function:

bool Arc::Time::operator> ( const Time othertime) const

Comparing two Time objects.

Definition at line 449 of file DateTime.cpp.

                                                  {
    return gtime > othertime.GetTime();
  }

Here is the call graph for this function:

bool Arc::Time::operator>= ( const Time othertime) const

Comparing two Time objects.

Definition at line 459 of file DateTime.cpp.

                                                   {
    return gtime >= othertime.GetTime();
  }

Here is the call graph for this function:

void Arc::Time::SetFormat ( const TimeFormat format) [static]

Sets the default format for time strings.

Definition at line 292 of file DateTime.cpp.

                                               {
    time_format = format;
  }

Here is the caller graph for this function:

void Arc::Time::SetTime ( const time_t &  time)

sets the time

Definition at line 282 of file DateTime.cpp.

                                       {
    gtime = time;
  }

Here is the caller graph for this function:

std::string Arc::Time::str ( const TimeFormat format = time_format) const

Returns a string representation of the time, using the specified format.

Definition at line 307 of file DateTime.cpp.

                                                    {

    const char *day[] = {
      "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    };
    // C week starts on Sunday - just live with it...
    const char *month[] = {
      "Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    };

    switch (format) {

    case ASCTime:  // Day Mon DD HH:MM:SS YYYY
      {
        tm tmtime;
        localtime_r(&gtime, &tmtime);

        std::stringstream ss;

        ss << std::setfill('0');

        ss << day[tmtime.tm_wday] << ' '
           << month[tmtime.tm_mon] << ' '
           << std::setw(2) << tmtime.tm_mday << ' '
           << std::setw(2) << tmtime.tm_hour << ':'
           << std::setw(2) << tmtime.tm_min << ':'
           << std::setw(2) << tmtime.tm_sec << ' '
           << std::setw(4) << tmtime.tm_year + 1900;

        return ss.str();
      }

    case UserTime:
      {
        tm tmtime;
        localtime_r(&gtime, &tmtime);

        std::stringstream ss;

        ss << std::setfill('0');

        ss << std::setw(4) << tmtime.tm_year + 1900 << '-'
           << std::setw(2) << tmtime.tm_mon + 1 << '-'
           << std::setw(2) << tmtime.tm_mday << ' '
           << std::setw(2) << tmtime.tm_hour << ':'
           << std::setw(2) << tmtime.tm_min << ':'
           << std::setw(2) << tmtime.tm_sec;

        return ss.str();
      }

    case MDSTime:
      {
        tm tmtime;
        gmtime_r(&gtime, &tmtime);

        std::stringstream ss;

        ss << std::setfill('0');

        ss << std::setw(4) << tmtime.tm_year + 1900
           << std::setw(2) << tmtime.tm_mon + 1
           << std::setw(2) << tmtime.tm_mday
           << std::setw(2) << tmtime.tm_hour
           << std::setw(2) << tmtime.tm_min
           << std::setw(2) << tmtime.tm_sec << 'Z';

        return ss.str();
      }

    case ISOTime:
      {
        tm tmtime;
        localtime_r(&gtime, &tmtime);
        time_t tzoffset = timegm(&tmtime) - gtime;

        std::stringstream ss;

        ss << std::setfill('0');

        ss << std::setw(4) << tmtime.tm_year + 1900 << '-'
           << std::setw(2) << tmtime.tm_mon + 1 << '-'
           << std::setw(2) << tmtime.tm_mday << 'T'
           << std::setw(2) << tmtime.tm_hour << ':'
           << std::setw(2) << tmtime.tm_min << ':'
           << std::setw(2) << tmtime.tm_sec << (tzoffset < 0 ? '-' : '+')
           << std::setw(2) << abs(tzoffset) / Time::HOUR << ':'
           << std::setw(2) << (abs(tzoffset) % Time::HOUR) / 60;

        return ss.str();
      }

    case UTCTime:
      {
        tm tmtime;
        gmtime_r(&gtime, &tmtime);

        std::stringstream ss;

        ss << std::setfill('0');

        ss << std::setw(4) << tmtime.tm_year + 1900 << '-'
           << std::setw(2) << tmtime.tm_mon + 1 << '-'
           << std::setw(2) << tmtime.tm_mday << 'T'
           << std::setw(2) << tmtime.tm_hour << ':'
           << std::setw(2) << tmtime.tm_min << ':'
           << std::setw(2) << tmtime.tm_sec << 'Z';

        return ss.str();
      }

    case RFC1123Time:
      {
        tm tmtime;
        gmtime_r(&gtime, &tmtime);

        std::stringstream ss;

        ss << std::setfill('0');

        ss << day[tmtime.tm_wday] << ", "
           << std::setw(2) << tmtime.tm_mday << ' '
           << month[tmtime.tm_mon] << ' '
           << std::setw(4) << tmtime.tm_year + 1900 << ' '
           << std::setw(2) << tmtime.tm_hour << ':'
           << std::setw(2) << tmtime.tm_min << ':'
           << std::setw(2) << tmtime.tm_sec << " GMT";

        return ss.str();
      }

    }
    return "";
  }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

const int Arc::Time::DAY = 86400 [static]

Definition at line 169 of file DateTime.h.

time_t Arc::Time::gtime [private]

The time stored -- by default it is equal to the current time.

Definition at line 174 of file DateTime.h.

const int Arc::Time::HOUR = 3600 [static]

Definition at line 170 of file DateTime.h.

const int Arc::Time::MONTH = 2592000 [static]

Definition at line 167 of file DateTime.h.

The time-format stored.

By default it is equal to UserTime

Definition at line 177 of file DateTime.h.

const int Arc::Time::WEEK = 604800 [static]

Definition at line 168 of file DateTime.h.

const int Arc::Time::YEAR = 31536000 [static]

Definition at line 166 of file DateTime.h.


The documentation for this class was generated from the following files: