Skip to content

Commit 78b511f

Browse files
committed
Fix exception in conversion from DateTime[Offset] to Timestamp. #296
If the date time is before Unix epoc and it has subsecond value, internal divrem operation returns negative remains and it causes exception. This commit add adjustment for this situation.
1 parent 26022db commit 78b511f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/MsgPack/Timestamp.Conversion.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ private static void FromOffsetTicks( long ticks, out long unixEpocSeconds, out i
133133
long remaining;
134134
unixEpocSeconds = DivRem( ticks, SecondsToTicks, out remaining );
135135
nanoSeconds = unchecked( ( int )remaining ) * 100;
136+
if ( nanoSeconds < 0 )
137+
{
138+
// In this case, we must adjust these values
139+
// from "negative nanosec from nearest larger negative integer"
140+
// to "positive nanosec from nearest smaller nagative integer".
141+
unixEpocSeconds -= 1;
142+
nanoSeconds = ( MaxNanoSeconds + 1 ) + nanoSeconds;
143+
}
136144
}
137145

138146
/// <summary>

0 commit comments

Comments
 (0)