Example #1
0
TEST(DurationTest, Arithmetic)
{
  Duration d = Seconds(11);
  d += Seconds(9);
  EXPECT_EQ(Seconds(20), d);

  d = Seconds(11);
  d -= Seconds(21);
  EXPECT_EQ(Seconds(-10), d);

  d = Seconds(10);
  d *= 2;
  EXPECT_EQ(Seconds(20), d);

  d = Seconds(10);
  d /= 2.5;
  EXPECT_EQ(Seconds(4), d);

  EXPECT_EQ(Seconds(20), Seconds(11) + Seconds(9));
  EXPECT_EQ(Seconds(-10), Seconds(11) - Seconds(21));
  EXPECT_EQ(Duration::create(3.3).get(), Seconds(10) * 0.33);
  EXPECT_EQ(Duration::create(1.25).get(), Seconds(10) / 8);

  EXPECT_EQ(Duration::create(Days(11).secs() + 9).get(), Days(11) + Seconds(9));
}
Example #2
0
TEST(DurationTest, OutputFormat)
{
  EXPECT_EQ("1ns", stringify(Nanoseconds(1)));
  EXPECT_EQ("2ns", stringify(Nanoseconds(2)));

  // Truncated. Seconds in 15 digits of precision, max of double
  // type's precise digits.
  EXPECT_EQ("3.141592653secs",
            stringify(Duration::create(3.14159265358979).get()));
  EXPECT_EQ("3140ms", stringify(Duration::create(3.14).get()));
  EXPECT_EQ("10hrs", stringify(Hours(10)));
  EXPECT_EQ("-10hrs", stringify(Hours(-10)));

  // "10days" reads better than "1.42857142857143weeks" so it is
  // printed out in the lower unit.
  EXPECT_EQ("10days", stringify(Days(10)));
  // We go one-level down and it is still not a whole number so we
  // print it out using the higher unit.
  EXPECT_EQ("1.1875days", stringify(Days(1) + Hours(4) + Minutes(30)));
  // "2weeks" reads better than "14days" so we use the higher unit
  // here.
  EXPECT_EQ("2weeks", stringify(Days(14)));

  // Boundary cases.
  EXPECT_EQ("0ns", stringify(Duration::zero()));
  EXPECT_EQ("15250.2844524715weeks", stringify(Duration::max()));
  EXPECT_EQ("-15250.2844524715weeks", stringify(Duration::min()));
}
Example #3
0
/**
 * return 1 if date is invalid
 */
static int
ParseEventDate(const char *date, event_t * t)
{
    int retval = ParseDate(date, &t->year, &t->month, &t->day);
    if (retval)
	return retval;
    t->days = Days(t->year, t->month, t->day);
    return retval;
}
Example #4
0
TEST(DurationTest, Comparison)
{
  EXPECT_EQ(Duration::zero(), Seconds(0));
  EXPECT_EQ(Minutes(180), Hours(3));
  EXPECT_EQ(Seconds(10800), Hours(3));
  EXPECT_EQ(Milliseconds(10800000), Hours(3));

  EXPECT_EQ(Milliseconds(1), Microseconds(1000));
  EXPECT_EQ(Milliseconds(1000), Seconds(1));

  EXPECT_GT(Weeks(1), Days(6));

  EXPECT_LT(Hours(23), Days(1));

  EXPECT_LE(Hours(24), Days(1));
  EXPECT_GE(Hours(24), Days(1));

  EXPECT_NE(Minutes(59), Hours(1));

  // Maintains precision for a 100 year duration.
  EXPECT_GT(Weeks(5217) + Nanoseconds(1), Weeks(5217));
  EXPECT_LT(Weeks(5217) - Nanoseconds(1), Weeks(5217));
}
Example #5
0
	Days operator -(const Date& to, const Date& from)
	{
		return Days(detail::DateToEpochDays(to) - detail::DateToEpochDays(from));
	}
