Back to index

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

#include <DateTime.h>

Collaboration diagram for Arc::Period:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Period ()
 Default constructor.
 Period (const time_t &)
 Constructor that takes a time_t variable and stores it.
 Period (const std::string &, PeriodBase base=PeriodSeconds)
 Constructor that tries to convert a string.
Periodoperator= (const time_t &)
 Assignment operator from a time_t.
Periodoperator= (const Period &)
 Assignment operator from a Period.
void SetPeriod (const time_t &)
 sets the period
time_t GetPeriod () const
 gets the period
const sigc::slot< const char * > * istr () const
 For use with IString.
 operator std::string () const
 Returns a string representation of the period.
bool operator< (const Period &) const
 Comparing two Period objects.
bool operator> (const Period &) const
 Comparing two Period objects.
bool operator<= (const Period &) const
 Comparing two Period objects.
bool operator>= (const Period &) const
 Comparing two Period objects.
bool operator== (const Period &) const
 Comparing two Period objects.
bool operator!= (const Period &) const
 Comparing two Period objects.

Private Member Functions

const char * IStr () const
 Internal IString implementation.

Private Attributes

time_t seconds
 The duration of the period.
sigc::slot< const char * > slot
std::string is

Detailed Description

Definition at line 33 of file DateTime.h.


Constructor & Destructor Documentation

Default constructor.

The period is set to 0 length.

Definition at line 525 of file DateTime.cpp.

    : seconds(0) {}
Arc::Period::Period ( const time_t &  length)

Constructor that takes a time_t variable and stores it.

Definition at line 529 of file DateTime.cpp.

    : seconds(length) {}
Arc::Period::Period ( const std::string &  period,
PeriodBase  base = PeriodSeconds 
)

Constructor that tries to convert a string.

Definition at line 533 of file DateTime.cpp.

    : seconds(0) {

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

    if (period[0] == 'P') {
      // ISO duration
      std::string::size_type pos = 1;
      bool min = false; // months or minutes?
      while (pos < period.size()) {
        if (period[pos] == 'T')
          min = true;
        else {
          std::string::size_type pos2 = pos;
          while (pos2 < period.size() && isdigit(period[pos2]))
            pos2++;
          if (pos2 == pos || pos2 == period.size()) {
            dateTimeLogger.msg(ERROR, "Invalid ISO duration format: %s",
                               period);
            seconds = 0;
            return;
          }
          int num = stringtoi(period.substr(pos, pos2 - pos));
          pos = pos2;
          switch (period[pos]) {
          case 'Y':
            seconds += num * Time::YEAR;
            break;

          case 'W':
            seconds += num * Time::WEEK;
            min = true;
            break;

          case 'D':
            seconds += num * Time::DAY;
            min = true;
            break;

          case 'H':
            seconds += num * Time::HOUR;
            min = true;
            break;

          case 'M':
            if (min)
              seconds += num * 60;
            else {
              seconds += num * Time::MONTH;
              min = true;
            }
            break;

          case 'S':
            seconds += num;
            break;

          default:
            dateTimeLogger.msg(ERROR, "Invalid ISO duration format: %s",
                               period);
            seconds = 0;
            return;
            break;
          }
        }
        pos++;
      }
    }
    else {
      // "free" format
      std::string::size_type pos = std::string::npos;
      int len = 0;

      for (std::string::size_type i = 0; i != period.length(); i++) {
        if (isdigit(period[i])) {
          if (pos == std::string::npos) {
            pos = i;
            len = 0;
          }
          len++;
        }
        else if (pos != std::string::npos) {
          switch (period[i]) {
          case 'w':
          case 'W':
            seconds += stringtoi(period.substr(pos, len)) * Time::WEEK;
            pos = std::string::npos;
            base = PeriodDays;
            break;

          case 'd':
          case 'D':
            seconds += stringtoi(period.substr(pos, len)) * Time::DAY;
            pos = std::string::npos;
            base = PeriodHours;
            break;

          case 'h':
          case 'H':
            seconds += stringtoi(period.substr(pos, len)) * Time::HOUR;
            pos = std::string::npos;
            base = PeriodMinutes;
            break;

          case 'm':
          case 'M':
            seconds += stringtoi(period.substr(pos, len)) * 60;
            pos = std::string::npos;
            base = PeriodSeconds;
            break;

          case 's':
          case 'S':
            seconds += stringtoi(period.substr(pos, len));
            pos = std::string::npos;
            base = PeriodMiliseconds;
            break;

          case ' ':
            break;

          default:
            dateTimeLogger.msg(ERROR, "Invalid period string: %s", period);
            seconds = 0;
            return;
            break;
          }
        }
      }

      if (pos != std::string::npos) {
        int n = stringtoi(period.substr(pos, len));
        switch (base) {
        case PeriodMiliseconds:
          n /= 1000;
          break;

        case PeriodSeconds:
          break;

        case PeriodMinutes:
          n *= 60;
          break;

        case PeriodHours:
          n *= Time::HOUR;
          break;

        case PeriodDays:
          n *= Time::DAY;
          break;

        case PeriodWeeks:
          n *= Time::WEEK;
          break;
        }
        seconds += n;
      }
    }
  }

