Google.Protobuf.WellKnownTypes.Duration

A Duration represents a signed, fixed-length span of time represented as a count of seconds and fractions of seconds at nanosecond resolution.

Summary

It is independent of any calendar and concepts like "day" or "month". It is related to Timestamp in that the difference between two Timestamp values is a Duration and it can be added or subtracted from a Timestamp. Range is approximately +-10,000 years.

Example 1: Compute Duration from two Timestamps in pseudo code.

Timestamp start = ...;
Timestamp end = ...;
Duration duration = ...;

duration.seconds = end.seconds - start.seconds;
duration.nanos = end.nanos - start.nanos;

if (duration.seconds < 0 && duration.nanos > 0) {
  duration.seconds += 1;
  duration.nanos -= 1000000000;
} else if (durations.seconds > 0 && duration.nanos < 0) {
  duration.seconds -= 1;
  duration.nanos += 1000000000;
}

Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.

Timestamp start = ...;
Duration duration = ...;
Timestamp end = ...;

end.seconds = start.seconds + duration.seconds;
end.nanos = start.nanos + duration.nanos;

if (end.nanos < 0) {
  end.seconds -= 1;
  end.nanos += 1000000000;
} else if (end.nanos >= 1000000000) {
  end.seconds += 1;
  end.nanos -= 1000000000;
}

Example 3: Compute Duration from datetime.timedelta in Python.

td = datetime.timedelta(days=3, minutes=10)
duration = Duration()
duration.FromTimedelta(td)

Inheritance

Inherits from: Google.Protobuf.ICustomDiagnosticMessage, pb::IMessage< Duration >

Constructors and Destructors

Duration()
Duration(Duration other)

Properties

Descriptor
pbr::MessageDescriptor
Descriptor
pbr::MessageDescriptor pb::IMessage.
Nanos
int
Signed fractions of a second at nanosecond resolution of the span of time.
Parser
pb::MessageParser< Duration >
Seconds
long
Signed seconds of the span of time.

Public attributes

MaxSeconds = 315576000000L
const long
The maximum permitted number of seconds.
MinSeconds = -315576000000L
const long
The minimum permitted number of seconds.
NanosFieldNumber = 2
const int
Field number for the "nanos" field.
NanosecondsPerSecond = 1000000000
const int
The number of nanoseconds in a second.
NanosecondsPerTick = 100
const int
The number of nanoseconds in a BCL tick (as used by TimeSpan and DateTime).
SecondsFieldNumber = 1
const int
Field number for the "seconds" field.

Public functions

CalculateSize()
int
Calculates the size of this message in Protocol Buffer wire format, in bytes.
Clone()
Duration
Equals(object other)
override bool
Equals(Duration other)
bool
GetHashCode()
override int
MergeFrom(Duration other)
void
MergeFrom(pb::CodedInputStream input)
void
ToDiagnosticString()
string
Returns a string representation of this Duration for diagnostic purposes.
ToString()
override string
ToTimeSpan()
TimeSpan
Converts this Duration to a TimeSpan.
WriteTo(pb::CodedOutputStream output)
void

Public static functions

FromTimeSpan(TimeSpan timeSpan)
Duration
Converts the given TimeSpan to a Duration.
operator+(Duration lhs, Duration rhs)
Duration
Adds the two specified Duration values together.
operator-(Duration value)
Duration
Returns the result of negating the duration.
operator-(Duration lhs, Duration rhs)
Duration
Subtracts one Duration from another.

Properties

Descriptor

pbr::MessageDescriptor Descriptor

Descriptor

pbr::MessageDescriptor pb::IMessage. Descriptor

Nanos

int Nanos

Signed fractions of a second at nanosecond resolution of the span of time.

Durations less than one second are represented with a 0 seconds field and a positive or negative nanos field. For durations of one second or more, a non-zero value for the nanos field must be of the same sign as the seconds field. Must be from -999,999,999 to +999,999,999 inclusive.

Parser

pb::MessageParser< Duration > Parser

Seconds

long Seconds

Signed seconds of the span of time.

Must be from -315,576,000,000 to +315,576,000,000 inclusive.

Public attributes

MaxSeconds

const long MaxSeconds = 315576000000L

The maximum permitted number of seconds.

MinSeconds

const long MinSeconds = -315576000000L

The minimum permitted number of seconds.

NanosFieldNumber

const int NanosFieldNumber = 2

Field number for the "nanos" field.

NanosecondsPerSecond

const int NanosecondsPerSecond = 1000000000

The number of nanoseconds in a second.

NanosecondsPerTick

const int NanosecondsPerTick = 100

The number of nanoseconds in a BCL tick (as used by TimeSpan and DateTime).

SecondsFieldNumber

const int SecondsFieldNumber = 1

Field number for the "seconds" field.

Public functions

CalculateSize

int CalculateSize()

Calculates the size of this message in Protocol Buffer wire format, in bytes.

Details
Returns
The number of bytes required to write this message to a coded output stream.

Clone

Duration Clone()

Duration

 Duration()

Duration

 Duration(
  Duration other
)

Equals

override bool Equals(
  object other
)

Equals

bool Equals(
  Duration other
)

GetHashCode

override int GetHashCode()

MergeFrom

void MergeFrom(
  Duration other
)

MergeFrom

void MergeFrom(
  pb::CodedInputStream input
)

ToDiagnosticString

string ToDiagnosticString()

Returns a string representation of this Duration for diagnostic purposes.

Normally the returned value will be a JSON string value (including leading and trailing quotes) but when the value is non-normalized or out of range, a JSON object representation will be returned instead, including a warning. This is to avoid exceptions being thrown when trying to diagnose problems - the regular JSON formatter will still throw an exception for non-normalized values.

Details
Returns
A string representation of this value.

ToString

override string ToString()

ToTimeSpan

TimeSpan ToTimeSpan()

Converts this Duration to a TimeSpan.

If the duration is not a precise number of ticks, it is truncated towards 0.

Details
Exceptions
InvalidOperationException
This value isn't a valid normalized duration, as described in the documentation.
Returns
The value of this duration, as a TimeSpan.

WriteTo

void WriteTo(
  pb::CodedOutputStream output
)

Public static functions

FromTimeSpan

Duration FromTimeSpan(
  TimeSpan timeSpan
)

Converts the given TimeSpan to a Duration.

Details
Parameters
timeSpan
The TimeSpan to convert.
Returns
The value of the given TimeSpan, as a Duration.

operator+

Duration operator+(
  Duration lhs,
  Duration rhs
)

Adds the two specified Duration values together.

Details
Parameters
lhs
The first value to add. Must not be null.
rhs
The second value to add. Must not be null.
Returns

operator-

Duration operator-(
  Duration value
)

Returns the result of negating the duration.

For example, the negation of 5 minutes is -5 minutes.

Details
Parameters
value
The duration to negate. Must not be null.
Returns
The negated value of this duration.

operator-

Duration operator-(
  Duration lhs,
  Duration rhs
)

Subtracts one Duration from another.

Details
Parameters
lhs
The duration to subtract from. Must not be null.
rhs
The duration to subtract. Must not be null.
Returns
The difference between the two specified durations.