Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: CodeProject Daily News - The only newsletter I would, deliberately, subscribe to PinprotectorOriginalGriff16-Jun-14 6:28 
GeneralEnhanced Password Security PinprofessionalBernhard Hiller10-Jun-14 22:08 
JokeRe: Enhanced Password Security PinprofessionalKornfeld Eliyahu Peter10-Jun-14 23:17 
GeneralRe: Enhanced Password Security PinprofessionalEddy Vluggen11-Jun-14 1:29 
GeneralRe: Enhanced Password Security PinmemberTrajan McGill11-Jun-14 4:07 
GeneralRe: Enhanced Password Security PinmemberPIEBALDconsult11-Jun-14 13:02 
GeneralRe: Enhanced Password Security [modified] PinmemberPIEBALDconsult11-Jun-14 13:10 
GeneralDateTime.Now vs. GetSystemTime PinprofessionalBernhard Hiller30-May-14 1:17 
Working with date and time is known to be a source of beautiful code...
System.DateTime.Now gets the current system time, but you cannot set it. When you want to be able to set the system time, you can try SetSystemTime(ref SYSTEMTIME lpSystemTime) of kernel32.dll; and there is also a GetSystemTime(ref SYSTEMTIME lpSystemTime) function in that dll.
When I played with these functions, I wrote some tests to make sure I got the conversion from struct SYSTEMTIME to System.DateTime and vice versa right.
[Test]
public void GetSystemTime()
{
    DateTime expected = System.DateTime.Now;
    DateTime actual = SystemTime.GetSystemTime();
    Assert.AreEqual(expected, actual);
}
Well, that always failed: it takes a tick or two to do all the calculations.
I had to update it to allow for some delay:
        [Test]
        public void GetSystemTime()
        {
            DateTime expected = System.DateTime.Now;
            DateTime actual = SystemTime.GetSystemTime();
            int delta = Math.Abs(Convert.ToInt32((actual - expected).Ticks));
            int maxDuration = Convert.ToInt32(2 * TimeSpan.TicksPerMillisecond);
            Assert.Less(delta, maxDuration);
        }
That's 2 Milliseconds for that simple code. But still it fails rather often:
Errors and Failures:
1) Test Failure : XY.SystemTimeTests.GetSystemTime
  Expected: less than 20000
  But was:  147763
What does the computer do during those almost 15 ms? Is this a test for catching the computer asleep?
GeneralRe: DateTime.Now vs. GetSystemTime PinprotectorPete O'Hanlon30-May-14 2:05 
GeneralRe: DateTime.Now vs. GetSystemTime PinprofessionalBernhard Hiller2-Jun-14 1:30 
GeneralRe: DateTime.Now vs. GetSystemTime PinprotectorPete O'Hanlon2-Jun-14 1:32 
GeneralRe: DateTime.Now vs. GetSystemTime PinprofessionalJörgen Andersson2-Jun-14 22:11 
GeneralRe: DateTime.Now vs. GetSystemTime PinprofessionalBernhard Hiller2-Jun-14 22:35 
GeneralRe: DateTime.Now vs. GetSystemTime PinprofessionalJörgen Andersson2-Jun-14 22:56 
GeneralRe: DateTime.Now vs. GetSystemTime PinmemberBobJanova2-Jun-14 1:51 
GeneralRe: DateTime.Now vs. GetSystemTime PinprofessionalRob Philpott10-Jun-14 6:20 
GeneralRe: DateTime.Now vs. GetSystemTime PinprofessionalEddy Vluggen6-Jun-14 6:11 
GeneralSuccessfully writing to a string literal PinprofessionalIndivara19-May-14 0:44 
GeneralRe: Successfully writing to a string literal PinmemberFreak3019-May-14 2:45 
GeneralRe: Successfully writing to a string literal [modified] PinprofessionalIndivara19-May-14 6:17 
GeneralRe: Successfully writing to a string literal Pinmembernewton.saber19-May-14 2:45 
GeneralRe: Successfully writing to a string literal PinprofessionalIndivara19-May-14 6:15 
GeneralRe: Successfully writing to a string literal Pinmembernewton.saber19-May-14 6:34 
GeneralThe Exception to the C# Rules PinmemberBrady Kelly15-May-14 19:55 
GeneralRe: The Exception to the C# Rules PinprofessionalBernhard Hiller15-May-14 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 22 Dec 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid