Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
4113ba54
Commit
4113ba54
authored
9 years ago
by
Jan Tattermusch
Browse files
Options
Downloads
Patches
Plain Diff
implemented FromDateTime
parent
f6410f54
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/csharp/Grpc.Core.Tests/Internal/TimespecTest.cs
+14
-1
14 additions, 1 deletion
src/csharp/Grpc.Core.Tests/Internal/TimespecTest.cs
src/csharp/Grpc.Core/Internal/Timespec.cs
+35
-0
35 additions, 0 deletions
src/csharp/Grpc.Core/Internal/Timespec.cs
with
49 additions
and
1 deletion
src/csharp/Grpc.Core.Tests/Internal/TimespecTest.cs
+
14
−
1
View file @
4113ba54
...
...
@@ -41,9 +41,22 @@ namespace Grpc.Core.Internal.Tests
public
class
TimespecTest
{
[
Test
]
public
void
Now
()
public
void
Now_IsInUtc
()
{
Assert
.
AreEqual
(
DateTimeKind
.
Utc
,
Timespec
.
Now
.
ToDateTime
().
Kind
);
}
[
Test
]
public
void
Now_AgreesWithUtcNow
()
{
var
timespec
=
Timespec
.
Now
;
var
utcNow
=
DateTime
.
UtcNow
;
TimeSpan
difference
=
utcNow
-
timespec
.
ToDateTime
();
// This test is inherently a race - but the two timestamps
// should really be way less that a minute apart.
Assert
.
IsTrue
(
difference
.
TotalSeconds
<
60
);
}
[
Test
]
...
...
This diff is collapsed.
Click to expand it.
src/csharp/Grpc.Core/Internal/Timespec.cs
+
35
−
0
View file @
4113ba54
...
...
@@ -179,6 +179,41 @@ namespace Grpc.Core.Internal
return
tv_sec
.
ToInt64
()
>
0
?
DateTime
.
MaxValue
:
DateTime
.
MinValue
;
}
}
public
static
Timespec
FromDateTime
(
DateTime
dateTime
)
{
if
(
dateTime
==
DateTime
.
MaxValue
)
{
return
Timespec
.
InfFuture
;
}
if
(
dateTime
==
DateTime
.
MinValue
)
{
return
Timespec
.
InfPast
;
}
Preconditions
.
CheckArgument
(
dateTime
.
Kind
==
DateTimeKind
.
Utc
,
"dateTime"
);
try
{
TimeSpan
timeSpan
=
dateTime
-
UnixEpoch
;
long
ticks
=
timeSpan
.
Ticks
;
IntPtr
seconds
=
new
IntPtr
(
ticks
/
TicksPerSecond
);
// possible OverflowException
// (x % m + m) % m is workaround for modulo semantics with negative numbers.
int
nanos
=
(
int
)(((
ticks
%
TicksPerSecond
+
TicksPerSecond
)
%
TicksPerSecond
)
*
NanosPerTick
);
return
new
Timespec
(
seconds
,
nanos
);
}
catch
(
OverflowException
)
{
return
dateTime
>
UnixEpoch
?
Timespec
.
InfFuture
:
Timespec
.
InfPast
;
}
catch
(
ArgumentOutOfRangeException
)
{
return
dateTime
>
UnixEpoch
?
Timespec
.
InfFuture
:
Timespec
.
InfPast
;
}
}
internal
static
int
NativeSize
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment