/// <summary> /// Gets a DateTime by combining the two database fields used to store /// a database date and database time. /// </summary> /// <param name="reader">The reader.</param> /// <param name="dateFieldIndex">Index of the date field.</param> /// <param name="timeFieldIndex">Name of the time field.</param> /// <param name="defaultValueIfNull">The default value if null.</param> /// <returns></returns> public static DateTime GetSafe(this IDataReader reader, int dateFieldIndex, int timeFieldIndex, DateTime defaultValueIfNull) { // the database (should) always store a date/time in UTC. So the default should be of the same Kind: var utcDefault = defaultValueIfNull.ToUniversalTime(); var dbDate = Db.Parse(reader, dateFieldIndex, (DbDate)utcDefault); var dbTime = Db.Parse(reader, timeFieldIndex, (DbTime)utcDefault); // re-combine the read field values: return(DbDateTime.ToDateTime(dbDate, dbTime)); }
public void ToDateTime() { var utcDt = DateTime.MinValue.ToUniversalTime(); Assert.AreEqual(utcDt, DbDateTime.ToDateTime(DbDate.MinValue, DbTime.MinValue)); utcDt = new DateTime(2013, 2, 20, 13, 24, 56, 0, DateTimeKind.Utc); Assert.AreEqual(utcDt, DbDateTime.ToDateTime(new DbDate(2013, 2, 20), new DbTime(13, 24, 56, 0, DateTimeKind.Utc))); // using DateTime.Now here get non-Equal results (internal datedata are different, but the ticks and kind are equal) var localDt = new DateTime(2013, 2, 20, 14, 24, 56, 0, DateTimeKind.Local); // provide a local datetime var res = DbDateTime.ToDateTime( new DbDate(localDt.Year, localDt.Month, localDt.Day), new DbTime(localDt.Hour, localDt.Minute, localDt.Second, localDt.Millisecond, localDt.Kind)); Assert.AreEqual(localDt, res); Assert.AreEqual(localDt, DbDateTime.ToDateTime((DbDate)localDt, (DbTime)localDt)); }