How to save a timestamp using NodaTime?

I have the following implementation using basic UtcNow, and then converting it back to the UI. The purpose in this scenario is very simple. When I insert a new client to the database, I convert it to UtcNow, so I have a timestamp of this record. When I show it back to the user, I convert it to Local (eg: You are member since "dd/MM/yyyy").

Here is my implementation. I would like to know how do I get this done through NodaTime. And also, if anyone has a better approach for doing it, I'll be glad to hear!

public interface IDateTime
{
    DateTime ToUtcNow { get; }
    DateTime FromUtc(DateTime dateTimeToConvert);
}

public class DateTimeProvider : IDateTime
{
    public DateTime ToUtcNow
    {
        get { return DateTime.UtcNow; }
    }

    public DateTime FromUtc(DateTime dateTimeToConvert)
    {
        return dateTimeToConvert.ToLocalTime();
    }
}

Also, how should I save it to database, in this case Sql Server. What data type should I use? DateTime, DateTimeOffset? What about in code? OffsetDateTime, ZonedDateTime I am very confused about that. I have read the userguide, but it shows more complex scenarios that I'am not getting to implement it in such a simple scenario.

Thanks in advance!

Jon Skeet
people
quotationmark

It sounds like you're trying to store points in time, regardless of calendar system or time zone. Those are represented by Instant values in Noda Time. So within most of your code, that's probably what you should use to represent the timestamp. (That's also what IClock.Now will give you.)

However, your database is unlikely to know about Noda Time... I would suggest that you should probably store a DateTimeOffset, which you can obtain via Instant.ToDateTimeOffset (and there's Instant.FromDateTimeOffset too). That will always have an offset of 0, but at least then it's not ambiguous about which point in time it represents.

people

See more on this question at Stackoverflow