I am trying to handle DateTimeOffset? (Nullable) conversion from UTC to local in a custom class that is subclassing BusinessBase<T>.
public
abstract class CustomBusinessBase<T> : Csla.BusinessBase<T> where T : CustomBusinessBase<T>I would like to have a pair of methods that convert DateTimeOffset to and from local time.
protected
TimeSpan ToUniversalTime()protected
TimeSpan ToLocalTime();Each of these methods needs to loop through all properties that are of type DateTimeOffset?
Dates in the database are UTC. On the portal fetch the properties are loaded up with dates that are UTC (offset 0)
DateTimeOffset? dtoTemp;dtoTemp = drSafe.GetDateTimeNullable(Index);
if (dtoTemp != null){
dtoTemp =
new DateTimeOffset(dtoTemp.Value.Ticks, new TimeSpan(0));}
LoadProperty<
DateTimeOffset?>(DateProperty, dtoTemp);In the factory methods I would like to call ToLocalTime() to convert the datetime to the local offset (which would be done locally on a smart client)
So the ToLocalTime would need to get/loop a list of properties of type DateTimeOffset? and adjust the offset. I have code generated the ToLocalTime and put it into my business methods and a sample of the current code is show below. However, I think I should be able to move this in to a class that subclasses BusinessBase<T> and make ToLocalTime dynamic by getting a list of all managed properties and if the property's type is DateTimeOffset the adjust to Local.
protected override TimeSpan ToLocalTime()
{
TimeSpan offset = new TimeSpan(0);
this.ValidationRules.SuppressRuleChecking = true;
DateTimeOffset? dtoTempDatetime = ReadProperty<DateTimeOffset?>(DatetimeProperty);
if (dtoTempDatetime != null)
{
dtoTempDatetime = dtoTempDatetime.Value.ToLocalTime();
//Must Clear current value because dates shifted to different offsets (timezones)
//compare equal and CSLA will not update fields whos new value is equal to current value.
//ex. "5/10/2007 10:33:52 AM -05:00" == "5/10/2007 3:33:52 PM +00:00"
LoadProperty<DateTimeOffset?>(DatetimeProperty, null);
LoadProperty<DateTimeOffset?>(DatetimeProperty, dtoTempDatetime);
offset = dtoTempDatetime.Value.Offset;
}
this.ValidationRules.SuppressRuleChecking = false;
return offset;
}
Private static PropertyInfo<DateTimeOffset?> DatetimeProperty = new PropertyInfo<DateTimeOffset?>("Datetime");
public string Datetime
{
get { return GetProperty<DateTimeOffset?>(DatetimeProperty ); }
set { SetProperty<DateTimeOffset?>(DatetimeProperty , value); }
}
Ok, I think I found a way to do this. Does anyone see a problem with the use of FieldManager.GetRegisteredProperties(); in the method below?
protected TimeSpan ToLocalTime()
{
TimeSpan offset = new TimeSpan(0);
this.ValidationRules.SuppressRuleChecking = true;
//Get list of all Managed properties
List<Csla.Core.IPropertyInfo> l = FieldManager.GetRegisteredProperties();
if (l != null)
{
foreach (Csla.Core.IPropertyInfo item in l)
{
Csla.PropertyInfo<DateTimeOffset?> propInfoNull;
Csla.PropertyInfo<DateTimeOffset> propInfo;
propInfoNull = item as Csla.PropertyInfo<DateTimeOffset?>;
propInfo = item as Csla.PropertyInfo<DateTimeOffset>;
//If the PropertyInfo type is a DateTimeOffset? (nullable)
if (propInfoNull != null)
{
if (FieldManager.FieldExists(item))
{
DateTimeOffset? dtoNullable = null;
dtoNullable = ReadProperty<DateTimeOffset?>(propInfoNull);
if (dtoNullable.HasValue)
{
dtoNullable = dtoNullable.Value.ToLocalTime();
//Must Clear current value because dates shifted to different timezones
//compare equal and CSLA will not update fields whos new value is equal to current value.
//ex. "5/10/2007 10:33:52 AM -05:00" == "5/10/2007 3:33:52 PM +00:00"
LoadProperty<DateTimeOffset?>(propInfoNull, null);
LoadProperty<DateTimeOffset?>(propInfoNull, dtoNullable);
offset = dtoNullable.Value.Offset;
}
}
}
//If the PropertyInfo type is a DateTimeOffset
if (propInfo != null)
{
if (FieldManager.FieldExists(item))
{
DateTimeOffset dto;
dto = ReadProperty<DateTimeOffset>(propInfo);
dto = dto.ToLocalTime();
//Must Clear current value because dates shifted to different timezones
//compare equal and CSLA will not update fields whos new value is equal to current value.
//ex. "5/10/2007 10:33:52 AM -05:00" == "5/10/2007 3:33:52 PM +00:00"
LoadProperty<DateTimeOffset>(propInfo, new DateTimeOffset(1970, 1, 1, 0, 0, 0, 1, new TimeSpan(0)));
LoadProperty<DateTimeOffset>(propInfo, dto);
offset = dto.Offset;
}
}
}
}
ValidationRules.SuppressRuleChecking = false;
return offset;
}
Copyright (c) Marimer LLC