The best date store & calculation

  • Thread starter Thread starter Praveen
  • Start date Start date
P

Praveen

DateTime.UtcNow is added as root attribute, 'Modified' of an xml whenever
xml is changed.
Every 5 secs a check is made whether xml is changed or not, by comparing
previous 'Modified' and current 'Modified'.

By considering performance in mind whats best to way of stroring and
calculating date in this case?

Is UtcNow best for comparing every 5 secs.?
Or is it better saving UtcNow.ticks and comparing it.?

How to get UtcNow.ticks in seconds/milliseconds?
UtcNow.ticks gives ticks from Jan 01, 0001. How to get it from another date,
say How to get ticks/milliseconds since jan 01, 1970 (as in case of
Javascript)?


thanks,
Praveen
 
From the computer's perspective, "every 5 seconds" is not very often,
so I would keep it simple and stick with the natural format, DateTime.
Less to go wrong.

For duration between 2 times, use Subtract, which gives you a
TimeSpan. Or simply subtact pre-caculate the ticks of your origin
date, and subtract this from the ticks for now - i.e. (notepad code -
not tested)
long origin = new DateTime(1970,1,1).Ticks; // note this is in local
time, not UTC
....
long ticks = DateTime.Now.Ticks - origin; // so use local time here
also?

Can I ask what the scenario is here? What could change the xml? It is
a file? Something in memory? There may be other ways to monitor for
changes. Size of the xml, and the source (remote database vs in local
memory) makes a difference too.

Marc
 
Marc Gravell said:
so I would keep it simple and stick with the natural format, DateTime.
Less to go wrong.

For duration between 2 times, use Subtract, which gives you a
TimeSpan. Or simply subtact pre-caculate the ticks of your origin
date, and subtract this from the ticks for now - i.e. (notepad code -
not tested)
long origin = new DateTime(1970,1,1).Ticks; // note this is in local
time, not UTC
...
long ticks = DateTime.Now.Ticks - origin; // so use local time here
also?

Can I ask what the scenario is here? What could change the xml? It is
a file? Something in memory? There may be other ways to monitor for
changes. Size of the xml, and the source (remote database vs in local
memory) makes a difference too.

Its a web application running in localhost. xml is initially stored in disk
and same xmldocument cached in hashtable on first access.
Updates can happen from some other window which updates the xmldocument in
hashtable.
An xmlhttp request is made to check xml updated or not to refresh the UI.
 
OK - more or less what I suspected; just watch out for thread safety
when accessing the various objects. Web apps are multi-threaded.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top