Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
96833b8f
Commit
96833b8f
authored
Jul 09, 2018
by
Warren Falk
Committed by
Jie Luo
Jul 09, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement IComparable and comparison operators on Timestamp (#4318)
parent
5524c53e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
205 additions
and
1 deletion
+205
-1
TimestampTest.cs
.../src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs
+101
-0
TimestampPartial.cs
...rp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs
+104
-1
No files found.
csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs
View file @
96833b8f
...
...
@@ -111,5 +111,106 @@ namespace Google.Protobuf.WellKnownTypes
var
duration
=
new
Timestamp
{
Seconds
=
1
,
Nanos
=
-
1
};
Assert
.
AreEqual
(
"{ \"@warning\": \"Invalid Timestamp\", \"seconds\": \"1\", \"nanos\": -1 }"
,
duration
.
ToString
());
}
[
Test
]
public
void
Comparability
()
{
Timestamp
a
=
null
,
b
=
new
Timestamp
{
Seconds
=
1
,
Nanos
=
1
},
c
=
new
Timestamp
{
Seconds
=
1
,
Nanos
=
10
},
d
=
new
Timestamp
{
Seconds
=
10
,
Nanos
=
1
},
e
=
new
Timestamp
{
Seconds
=
10
,
Nanos
=
10
};
Assert
.
IsTrue
(
b
.
CompareTo
(
a
)
>
0
);
// null is always first (according to default behavior of Array.Sort)
Assert
.
IsTrue
(
b
.
CompareTo
(
b
)
==
0
);
Assert
.
IsTrue
(
b
.
CompareTo
(
b
.
Clone
())
==
0
);
Assert
.
IsTrue
(
b
.
CompareTo
(
c
)
<
0
);
Assert
.
IsTrue
(
b
.
CompareTo
(
d
)
<
0
);
Assert
.
IsTrue
(
b
.
CompareTo
(
e
)
<
0
);
Assert
.
IsTrue
(
c
.
CompareTo
(
a
)
>
0
);
Assert
.
IsTrue
(
c
.
CompareTo
(
b
)
>
0
);
Assert
.
IsTrue
(
c
.
CompareTo
(
c
)
==
0
);
Assert
.
IsTrue
(
c
.
CompareTo
(
c
.
Clone
())
==
0
);
Assert
.
IsTrue
(
c
.
CompareTo
(
d
)
<
0
);
Assert
.
IsTrue
(
c
.
CompareTo
(
e
)
<
0
);
Assert
.
IsTrue
(
d
.
CompareTo
(
a
)
>
0
);
Assert
.
IsTrue
(
d
.
CompareTo
(
b
)
>
0
);
Assert
.
IsTrue
(
d
.
CompareTo
(
c
)
>
0
);
Assert
.
IsTrue
(
d
.
CompareTo
(
d
)
==
0
);
Assert
.
IsTrue
(
d
.
CompareTo
(
d
.
Clone
())
==
0
);
Assert
.
IsTrue
(
d
.
CompareTo
(
e
)
<
0
);
Assert
.
IsTrue
(
e
.
CompareTo
(
a
)
>
0
);
Assert
.
IsTrue
(
e
.
CompareTo
(
b
)
>
0
);
Assert
.
IsTrue
(
e
.
CompareTo
(
c
)
>
0
);
Assert
.
IsTrue
(
e
.
CompareTo
(
d
)
>
0
);
Assert
.
IsTrue
(
e
.
CompareTo
(
e
)
==
0
);
Assert
.
IsTrue
(
e
.
CompareTo
(
e
.
Clone
())
==
0
);
}
[
Test
]
public
void
ComparabilityOperators
()
{
Timestamp
a
=
null
,
b
=
new
Timestamp
{
Seconds
=
1
,
Nanos
=
1
},
c
=
new
Timestamp
{
Seconds
=
1
,
Nanos
=
10
},
d
=
new
Timestamp
{
Seconds
=
10
,
Nanos
=
1
},
e
=
new
Timestamp
{
Seconds
=
10
,
Nanos
=
10
};
#pragma warning disable CS1718 // Comparison made to same variable
Assert
.
IsTrue
(
b
>
a
);
Assert
.
IsTrue
(
b
==
b
);
Assert
.
IsTrue
(
b
==
b
.
Clone
());
Assert
.
IsTrue
(
b
<
c
);
Assert
.
IsTrue
(
b
<
d
);
Assert
.
IsTrue
(
b
<
e
);
Assert
.
IsTrue
(
c
>
a
);
Assert
.
IsTrue
(
c
>
b
);
Assert
.
IsTrue
(
c
==
c
);
Assert
.
IsTrue
(
c
==
c
.
Clone
());
Assert
.
IsTrue
(
c
<
d
);
Assert
.
IsTrue
(
c
<
e
);
Assert
.
IsTrue
(
d
>
a
);
Assert
.
IsTrue
(
d
>
b
);
Assert
.
IsTrue
(
d
>
c
);
Assert
.
IsTrue
(
d
==
d
);
Assert
.
IsTrue
(
d
==
d
.
Clone
());
Assert
.
IsTrue
(
d
<
e
);
Assert
.
IsTrue
(
e
>
a
);
Assert
.
IsTrue
(
e
>
b
);
Assert
.
IsTrue
(
e
>
c
);
Assert
.
IsTrue
(
e
>
d
);
Assert
.
IsTrue
(
e
==
e
);
Assert
.
IsTrue
(
e
==
e
.
Clone
());
Assert
.
IsTrue
(
b
>=
a
);
Assert
.
IsTrue
(
b
<=
c
);
Assert
.
IsTrue
(
b
<=
d
);
Assert
.
IsTrue
(
b
<=
e
);
Assert
.
IsTrue
(
c
>=
a
);
Assert
.
IsTrue
(
c
>=
b
);
Assert
.
IsTrue
(
c
<=
d
);
Assert
.
IsTrue
(
c
<=
e
);
Assert
.
IsTrue
(
d
>=
a
);
Assert
.
IsTrue
(
d
>=
b
);
Assert
.
IsTrue
(
d
>=
c
);
Assert
.
IsTrue
(
d
<=
e
);
Assert
.
IsTrue
(
e
>=
a
);
Assert
.
IsTrue
(
e
>=
b
);
Assert
.
IsTrue
(
e
>=
c
);
Assert
.
IsTrue
(
e
>=
d
);
#pragma warning restore CS1718 // Comparison made to same variable
}
}
}
csharp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs
View file @
96833b8f
...
...
@@ -36,7 +36,7 @@ using System.Text;
namespace
Google.Protobuf.WellKnownTypes
{
public
partial
class
Timestamp
:
ICustomDiagnosticMessage
public
partial
class
Timestamp
:
ICustomDiagnosticMessage
,
IComparable
<
Timestamp
>
{
private
static
readonly
DateTime
UnixEpoch
=
new
DateTime
(
1970
,
1
,
1
,
0
,
0
,
0
,
DateTimeKind
.
Utc
);
// Constants determined programmatically, but then hard-coded so they can be constant expressions.
...
...
@@ -222,6 +222,109 @@ namespace Google.Protobuf.WellKnownTypes
}
}
/// <summary>
/// Given another timestamp, returns 0 if the timestamps are equivalent, -1 if this timestamp precedes the other, and 1 otherwise
/// </summary>
/// <remarks>
/// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results.
/// </remarks>
/// <param name="other">Timestamp to compare</param>
/// <returns>an integer indicating whether this timestamp precedes or follows the other</returns>
public
int
CompareTo
(
Timestamp
other
)
{
return
other
==
null
?
1
:
Seconds
<
other
.
Seconds
?
-
1
:
Seconds
>
other
.
Seconds
?
1
:
Nanos
<
other
.
Nanos
?
-
1
:
Nanos
>
other
.
Nanos
?
1
:
0
;
}
/// <summary>
/// Compares two timestamps and returns whether the first is less than (chronologically precedes) the second
/// </summary>
/// <remarks>
/// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results.
/// </remarks>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns>true if a precedes b</returns>
public
static
bool
operator
<(
Timestamp
a
,
Timestamp
b
)
{
return
a
.
CompareTo
(
b
)
<
0
;
}
/// <summary>
/// Compares two timestamps and returns whether the first is greater than (chronologically follows) the second
/// </summary>
/// <remarks>
/// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results.
/// </remarks>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns>true if a follows b</returns>
public
static
bool
operator
>(
Timestamp
a
,
Timestamp
b
)
{
return
a
.
CompareTo
(
b
)
>
0
;
}
/// <summary>
/// Compares two timestamps and returns whether the first is less than (chronologically precedes) the second
/// </summary>
/// <remarks>
/// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results.
/// </remarks>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns>true if a precedes b</returns>
public
static
bool
operator
<=(
Timestamp
a
,
Timestamp
b
)
{
return
a
.
CompareTo
(
b
)
<=
0
;
}
/// <summary>
/// Compares two timestamps and returns whether the first is greater than (chronologically follows) the second
/// </summary>
/// <remarks>
/// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results.
/// </remarks>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns>true if a follows b</returns>
public
static
bool
operator
>=(
Timestamp
a
,
Timestamp
b
)
{
return
a
.
CompareTo
(
b
)
>=
0
;
}
/// <summary>
/// Returns whether two timestamps are equivalent
/// </summary>
/// <remarks>
/// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results.
/// </remarks>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns>true if the two timestamps refer to the same nanosecond</returns>
public
static
bool
operator
==(
Timestamp
a
,
Timestamp
b
)
{
return
ReferenceEquals
(
a
,
b
)
||
(
a
is
null
?
(
b
is
null
?
true
:
false
)
:
a
.
Equals
(
b
));
}
/// <summary>
/// Returns whether two timestamps differ
/// </summary>
/// <remarks>
/// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results.
/// </remarks>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns>true if the two timestamps differ</returns>
public
static
bool
operator
!=(
Timestamp
a
,
Timestamp
b
)
{
return
!(
a
==
b
);
}
/// <summary>
/// Returns a string representation of this <see cref="Timestamp"/> for diagnostic purposes.
/// </summary>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment