I have some code which detects when properties are changed on objects, which are persisted to the db. The detection happens before they are saved back to the db.
I want to know the best way to apply the change in time to a whole set of related log items.
Say I have an item with a datetime of
01/01/2000 00:00:00
and I change it to
02/01/2000 06:00:00
I know I need to move each log item forward by 1 day and 6 hours. Should I be manually writing code to handle all this, or does .net have some smooth way of handling this for me. DateTimeOffset sounded like the right idea, but I don’t think it is used for this.
If I was to build this myself, I would probably calculate a timespan, and if the span should be added or subtracted from the DateTime. But is there something better?
TimeSpan will work for you, and yes, it does keep track of positive and negative. The Duration() method on the TimeSpan will return a new TimeSpan that has the absolute value of the original value (i.e. the positive change).
That being said, with the following you won’t have to worry about positive or negative changes: