Skip to content

Instantly share code, notes, and snippets.

@aklefdal
Created June 10, 2015 08:02
Show Gist options
  • Save aklefdal/f4114cfbb37c32cbdf93 to your computer and use it in GitHub Desktop.
Save aklefdal/f4114cfbb37c32cbdf93 to your computer and use it in GitHub Desktop.
Døde personer
private bool IsPersonDead(DateTime? dodstidspunkt)
{
if (dodstidspunkt == null)
return false;
DateTime deathDate = (DateTime)dodstidspunkt;
int compareResult = DateTime.Compare(DateTime.Now, deathDate);
if (compareResult > 0)
return true;
if (compareResult <= 0)
return false;
return false;
}
@torkildr
Copy link

private bool IsPersonDead(DateTime? dodstidspunkt)
{
    if (dodstidspunkt == null)
        return false;

    return dodstidspunkt.Value < DateTime.Now;
}

@akselqviller
Copy link

private bool IsPersonDead(DateTime? dodstidspunkt)
{
    return dodstidspunkt != null;
}

@torkildr
Copy link

Ser i hvertfall ut til at det gir samme resultat

void Main()
{
    Test(null);
    Test(DateTime.Now);
    Test(DateTime.Now.AddDays(1));
    Test(DateTime.Now.AddDays(-1));
}

void Test(DateTime? date) {
    Console.WriteLine("Old: " + OldIsPersonDead(date));
    Console.WriteLine("New: " + IsPersonDead(date));
    Console.WriteLine();
}

@aklefdal
Copy link
Author

private bool IsPersonDead(DateTime? dodstidspunkt)
{
    return dodstidspunkt.HasValue;
}

@torkildr
Copy link

@aklefdal: Den vil gi annen oppførsel, men det kan jo fort være det der man egentlig ønsker å skrive, ja.

@bjartwolf
Copy link

open NodaTime
type PersonVitalStatus = Alive | DeadSince of Instant

@akselqviller
Copy link

private float ProbabilityOfPersonDead(DateTime? dodstidspunkt)
{
     if (!dodstidspunkt.HasValue)
          return 0.0f;
     else if (dodstidspunkt.Value < DateTime.Now)
          return 1.0f;
     else
          return float.NaN;
}

@bjartwolf
Copy link

Ser ingen problemer med å bruke DateTime.Now heller. Det er litt morsomt at en person kan være levende og død i forskjellige tidssoner, litt avhengig av tidssonen du sammenligner i.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment