/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file.
// Time represents an absolute point in time, internally represented as // microseconds (s/1,000,000) since a platform-dependent epoch. Each // platform's epoch, along with other system-dependent clock interface // routines, is defined in time_PLATFORM.cc. // // TimeDelta represents a duration of time, internally represented in // microseconds. // // TimeTicks represents an abstract time that is always incrementing for use // in measuring time durations. It is internally represented in microseconds. // It can not be converted to a human-readable time, but is guaranteed not to // decrease (if the user changes the computer clock, Time::Now() may actually // decrease or jump). // // These classes are represented as only a 64-bit value, so they can be // efficiently passed by value.
#ifndef BASE_TIME_H_ #define BASE_TIME_H_
#include <time.h>
#include"base/basictypes.h"
namespace base {
class Time; class TimeTicks;
// This unit test does a lot of manual time manipulation. class PageLoadTrackerUnitTest;
class TimeDelta { public:
TimeDelta() : delta_(0) {}
// Converts units of time to TimeDeltas. static TimeDelta FromDays(int64_t days); static TimeDelta FromHours(int64_t hours); static TimeDelta FromMinutes(int64_t minutes); static TimeDelta FromSeconds(int64_t secs); static TimeDelta FromMilliseconds(int64_t ms); static TimeDelta FromMicroseconds(int64_t us);
// Returns the internal numeric value of the TimeDelta object. Please don't // use this and do arithmetic on it, as it is more error prone than using the // provided operators.
int64_t ToInternalValue() const { return delta_; }
// Returns the time delta in some unit. The F versions return a floating // point value, the "regular" versions return a rounded-down value. int InDays() const; int InHours() const; int InMinutes() const; double InSecondsF() const;
int64_t InSeconds() const; double InMillisecondsF() const;
int64_t InMilliseconds() const;
int64_t InMicroseconds() const;
// Constructs a delta given the duration in microseconds. This is private // to avoid confusion by callers with an integer constructor. Use // FromSeconds, FromMilliseconds, etc. instead. explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {}
// Represents an exploded time that can be formatted nicely. This is kind of // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few // additions and changes to prevent errors. struct Exploded { int year; // Four digit year "2007" signedchar month; // 1-based month (values 1 = January, etc.) signedchar day_of_week; // 0-based day of week (0 = Sunday, etc.) signedchar day_of_month; // 1-based day of month (1-31) signedchar hour; // Hour within the current day (0-23) signedchar minute; // Minute within the current hour (0-59) signedchar second; // Second within the current minute (0-59 plus // leap seconds which may take it up to 60). int millisecond; // Milliseconds within the current second (0-999)
};
// Contains the NULL time. Use Time::Now() to get the current time. explicit Time() : us_(0) {}
// Returns true if the time object has not been initialized. bool is_null() const { return us_ == 0; }
// Returns the current time. Watch out, the system might adjust its clock // in which case time will actually go backwards. We don't guarantee that // times are increasing, or that two calls to Now() won't be the same. static Time Now();
// Returns the current time. Same as Now() except that this function always // uses system time so that there are no discrepancies between the returned // time and system time even on virtual environments including our test bot. // For timing sensitive unittests, this function should be used. static Time NowFromSystemTime();
// Converts to/from time_t in UTC and a Time class. // TODO(brettw) this should be removed once everybody starts using the |Time| // class. static Time FromTimeT(time_t tt);
time_t ToTimeT() const;
// Converts time to/from a double which is the number of seconds since epoch // (Jan 1, 1970). Webkit uses this format to represent time. static Time FromDoubleT(double dt); double ToDoubleT() const;
// Converts an exploded structure representing either the local time or UTC // into a Time class. static Time FromUTCExploded(const Exploded& exploded) { return FromExploded(false, exploded);
} static Time FromLocalExploded(const Exploded& exploded) { return FromExploded(true, exploded);
}
// Converts an integer value representing Time to a class. This is used // when deserializing a |Time| structure, using a value known to be // compatible. It is not provided as a constructor because the integer type // may be unclear from the perspective of a caller. static Time FromInternalValue(int64_t us) { return Time(us); }
// Converts a string representation of time to a Time object. // An example of a time string which is converted is as below:- // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified // in the input string, we assume local time. // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to // a new time converter class. staticbool FromString(constwchar_t* time_string, Time* parsed_time);
// For serializing, use FromInternalValue to reconstitute. Please don't use // this and do arithmetic on it, as it is more error prone than using the // provided operators.
int64_t ToInternalValue() const { return us_; }
// Fills the given exploded structure with either the local time or UTC from // this time structure (containing UTC). void UTCExplode(Exploded* exploded) const { return Explode(false, exploded); } void LocalExplode(Exploded* exploded) const { return Explode(true, exploded);
}
// Rounds this time down to the nearest day in local time. It will represent // midnight on that day.
Time LocalMidnight() const;
// Compute the difference between two times.
TimeDelta operator-(Time other) const { return TimeDelta(us_ - other.us_); }
// Modify by some time delta.
Time& operator+=(TimeDelta delta) {
us_ += delta.delta_; return *this;
}
Time& operator-=(TimeDelta delta) {
us_ -= delta.delta_; return *this;
}
// Return a new time modified by some delta.
Time operator+(TimeDelta delta) const { return Time(us_ + delta.delta_); }
Time operator-(TimeDelta delta) const { return Time(us_ - delta.delta_); }
// Explodes the given time to either local time |is_local = true| or UTC // |is_local = false|. void Explode(bool is_local, Exploded* exploded) const;
// Unexplodes a given time assuming the source is either local time // |is_local = true| or UTC |is_local = false|. static Time FromExploded(bool is_local, const Exploded& exploded);
explicit Time(int64_t us) : us_(us) {}
// The representation of Jan 1, 1970 UTC in microseconds since the // platform-dependent epoch. staticconst int64_t kTimeTToMicrosecondsOffset;
// Platform-dependent tick count representing "right now." // The resolution of this clock is ~1-15ms. Resolution varies depending // on hardware/operating system configuration. static TimeTicks Now();
// Returns true if this object has not been initialized. bool is_null() const { return ticks_ == 0; }
// Returns the internal numeric value of the TimeTicks object.
int64_t ToInternalValue() const { return ticks_; }
// Please use Now() to create a new object. This is for internal use // and testing. Ticks is in microseconds. explicit TimeTicks(int64_t ticks) : ticks_(ticks) {}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.