2

I have about 100 Datetime.Now in my C# windows application .My application is installed on 30 client computers,and each computer clients have their system datetime ,I want to Override my datetime 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 datetime.now to server time

Sample get datetime from server

var dateQuery = yourDbContext.CreateQuery<DateTime>("CurrentDateTime() ");
DateTime dateFromSql = dateQuery .AsEnumerable().First();
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • Maybe help to you this link: http://stackoverflow.com/questions/6676191/how-can-get-datetime-from-internet-external-resource-not-from-server or you use webservice. and webservice getting data from service – Valeh Mikayilzadeh Aug 27 '16 at 07:59

3 Answers3

3

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;
    try
    {
      now = GetServerNow();
    }
    catch
    {
      now = DateTime.Now;
    }
  }
}
Ido Ran
  • 10,584
  • 17
  • 80
  • 143
  • The problem is i have to change all my code i mean datetime.now to static variable – Ehsan Akbar Aug 27 '16 at 07:55
  • 3
    @EhsanAkbar: "The problem is i have to change all my code i mean datetime.now to static variable" - yes, you need to change everywhere that you've currently got the wrong code. That part should be fairly easy to do with search and replace... then you can work on the dependency injection part. I'd also suggest that `UtcNow` is almost always more appropriate than `Now` anyway. – Jon Skeet Aug 27 '16 at 07:58
2

Another solution which might work for you, would be to set the default culture to a specific timezone to match your server instead of using the clients timezone.

static void Main(string[] args)
{
    TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time"));
    var foo = DateTime.Now;     
}
Joe_DM
  • 985
  • 1
  • 5
  • 12
0

If you really want to use the DateTime.Now and return your own value into it, then something like this can do the job... But it gets hard to know if the code is executing through your method or System.DateTime's method. The long term solution would be to implement your own replacement.

class Program
{
    static void Main(string[] args)
    {
        var foo = DateTime.Now;            
    }
}
public static class DateTime
{
    public static System.DateTime Now => new System.DateTime(1900, 1, 1);
}

This is also going to give you many issues accessing the real DateTime and after playing with it for just a few minutes it looks completely unworkable.

A better solution would be a separate class like this (one that I'm using until we move to an interface):

public static class SystemTime
{
    public static DateTime Now => MockedNow ?? DateTime.Now;
    public static DateTime UtcNow => MockedUtcNow ?? DateTime.UtcNow;
    public static DateTime Today => MockedToday ?? DateTime.Today;

    public static DateTime? MockedNow { get; set; }
    public static DateTime? MockedUtcNow { get; set; }
    public static DateTime? MockedToday { get; set; }

    public static void ResetMocks()
    {
        MockedNow = null;
        MockedToday = null;
        MockedUtcNow = null;
    }
}
Joe_DM
  • 985
  • 1
  • 5
  • 12