Here is the call graph for this function:


Member Function Documentation

time_t Arc::Period::GetPeriod ( ) const

gets the period

Definition at line 713 of file DateTime.cpp.

                                 {
    return seconds;
  }

Here is the caller graph for this function:

const sigc::slot< const char * > * Arc::Period::istr ( ) const

For use with IString.

Definition at line 717 of file DateTime.cpp.

                                                  {
    const_cast<Period*>(this)->slot = sigc::mem_fun(this, &Arc::Period::IStr);
    return &slot;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

const char * Arc::Period::IStr ( ) const [private]

Internal IString implementation.

Definition at line 722 of file DateTime.cpp.

                                 {

    time_t remain = seconds;

    std::stringstream ss;

    if (remain >= Time::YEAR) {
      ss << remain / Time::YEAR << " "
         << FindNTrans("year", "years", remain / Time::YEAR);
      remain %= Time::YEAR;
    }
    if (remain >= Time::MONTH) {
      if (remain != seconds) ss << " ";
      ss << remain / Time::MONTH << " "
         << FindNTrans("month", "months", remain / Time::MONTH);
      remain %= Time::MONTH;
    }
    if (remain >= Time::DAY) {
      if (remain != seconds) ss << " ";
      ss << remain / Time::DAY << " "
         << FindNTrans("day", "days", remain / Time::DAY);
      remain %= Time::DAY;
    }
    if (remain >= Time::HOUR) {
      if (remain != seconds) ss << " ";
      ss << remain / Time::HOUR << " "
         << FindNTrans("hour", "hours", remain / Time::HOUR);
      remain %= Time::HOUR;
    }
    if (remain >= 60) {
      if (remain != seconds) ss << " ";
      ss << remain / 60 << " "
         << FindNTrans("minute", "minutes", remain / 60);
      remain %= 60;
    }
    if (remain >= 1) {
      if (remain != seconds) ss << " ";
      ss << remain << " "
         << FindNTrans("second", "seconds", remain);
    }

    const_cast<Period*>(this)->is = ss.str();
    return is.c_str();
  }

Here is the call graph for this function:

Here is the caller graph for this function:

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

Returns a string representation of the period.

Definition at line 767 of file DateTime.cpp.

                                   {
    time_t remain = seconds;

    std::stringstream ss;

    ss << 'P';
    if (remain >= Time::YEAR) {
      ss << remain / Time::YEAR << 'Y';
      remain %= Time::YEAR;
    }
    if (remain >= Time::MONTH) {
      ss << remain / Time::MONTH << 'M';
      remain %= Time::MONTH;
    }
    if (remain >= Time::DAY) {
      ss << remain / Time::DAY << 'D';
      remain %= Time::DAY;
    }
    if (remain)
      ss << 'T';
    if (remain >= Time::HOUR) {
      ss << remain / Time::HOUR << 'H';
      remain %= Time::HOUR;
    }
    if (remain >= 60) {
      ss << remain / 60 << 'M';
      remain %= 60;
    }
    if (remain >= 1)
      ss << remain << 'S';

    return ss.str();
  }
bool Arc::Period::operator!= ( const Period othertime) const

Comparing two Period objects.

Definition at line 827 of file DateTime.cpp.

                                                       {
    return seconds != othertime.GetPeriod();
  }

Here is the call graph for this function:

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

Comparing two Period objects.

Definition at line 802 of file DateTime.cpp.

                                                      {
    return seconds < othertime.GetPeriod();
  }

Here is the call graph for this function:

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

Comparing two Period objects.

Definition at line 812 of file DateTime.cpp.

                                                       {
    return seconds <= othertime.GetPeriod();
  }

Here is the call graph for this function:

Period & Arc::Period::operator= ( const time_t &  length)

Assignment operator from a time_t.

Definition at line 698 of file DateTime.cpp.

                                                {
    seconds = length;
    return *this;
  }
Period & Arc::Period::operator= ( const Period newperiod)

Assignment operator from a Period.

Definition at line 703 of file DateTime.cpp.

                                                   {
    seconds = newperiod.GetPeriod();
    return *this;
  }

Here is the call graph for this function:

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

Comparing two Period objects.

Definition at line 822 of file DateTime.cpp.

                                                       {
    return seconds == othertime.GetPeriod();
  }

Here is the call graph for this function:

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

Comparing two Period objects.

Definition at line 807 of file DateTime.cpp.

                                                      {
    return seconds > othertime.GetPeriod();
  }

Here is the call graph for this function:

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

Comparing two Period objects.

Definition at line 817 of file DateTime.cpp.

                                                       {
    return seconds >= othertime.GetPeriod();
  }

Here is the call graph for this function:

void Arc::Period::SetPeriod ( const time_t &  length)

sets the period

Definition at line 708 of file DateTime.cpp.

                                             {
    seconds = length;
  }

Here is the caller graph for this function:


Member Data Documentation

std::string Arc::Period::is [private]

Definition at line 87 of file DateTime.h.

time_t Arc::Period::seconds [private]

The duration of the period.

Definition at line 82 of file DateTime.h.

sigc::slot<const char*> Arc::Period::slot [private]

Definition at line 86 of file DateTime.h.


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