Nitin Chhajer
Nitin Chhajer

Reputation: 2339

Is it possible for my application to show a time irrespective of if systime has been modified

Also I would like to ask if my app is not running then can I create a daemon process to keep a check on this(assuming that this process runs all the time).

The situation is something like this: The user changes the sys date from 1 January 2012 to 1 January 2013, but the application should still use 1 January 2012.

I am using a Windows operating system.

Upvotes: 1

Views: 159

Answers (3)

domskey
domskey

Reputation: 1112

You could keep track of the time internally with a static class, like the internal date time class below. At the start of your program, initialise it with:

DateTime now = InternalDateTime.Now;

You can now change the system time and have both times at hand, could also be used for measuring local clock drift, if you sync with a time server and that's your thing.

MessageBox.Show(
    DateTime.Now.ToString()+Environment.NewLine+
    InternalDateTime.Now.ToString());

And access it the same way instead of using DateTime.Now, use InternalDateTime.Now It will hold onto the time even whe a system time change occurs.

public class InternalDateTime
{
    private static volatile InternalDateTime _instance;
    private static object syncRoot = new Object();
    //threadsafe singleton
    public static InternalDateTime Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (syncRoot)
                {
                    if (_instance == null)
                        _instance = new InternalDateTime();
                }
            }

            return _instance;
        }
    }

    private Stopwatch sw;
    private DateTime dtAppStart;

    private InternalDateTime()
    {
        dtAppStart = DateTime.Now;
        sw = new Stopwatch();
        sw.Start();
    }
    public static DateTime Now { get { return InternalDateTime.Instance.DateTimeNow; } }

    public DateTime DateTimeNow
    {
        get
        {
             return new DateTime(dtAppStart.Ticks).AddMilliseconds(sw.ElapsedMilliseconds);

        }
    }
}

Upvotes: 0

aleroot
aleroot

Reputation: 72666

There is no reliable way to keep track of time without rely on system time or from an external source(web service or NTP), since the date could be tampered by the user in several different way.

Your assumed option to keep track of the time with a background service can't prevent a time change during a reboot(e.g. from BIOS), so what is the purpose to keep internal time ? It is related to a copy protection system (like trial time limit)?

If so you can take a look at a library like CryptoLicensing that claim to have a Detects Date Tampering algoritm and can be useful for you.

However i think that the way should be try to detect Date/Time tempering rather than trying to keep track of time by yourself...

Upvotes: 2

Tigran
Tigran

Reputation: 62286

There is no way acheive what you're asking about, nor that at least I'm aware of.

I just suppose this is a software trial licensing issue, if so, there are several workarrounds, like

  • as it was suggested check the real date time from remote source, some secure web service.

  • limit trial version not on time, but on execution count, or save count. Store that values somewhere in registry or user temp folder within encrypted file.

Hope this helps.

Upvotes: 1

Related Questions