DateTimeOffset values should keep the 'offset' property in database when stored in a timestamptz column. Doc link:
https://www.postgresql.org/docs/9.2/static/datatype-datetime.html
timestamp [ (p) ] with time zone | 8 bytes | both date and time, with time zone
DateTimeOffset are converted to utc when stored in a timestamptz column, thus definitly loosing the time offset (timezone), which is needed for example to compare worlwide the time of day a user is doing something.
migrationBuilder.CreateTable(
name: "CrashAlert",
columns: table => new
{
CrashAlertId = table.Column<int>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn),
DateCreated = table.Column<DateTimeOffset>(nullable: false),
DateReceived = table.Column<DateTimeOffset>(nullable: false),
},
constraints: table =>
{
table.PrimaryKey("PK_CrashAlert", x => x.CrashAlertId);
});
[AllowAnonymous]
[HttpGet("api/testdb")]
public string TestDb([FromServices]ApiDatabase db)
{
var now = DateTimeOffset.Now; //My local time offset is +1
var crash = new DbCrashAlert
{
DateCreated = now, //Should be stored as 11:00+1, but instead is stored as 10:00+0
DateReceived = now.ToUniversalTime(), //Is correctly stored as 10:00+0
LastGeoPositonId = 1,
TripId = 11
};
db.Add(crash);
db.SaveChanges();
return "OK";
}
Unfortunately this is exactly how "timestamp with time zone" in postgres works. I know the documentation is confusing, but the database only stores a utc timestamp without a time zone indicator. Then when it's converted to text, the time is converted to the server's local time and that timestamp and zone is printed.
Ok so this is my first and last experience with postgre. Not a good db.