DateTimeOffset INpgsqlSimpleTypeHandler <DateTimeOffset> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) { try { var value = buf.ReadInt64(); switch (value) { case long.MaxValue: return(DisableDateTimeInfinityConversions ? throw new InvalidCastException(InfinityExceptionMessage) : DateTimeOffset.MaxValue); case long.MinValue: return(DisableDateTimeInfinityConversions ? throw new InvalidCastException(InfinityExceptionMessage) : DateTimeOffset.MinValue); default: var dateTime = DecodeTimestamp(value, DateTimeKind.Utc); return(LegacyTimestampBehavior ? dateTime.ToLocalTime() : dateTime); } } catch (ArgumentOutOfRangeException e) { throw new InvalidCastException("Out of the range of DateTime (year must be between 1 and 9999)", e); } }
ZonedDateTime INpgsqlSimpleTypeHandler <ZonedDateTime> .Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription) { try { if (_integerFormat) { var value = buf.ReadInt64(); if (value == long.MaxValue || value == long.MinValue) { throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported for timestamp with time zone")); } return(TimestampHandler.Decode(value).InZone(_dateTimeZoneProvider[buf.Connection.Timezone])); } else { var value = buf.ReadDouble(); if (double.IsPositiveInfinity(value) || double.IsNegativeInfinity(value)) { throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported for timestamp with time zone")); } return(TimestampHandler.Decode(value).InZone(_dateTimeZoneProvider[buf.Connection.Timezone])); } } catch (TimeZoneNotFoundException) when(string.Equals(buf.Connection.Timezone, "localtime", StringComparison.OrdinalIgnoreCase)) { throw new NpgsqlSafeReadException( new TimeZoneNotFoundException( "The special PostgreSQL timezone 'localtime' is not supported when reading values of type 'timestamp with time zone'. " + "Please specify a real timezone in 'postgresql.conf' on the server, or set the 'PGTZ' environment variable on the client.")); } catch (TimeZoneNotFoundException e) { throw new NpgsqlSafeReadException(e); } }
public override Instant Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) { if (_integerFormat) { var value = buf.ReadInt64(); if (_convertInfinityDateTime) { if (value == long.MaxValue) { return(Instant.MaxValue); } if (value == long.MinValue) { return(Instant.MinValue); } } return(Decode(value)); } else { var value = buf.ReadDouble(); if (_convertInfinityDateTime) { if (double.IsPositiveInfinity(value)) { return(Instant.MaxValue); } if (double.IsNegativeInfinity(value)) { return(Instant.MinValue); } } return(Decode(value)); } }
public override decimal Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) { return(new DecimalRaw(buf.ReadInt64()) { Scale = MoneyScale }.Value); }
DateTimeOffset INpgsqlSimpleTypeHandler <DateTimeOffset> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) { var postgresTimestamp = buf.ReadInt64(); if (postgresTimestamp == long.MaxValue) { return(ConvertInfinityDateTime ? DateTimeOffset.MaxValue : throw new InvalidCastException(InfinityExceptionMessage)); } if (postgresTimestamp == long.MinValue) { return(ConvertInfinityDateTime ? DateTimeOffset.MinValue : throw new InvalidCastException(InfinityExceptionMessage)); } try { return(FromPostgresTimestamp(postgresTimestamp).ToLocalTime()); } catch (ArgumentOutOfRangeException e) { throw new InvalidCastException(OutOfRangeExceptionMessage, e); } }
ZonedDateTime INpgsqlSimpleTypeHandler <ZonedDateTime> .Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription) { try { if (_integerFormat) { var value = buf.ReadInt64(); if (value == long.MaxValue || value == long.MinValue) { throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported for timestamp with time zone")); } return(TimestampHandler.Decode(value).InZone(_dateTimeZoneProvider[buf.Connection.Timezone])); } else { var value = buf.ReadDouble(); if (double.IsPositiveInfinity(value) || double.IsNegativeInfinity(value)) { throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported for timestamp with time zone")); } return(TimestampHandler.Decode(value).InZone(_dateTimeZoneProvider[buf.Connection.Timezone])); } } catch (DateTimeZoneNotFoundException e) { throw new NpgsqlSafeReadException(e); } }
/// <inheritdoc /> public override DateTime Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { var postgresTimestamp = buf.ReadInt64(); if (postgresTimestamp == long.MaxValue) { return(ConvertInfinityDateTime ? DateTime.MaxValue : throw new InvalidCastException(InfinityExceptionMessage)); } if (postgresTimestamp == long.MinValue) { return(ConvertInfinityDateTime ? DateTime.MinValue : throw new InvalidCastException(InfinityExceptionMessage)); } try { return(FromPostgresTimestamp(postgresTimestamp)); } catch (ArgumentOutOfRangeException e) { throw new InvalidCastException(OutOfRangeExceptionMessage, e); } }
/// <inheritdoc /> protected override NpgsqlTimeSpan ReadPsv(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { var ticks = buf.ReadInt64(); var day = buf.ReadInt32(); var month = buf.ReadInt32(); return(new NpgsqlTimeSpan(month, day, ticks * 10)); }
NpgsqlInterval INpgsqlSimpleTypeHandler <NpgsqlInterval> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) { var ticks = buf.ReadInt64(); var day = buf.ReadInt32(); var month = buf.ReadInt32(); return(new NpgsqlInterval(month, day, ticks)); }
/// <inheritdoc /> public override DateTimeOffset Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { // Adjust from 1 microsecond to 100ns. Time zone (in seconds) is inverted. var ticks = buf.ReadInt64() * 10; var offset = new TimeSpan(0, 0, -buf.ReadInt32()); return(new DateTimeOffset(ticks + TimeSpan.TicksPerDay, offset)); }
// Binary Format: int64 expressing microseconds, int32 expressing timezone in seconds, negative #region Read public override DateTimeOffset Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) { // Adjust from 1 microsecond to 100ns. Time zone (in seconds) is inverted. var ticks = _integerFormat ? buf.ReadInt64() * 10 : (long)(buf.ReadDouble() * TimeSpan.TicksPerSecond); var offset = new TimeSpan(0, 0, -buf.ReadInt32()); return(new DateTimeOffset(ticks, offset)); }
public override decimal Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) { var result = new DecimalRaw(buf.ReadInt64()) { Scale = MoneyScale }; return(Unsafe.As <DecimalRaw, decimal>(ref result)); }
LocalDateTime INpgsqlSimpleTypeHandler <LocalDateTime> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) { var value = buf.ReadInt64(); if (value == long.MaxValue || value == long.MinValue) { throw new NotSupportedException("Infinity values not supported when reading LocalDateTime, read as Instant instead"); } return(Decode(value).InUtc().LocalDateTime); }
public override TimeSpan Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) { if (_integerFormat) { // PostgreSQL time resolution == 1 microsecond == 10 ticks return(new TimeSpan(buf.ReadInt64() * 10)); } else { return(TimeSpan.FromSeconds(buf.ReadDouble())); } }
public override LocalTime Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) { if (_integerFormat) { // PostgreSQL time resolution == 1 microsecond == 10 ticks return(LocalTime.FromTicksSinceMidnight(buf.ReadInt64() * 10)); } else { return(LocalTime.FromTicksSinceMidnight((long)(buf.ReadDouble() * NodaConstants.TicksPerSecond))); } }
/// <inheritdoc /> public override Guid Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { var raw = new GuidRaw { Data1 = buf.ReadInt32(), Data2 = buf.ReadInt16(), Data3 = buf.ReadInt16(), Data4 = buf.ReadInt64(BitConverter.IsLittleEndian) }; return(raw.Value); }
/// <inheritdoc /> public override TimeSpan Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { var microseconds = buf.ReadInt64(); var days = buf.ReadInt32(); var months = buf.ReadInt32(); if (months > 0) { throw new InvalidCastException("Cannot convert interval value with non-zero months to TimeSpan"); } return(new(microseconds * 10 + days * TimeSpan.TicksPerDay)); }
Duration INpgsqlSimpleTypeHandler <Duration> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) { var microsecondsInDay = buf.ReadInt64(); var days = buf.ReadInt32(); var totalMonths = buf.ReadInt32(); if (totalMonths != 0) { throw new NpgsqlException("Cannot read PostgreSQL interval with non-zero months to NodaTime Duration. Try reading as a NodaTime Period instead."); } return(Duration.FromDays(days) + Duration.FromNanoseconds(microsecondsInDay * 1000)); }
public override Instant Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { var value = buf.ReadInt64(); if (_convertInfinityDateTime) { if (value == long.MaxValue) { return(Instant.MaxValue); } if (value == long.MinValue) { return(Instant.MinValue); } } return(TimestampHandler.Decode(value)); }
protected override NpgsqlTimeSpan ReadPsv(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) { if (_integerFormat) { var ticks = buf.ReadInt64(); var day = buf.ReadInt32(); var month = buf.ReadInt32(); return(new NpgsqlTimeSpan(month, day, ticks * 10)); } else { var seconds = buf.ReadDouble(); var day = buf.ReadInt32(); var month = buf.ReadInt32(); return(new NpgsqlTimeSpan(month, day, (long)(seconds * TimeSpan.TicksPerSecond))); } }
public override Period Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { var microsecondsInDay = buf.ReadInt64(); var days = buf.ReadInt32(); var totalMonths = buf.ReadInt32(); // NodaTime will normalize most things (i.e. nanoseconds to milliseconds, seconds...) // but it will not normalize months to years. var months = totalMonths % 12; var years = totalMonths / 12; return(new PeriodBuilder { Nanoseconds = microsecondsInDay * 1000, Days = days, Months = months, Years = years }.Build().Normalize()); }
LocalDateTime INpgsqlSimpleTypeHandler <LocalDateTime> .Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription) { if (_integerFormat) { var value = buf.ReadInt64(); if (value == long.MaxValue || value == long.MinValue) { throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported when reading LocalDateTime, read as Instant instead")); } return(Decode(value).InUtc().LocalDateTime); } else { var value = buf.ReadDouble(); if (double.IsPositiveInfinity(value) || double.IsNegativeInfinity(value)) { throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported when reading LocalDateTime, read as Instant instead")); } return(Decode(value).InUtc().LocalDateTime); } }
public override Instant Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) { if (_integerFormat) { var value = buf.ReadInt64(); if (value == long.MaxValue || value == long.MinValue) { throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported for timestamp with time zone")); } return(TimestampHandler.Decode(value)); } else { var value = buf.ReadDouble(); if (double.IsPositiveInfinity(value) || double.IsNegativeInfinity(value)) { throw new NpgsqlSafeReadException(new NotSupportedException("Infinity values not supported for timestamp with time zone")); } return(TimestampHandler.Decode(value)); } }
/// <summary> /// Reads a timestamp from the buffer as an <see cref="NpgsqlDateTime"/>. /// </summary> protected NpgsqlDateTime ReadTimeStamp(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) { var value = buf.ReadInt64(); if (value == long.MaxValue) { return(NpgsqlDateTime.Infinity); } if (value == long.MinValue) { return(NpgsqlDateTime.NegativeInfinity); } if (value >= 0) { var date = (int)(value / 86400000000L); var time = value % 86400000000L; date += 730119; // 730119 = days since era (0001-01-01) for 2000-01-01 time *= 10; // To 100ns return(new NpgsqlDateTime(new NpgsqlDate(date), new TimeSpan(time))); } else { value = -value; var date = (int)(value / 86400000000L); var time = value % 86400000000L; if (time != 0) { ++date; time = 86400000000L - time; } date = 730119 - date; // 730119 = days since era (0001-01-01) for 2000-01-01 time *= 10; // To 100ns return(new NpgsqlDateTime(new NpgsqlDate(date), new TimeSpan(time))); } }
ZonedDateTime INpgsqlSimpleTypeHandler <ZonedDateTime> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) { try { var zonedDateTime = ((INpgsqlSimpleTypeHandler <ZonedDateTime>)_wrappedHandler).Read(buf, len, fieldDescription); var value = buf.ReadInt64(); if (value == long.MaxValue || value == long.MinValue) { throw new NotSupportedException("Infinity values not supported for timestamp with time zone"); } return(zonedDateTime.WithZone(_dateTimeZoneProvider[buf.Connection.Timezone])); } catch (Exception e) when( string.Equals(buf.Connection.Timezone, "localtime", StringComparison.OrdinalIgnoreCase) && (e is TimeZoneNotFoundException || e is DateTimeZoneNotFoundException)) { throw new TimeZoneNotFoundException( "The special PostgreSQL timezone 'localtime' is not supported when reading values of type 'timestamp with time zone'. " + "Please specify a real timezone in 'postgresql.conf' on the server, or set the 'PGTZ' environment variable on the client.", e); } }
long INpgsqlSimpleTypeHandler <long> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription) => buf.ReadInt64();
// Adjust from 1 microsecond to 100ns. Time zone (in seconds) is inverted. public override OffsetTime Read(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null) => new OffsetTime(_integerFormat ? LocalTime.FromTicksSinceMidnight(buf.ReadInt64() * 10) : LocalTime.FromTicksSinceMidnight((long)(buf.ReadDouble() * NodaConstants.TicksPerSecond)), Offset.FromSeconds(-buf.ReadInt32()));
internal static Instant ReadInstant(NpgsqlReadBuffer buf) => buf.ReadInt64() switch {
// TODO: Switch to use LocalDateTime.MinMaxValue when available (#4061) internal static LocalDateTime ReadLocalDateTime(NpgsqlReadBuffer buf) => buf.ReadInt64() switch {
/// <inheritdoc /> public override long Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null) => buf.ReadInt64();