Example #6
0
inline std::ostream& operator<<(std::ostream& stream, const Duration& duration_)
{
  // Output the duration in full double precision and save the old precision.
  std::streamsize precision =
    stream.precision(std::numeric_limits<double>::digits10);

  // Parse the duration as the sign and the absolute value.
  Duration duration = duration_;
  if (duration_ < Duration::zero()) {
    stream << "-";

    // Duration::min() may not be representable as a positive Duration.
    if (duration_ == Duration::min()) {
      duration = Duration::max();
    } else {
      duration = duration_ * -1;
    }
  }

  // First determine which bucket of time unit the duration falls into
  // then check whether the duration can be represented as a whole
  // number with this time unit or a smaller one.
  // e.g. 1.42857142857143weeks falls into the 'Weeks' bucket but
  // reads better with a smaller unit: '10days'. So we use 'days'
  // instead of 'weeks' to output the duration.
  int64_t nanoseconds = duration.ns();
  if (duration < Microseconds(1)) {
    stream << duration.ns() << Nanoseconds::units();
  } else if (duration < Milliseconds(1)) {
    if (nanoseconds % Duration::MICROSECONDS != 0) {
      // We can't get a whole number using this unit but we can at
      // one level down.
      stream << duration.ns() << Nanoseconds::units();
    } else {
      stream << duration.us() << Microseconds::units();
    }
  } else if (duration < Seconds(1)) {
    if (nanoseconds % Duration::MILLISECONDS != 0 &&
        nanoseconds % Duration::MICROSECONDS == 0) {
      stream << duration.us() << Microseconds::units();
    } else {
      stream << duration.ms() << Milliseconds::units();
    }
  } else if (duration < Minutes(1)) {
    if (nanoseconds % Duration::SECONDS != 0 &&
        nanoseconds % Duration::MILLISECONDS == 0) {
      stream << duration.ms() << Milliseconds::units();
    } else {
      stream << duration.secs() << Seconds::units();
    }
  } else if (duration < Hours(1)) {
    if (nanoseconds % Duration::MINUTES != 0 &&
        nanoseconds % Duration::SECONDS == 0) {
      stream << duration.secs() << Seconds::units();
    } else {
      stream << duration.mins() << Minutes::units();
    }
  } else if (duration < Days(1)) {
    if (nanoseconds % Duration::HOURS != 0 &&
        nanoseconds % Duration::MINUTES == 0) {
      stream << duration.mins() << Minutes::units();
    } else {
      stream << duration.hrs() << Hours::units();
    }
  } else if (duration < Weeks(1)) {
    if (nanoseconds % Duration::DAYS != 0 &&
        nanoseconds % Duration::HOURS == 0) {
      stream << duration.hrs() << Hours::units();
    } else {
      stream << duration.days() << Days::units();
    }
  } else {
    if (nanoseconds % Duration::WEEKS != 0 &&
        nanoseconds % Duration::DAYS == 0) {
      stream << duration.days() << Days::units();
    } else {
      stream << duration.weeks() << Weeks::units();
    }
  }

  // Return the stream to original formatting state.
  stream.precision(precision);

  return stream;
}
Example #7
0
	/// The current day of the period
	int Day(void) const
	{
		return int(Days());
	}
Example #8
0
		[[nodiscard]] inline constexpr Days operator ""_d(unsigned long long days)
		{
			return Days(days);
		}
Example #9
0
static int
GenerateCalendar(char **buf, int y, int m, int today, event_t * e)
{
    char    *week_str[7] = {"日", "一", "二", "三", "四", "五", "六"};
    char    *month_color[12] = {
	ANSI_COLOR(1;32), ANSI_COLOR(1;33), ANSI_COLOR(1;35), ANSI_COLOR(1;36),
	ANSI_COLOR(1;32), ANSI_COLOR(1;33), ANSI_COLOR(1;35), ANSI_COLOR(1;36),
	ANSI_COLOR(1;32), ANSI_COLOR(1;33), ANSI_COLOR(1;35), ANSI_COLOR(1;36)
    };
    char    *month_str[12] = {
	"一月  ", "二月  ", "三月  ", "四月  ", "五月  ", "六月  ",
	"七月  ", "八月  ", "九月  ", "十月  ", "十一月", "十二月"
    };

    char           *p, attr1[16], *attr2;
    int             i, d, w, line = 0, first_day = Days(y, m, 1);


    /* week day banner */
    p = buf[line];
    p += sprintf(p, "    %s%s%s%s", HEADER_COLOR, HEADER_SUNDAY_COLOR,
		 week_str[0], HEADER_DAY_COLOR);
    for (i = 1; i < 7; i++)
	p += sprintf(p, " %s", week_str[i]);
    p += sprintf(p, ANSI_RESET);

    /* indent for first line */
    p = buf[++line];
    p += sprintf(p, "    %s", CALENDAR_COLOR);
    for (i = 0, w = first_day % 7; i < w; i++)
	p += sprintf(p, "   ");