using System; using NHibernate; using NHibernate.Type; class UtcDateTimeInterceptor : IInterceptor { public bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types) { ConvertDatabaseDateTimeToUtc(state, types); return true; } public bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types) { ConvertLocalDateToUtc(state, types); return true; } public bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types) { ConvertLocalDateToUtc(currentState, types); return true; } private void ConvertLocalDateToUtc(object[] state, IType[] types) { int index = 0; foreach (IType type in types) { if ((type.ReturnedClass == typeof(DateTime)) && state[index] != null && (((DateTime)state[index]).Kind == DateTimeKind.Local)) { state[index] = ((DateTime)state[index]).ToUniversalTime(); } ++index; } } private void ConvertDatabaseDateTimeToUtc(object[] state, IType[] types) { int index = 0; foreach (IType type in types) { if ((type.ReturnedClass == typeof(DateTime)) && state[index] != null && (((DateTime)state[index]).Kind != DateTimeKind.Utc)) { //Create a new date and assume the value stored in the database is Utc DateTime cur = (DateTime)state[index]; DateTime result = DateTime.SpecifyKind(cur, DateTimeKind.Utc); state[index] = result; } ++index; } } public void OnDelete(object entity, object id, object[] state, string[] propertyNames, IType[] types) { } public int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types) { return null; } public object Instantiate(Type type, object id) { return null; } public object IsUnsaved(object entity) { return null; } public void PreFlush(System.Collections.ICollection entities) { } public void PostFlush(System.Collections.ICollection entities) { } }