Skip to content

Commit 98a2089

Browse files
Fix Datetime localization issue when handling serverdate in PostgreSQL (#1158)
* Added utility method NormalizeDbmsDateTime to normalize DateTime objects in postgresql by setting their Kind to Unspecified for consistent handling of time values * Add method ResetTimeAndKind to reset the Kind property of DateTime when it is Local. This handles cases like Npgsql, which returns DateTime values with Kind=Local, causing comparisons with Kind=Unspecified to fail. ServerNow always returns the DateTime in the local timezone of the DBMS.
1 parent 8ee1572 commit 98a2089

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3277,7 +3277,16 @@ public static DateTime ServerDate(IGxContext context, IDataStoreProvider dataSto
32773277
{
32783278
if (dataStore == null)
32793279
return ServerDate(context, new DataStoreHelperBase().getDataStoreName());
3280-
return ResetTime(dataStore.serverNow());
3280+
return ResetTimeAndKind(dataStore.serverNow());
3281+
}
3282+
3283+
private static DateTime ResetTimeAndKind(DateTime dateTime)
3284+
{
3285+
if (dateTime.Kind != DateTimeKind.Unspecified)
3286+
{
3287+
return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0);
3288+
}
3289+
else return ResetTime(dateTime);
32813290
}
32823291
#if !NETCORE
32833292
[Obsolete("ServerTime with string dataSource is deprecated, use ServerTime(IGxContext context, Data.NTier.IDataStoreProvider dataStore) instead", false)]

dotnet/src/dotnetframework/GxClasses/Data/GXDataCommon.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,11 @@ public virtual string AfterCreateCommand(string stmt, GxParameterCollection parm
11691169
{
11701170
return stmt;
11711171
}
1172+
1173+
internal virtual DateTime NormalizeDbmsDateTime(DateTime d)
1174+
{
1175+
return d;
1176+
}
11721177
}
11731178

11741179
public class DbDataAdapterElem

dotnet/src/dotnetframework/GxClasses/Data/GXDataNTier.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,11 @@ public DateTime serverNow()
752752
public DateTime serverNowIn(bool hasMilliseconds )
753753
{
754754
string stmt = "";
755+
GxDataRecord gxDataRecord = (GxDataRecord)_ds.Db;
755756
if (hasMilliseconds)
756-
stmt = ((GxDataRecord)_ds.Db).GetServerDateTimeStmtMs(_ds.Connection);
757+
stmt = gxDataRecord.GetServerDateTimeStmtMs(_ds.Connection);
757758
else
758-
stmt = ((GxDataRecord)_ds.Db).GetServerDateTimeStmt(_ds.Connection);
759+
stmt = gxDataRecord.GetServerDateTimeStmt(_ds.Connection);
759760

760761
if (string.IsNullOrEmpty(stmt))
761762
{

dotnet/src/dotnetframework/GxClasses/Data/GXDataPostgreSQL.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,14 @@ public override string ConcatOp(int pos)
630630
{
631631
return ConcatOpValues[pos];
632632
}
633+
internal override DateTime NormalizeDbmsDateTime(DateTime d)
634+
{
635+
if (d.Kind != DateTimeKind.Unspecified)
636+
{
637+
d = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second, d.Millisecond);
638+
}
639+
return d;
640+
}
633641
}
634642
sealed internal class PostgresqlConnectionWrapper : GxAbstractConnectionWrapper
635643
{

0 commit comments

Comments
 (0)