/
dev-npgsql
/
npgsql
Обзор
Документация
Войти
/
dev-npgsql
/
npgsql
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
v9.0.2
src/Npgsql.NodaTime/Internal/DurationConverter.cs
42 строки
1 KB
Nino Floris
Handler rework (#5123)
26 сен 2023, 00:08
Не верифицирован
26 сен 2023, 00:08
0b7aecb
Код
Авторство
О чём код?
using System; using NodaTime; using Npgsql.Internal; using Npgsql.NodaTime.Properties; namespace Npgsql.NodaTime.Internal; sealed class DurationConverter : PgBufferedConverter<Duration> { public override bool CanConvert(DataFormat format, out BufferRequirements bufferRequirements) { bufferRequirements = BufferRequirements.CreateFixedSize(sizeof(long) + sizeof(int) + sizeof(int)); return format is DataFormat.Binary; } protected override Duration ReadCore(PgReader reader) { var microsecondsInDay = reader.ReadInt64(); var days = reader.ReadInt32(); var totalMonths = reader.ReadInt32(); if (totalMonths != 0) throw new InvalidCastException(NpgsqlNodaTimeStrings.CannotReadIntervalWithMonthsAsDuration); return Duration.FromDays(days) + Duration.FromNanoseconds(microsecondsInDay * 1000); } protected override void WriteCore(PgWriter writer, Duration value) { const long microsecondsPerSecond = 1_000_000; // Note that the end result must be long // see #3438 var microsecondsInDay = (((value.Hours * NodaConstants.MinutesPerHour + value.Minutes) * NodaConstants.SecondsPerMinute + value.Seconds) * microsecondsPerSecond + value.SubsecondNanoseconds / 1000); // Take the microseconds, discard the nanosecond remainder writer.WriteInt64(microsecondsInDay); writer.WriteInt32(value.Days); // days writer.WriteInt32(0); // months } }