Permalink
Browse files

Handle exception when timezone is `localtime` (#2264)

The configuration property `timezone` in `postgresql.conf` can be
set to a special value of `localtime` to indicate that PosgreSQL
should use the timezone of the host system.

Unfortunately, when this value is set, we do not have sufficient
information to determine the actual timezone of the server because
`SHOW timezone` itself returns `localtime`.

This value is not well-documented, but is also unlikely to be
encountered very often, so we identify the exception and construct
a new `TimeZoneNotFoundException` with a more informative
message that includes mitigations that can be applied on either
the server or the client.

See: npgsql/Npgsql.EntityFrameworkCore.PostgreSQL#578
  • Loading branch information...
austindrenski committed Dec 22, 2018
1 parent f60e2a0 commit 140c1b4afb820446b7f19f38f8dffe50d0e7502e
Showing with 9 additions and 1 deletion.
  1. +1 −0 Npgsql.sln.DotSettings
  2. +8 −1 src/Npgsql.NodaTime/TimestampTzHandler.cs
@@ -82,6 +82,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Noda/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Npgsql/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Npgsql_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=PGTZ/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Postgre/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=P_0020keepaliv/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=resultset/@EntryIndexedValue">True</s:Boolean>
@@ -93,7 +93,14 @@ ZonedDateTime INpgsqlSimpleTypeHandler<ZonedDateTime>.Read(NpgsqlReadBuffer buf,
return TimestampHandler.Decode(value).InZone(_dateTimeZoneProvider[buf.Connection.Timezone]);
}
}
catch (DateTimeZoneNotFoundException e)
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);
}

0 comments on commit 140c1b4

Please sign in to comment.