0

I make struct name DateTimeZone , I set it to take the UTC time and I get it to take local time.

    DateTimeZone time = DateTime.Now;//time will equal the UTC time
    DateTime localTime= time; // local time will equal the Local time

the struct

  public struct DateTimeZone
{
    private DateTime dateTime;

 //   public  DateTimeZone Value { get; }

    public static implicit operator DateTimeZone(DateTime value)
    {
        return new DateTimeZone() { dateTime = value.ToUniversalTime() };
    }

    public static implicit operator DateTime(DateTimeZone value)
    {
        return value.dateTime.ToLocalTime();
    }
}

My question : is there easiest way to implement this than struct? and this struct have exception when i save in DB cause entity frame work, so I need to make mapping every time I use struct , How I can make mapping in concise manner?

Duha
  • 811
  • 1
  • 12
  • 26
  • 2
    `DateTime` is timezone _awareness_. It is _not_ clear what you try to do, honestly, but it would be better to use [Nodatime](http://nodatime.org/) which is a better API for DateTime. – Soner Gönül Nov 03 '16 at 14:40
  • thank you, I have problem with time zone , so i need datetime to save utc time in db always & to get local time when show it UI, I modify the question – Duha Nov 03 '16 at 14:46

2 Answers2

1

Maybe you should try this one:

public struct DateTimeZone
{
    public DateTime DateTime;

    public static explicit operator DateTimeZone(DateTime dt)
    {
        return new DateTimeZone { DateTime = dt.ToUniversalTime() };
    }
}

var time = (DateTimeZone)DateTime.Now;
var localTime = time.DateTime; 
1

A few things:

  • Be careful with your naming. An object with a name like DateTimeZone would be expected to contain either 1) time zone information only, or 2) a date and time and time zone. Your object is only an encapsulation wrapper around DateTime, so it is neither of these things.

  • Implicit operations can be evil - especially if they change the meaning of the value you're working with. I don't recommend using them with date/time, unless you really know what you're doing. It will quickly become confusing to any user of this object as to what value you were actually working with.

  • The ToUniversalTime and ToLocalTime functions change their behavior based on the DateTimeKind assigned to the .Kind property of the DateTime objects you're working with. You seem to be creating an API in which DateTime is always local and DateTimeZone is always UTC, but DateTimeKind will get in the way of this idea.

  • As mentioned in comments, you might look into using Noda Time, which is a very solid and well-thought-out API. In Noda Time, the Instant type always represents UTC, and the LocalDateTime type always represents a timezoneless date and time. Time zones are represented by DateTimeZone (see the conflict with your name), and the ZonedDateTime type combines these, such that you have both instant-in-time information, local-time information, and the associated time zone.

  • You mentioned Entity Framework. Unfortunately, EF will not work directly with either your custom object, or with Noda Time. It does not have the ability to do simple type conversions. This has been requested, but not yet implemented. You can follow the work item for it here. A workaround you can use is "buddy properties", as described here. They're not fun, but they work. Mostly.

  • You may find it reasonable to just use DateTime, and call methods like ToUniversalTime or ToLocalTime manually, when needed. If you want EF to properly set DateTimeKind when loading from the database, see this answer.

  • Keep in mind that both ToUniversalTime and ToLocalTime work with the computer's local time zone where the code happens to be running. This works fine for desktop and mobile applications, but is rarely desired for web applications, because changing the time zone of the server could drastically affect the data. Consider instead working with a named time zone via the built-in TimeZoneInfo class, or with the DateTimeZone class in Noda Time.

Additional reading for you:

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575