// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // namespace IntegrationTests.Helpers; internal static class TimeExtensions { private const long NanoSecondsPerTick = 1000000 / TimeSpan.TicksPerMillisecond; private const long UnixEpochInTicks = 621355968000000000; // = DateTimeOffset.FromUnixTimeMilliseconds(0).Ticks /// /// Returns the number of nanoseconds that have elapsed since 1970-01-01T00:00:00.000Z. /// /// The value to get the number of elapsed nanoseconds for. /// The number of nanoseconds that have elapsed since 1970-01-01T00:00:00.000Z. public static long ToUnixTimeNanoseconds(this DateTimeOffset dateTimeOffset) { return (dateTimeOffset.Ticks - UnixEpochInTicks) * NanoSecondsPerTick; } /// /// Reconverts a long timestamp from TimeExtensions.ToUnixTimeMicroseconds. /// /// The unix time in microseconds. /// The corresponding DateTimeOffset. public static DateTimeOffset UnixMicrosecondsToDateTimeOffset(this long ts) { const long microsecondsPerMillisecond = 1000; const long ticksPerMicrosecond = TimeSpan.TicksPerMillisecond / microsecondsPerMillisecond; var ticks = (ts * ticksPerMicrosecond) + UnixEpochInTicks; return new DateTimeOffset(ticks, TimeSpan.Zero); } }