-2

I have an audit trail where the date and time of user's time in / time out was enlisted. Is it possible that even I changed the system date and time. It ignores the system date/time that I have changed and log the real date and time of time in/ time out of the user.

This is my code.

dbAuditTrail.AddActionLog(userID, timeIn, DateTime.Now.ToString("MM/dd/yyyy - hh:mm:ss tt"));
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Jet Seighart
  • 29
  • 1
  • 6
  • 2
    Real date time of what? which country? which area? there are many factors.. – Rajshekar Reddy Oct 26 '16 at 07:21
  • 2
    Changed how? Where does the code run? And why are you storing an arbitrary string that can't be parsed as a DateTime in the database, instead of an actual `DateTime` value? – Panagiotis Kanavos Oct 26 '16 at 07:21
  • Real DateTime of gmt + 8 areas for example. – Jet Seighart Oct 26 '16 at 07:24
  • That's not what you wrote in the question. Do you want to store the altered system time or not? In any case, if you want the offset use `DateTimeOffset.Now`. And store *that* in the database, as a `datetimeoffset` type, not the string represantation – Panagiotis Kanavos Oct 26 '16 at 07:26
  • Please clarify what your question is really about. Right now it is kinda hard to understand what you're asking, in particular because you've also brought up the problem with time zones in a comment. Please write out a question with all the details. – Lasse V. Karlsen Oct 26 '16 at 07:29
  • I change system time means ?? your are changing time from Server ? or the User's Machine? – Vivek Nuna Oct 26 '16 at 07:30
  • @LasseV.Karlsen that's not a good duplicate, it doesn't have an answer at all. The link provided by Patrick Hofman is better, it contains multiple solutions that call NIST directly or through a web service. I start to suspect though that the OP is confused about UTC and local times, or doesn't know about DateTime.UtcNow – Panagiotis Kanavos Oct 26 '16 at 07:43
  • I didn't mark it as duplicate, I marked it as unclear, community decided to close it as a duplicate. I reopened it because I agree that the duplicate closure was wrong, but it wasn't my fault :) I still think the question as it stands is unclear though but now I can't cast a new vote in that regard. – Lasse V. Karlsen Oct 26 '16 at 08:21
  • Possible duplicate of [How to get DateTime from the internet?](http://stackoverflow.com/questions/6435099/how-to-get-datetime-from-the-internet) – Panagiotis Kanavos Oct 26 '16 at 08:30

1 Answers1

7

Yes, get the date and time from an external service (NTP server for example) you trust.

This post suggests to use the NIST.gov webservice.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • The OP seems to ask about the opposite - the "system time" was changed but the code doesn't store the changed time. – Panagiotis Kanavos Oct 26 '16 at 07:23
  • "log the real date and time of time in/ time out of the user" > as in 'not influenced by the user'. This is what he needs. – Patrick Hofman Oct 26 '16 at 07:24
  • 1
    @PanagiotisKanavos you seem to have an opinion on everyones answers but haven't supplied any of your own. If the user is after the real time, and the system time cannot be relied upon what do you suggest. Going to an external service to get the real time and then add or minus any time to a local time. – Simon Price Oct 26 '16 at 07:26
  • @SimonPrice Please read the question again. The way it's worded it asks why the altered time isn't stored, which doesn't make much sense. The OP's comments seem to suggest he wants the real time. If the OP doesn't understand the difference, no answer will help. As for reliable time, i'd ensure the DB server's watch synchronizes with a UTP server and use a default value in the database. That's not what the OP asked though. – Panagiotis Kanavos Oct 26 '16 at 07:28
  • then offer your own answer than be a keyboard warrior against everyone else – Simon Price Oct 26 '16 at 07:32
  • @SimonPrice to which question? Or all of them? Since all of them *have* been answered already, shouldn't I close the question instead? What do *you* understand from this question? – Panagiotis Kanavos Oct 26 '16 at 07:35
  • "log the real date and time of time in/ time out of the user" tells me that he wants the real time – Simon Price Oct 26 '16 at 07:42
  • @SimonPrice the full phrase is `It ignores the system date/time that I have changed and log the real date and time of time in/ time out of the user.` It has the opposite meaning, although it doesn't make sense – Panagiotis Kanavos Oct 26 '16 at 07:51