Ehsan Akbar Ehsan Akbar - 1 year ago 59
C# Question

Override datetime class in my application to get the server datetime

I have about 100

in my C# windows application .My application is installed on
client computers,and each computer clients have their
system datetime
,I want to Override my
to get the server time
not the client system time
.Because if datetime can be overridden then i don't have to change all my
server time

Sample get
from server

var dateQuery = yourDbContext.CreateQuery<DateTime>("CurrentDateTime() ");
DateTime dateFromSql = dateQuery .AsEnumerable().First();

Answer Source

I think a better solution is to use a different class that has Now static property and implement fetching the time from the server. The reason is it will leave you with the ability to still get the local time while also be able to get the server time.

If you are using dependency injection it usually good idea to create an IClock interface with Now property and have one impl get the actual time while another return fixed time for testing purposes. In your case you'll also have ServerClock implementation.

One last thing, communication always fail sometimes so it's good idea to fall back on DateTime.Now if getting the time from the server fails.

class RemoteDateTime
  public DateTime Now
    DateTime now;
      now = GetServerNow();
      now = DateTime.Now;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download