简体   繁体   English

检查时间跨度

[英]Checking TimeSpan

I have a method that queries active directory, and returns the value of the Last Password Reset to a local variable.我有一个查询活动目录的方法,并将上次密码重置的值返回到局部变量。 I am trying to compare that value to the current date and time, and check if it's been less than 24 hours.我正在尝试将该值与当前日期和时间进行比较,并检查它是否小于 24 小时。 I think I'm close, but can't seem to get this to work.我想我已经接近了,但似乎无法让它发挥作用。

Thanks, Jason谢谢,杰森

string passwordLastSet = string.Empty;
passwordLastSet = DateTime.FromFileTime((Int64)(result.Properties["PwdLastSet"][0])).ToString();  
public string lastReset(DateTime pwordLastReset)
{
    if (DateTime.Now.AddHours(24) <= passwordLastSet)
    {
        return "try again later";
    }
    else
    {
        return "all is good";
    }
}

I am trying to compare that value to the current date and time, and check if it's been less than 24 hours.我正在尝试将该值与当前日期和时间进行比较,并检查它是否小于 24 小时。

This code almost writes itself.这段代码几乎是自己写的。

DateTime timeOfLastPasswordReset = // get time of last password reset
DateTime now = DateTime.Now;
TimeSpan difference = now.Subtract(timeOfLastPasswordReset);
if(difference < TimeSpan.FromHours(24)) {
    // password was reset less than twenty-four hours ago
}
else {
    // password was reset no less than twenty-four hours ago
}

Note how the code reads exactly as you specified in English.请注意代码的读取方式与您用英语指定的完全相同。

This:这个:

 if (DateTime.Now.AddHours(24) <= passwordLastSet)

should be应该是

   if (DateTime.Now <= passwordLastSet.AddHours(24))

How about (assuming I've read your intentions correctly):怎么样(假设我已经正确阅读了您的意图):

// Does passwordLastSet, with 24 hours added to it, fall before or after DateTime.Now?
// If AFTER, then reject, if BEFORE, then accept
if (passwordLastSet.Add(new TimeSpan(24, 0, 0)) > DateTime.Now)
{
    // Password was last set within the last 24 hours
    return "try again later";
}
else
{
    return "all is good";
}

尝试DateTime.Now.AddHours(-24) <= passwordLastSet因为你想模拟现在是过去 24 小时,而不是未来

You are comparing a string variable and a datetime variable, it is not possible to compare them您正在比较字符串变量和日期时间变量,无法比较它们

 DateTime passwordLastSet = DateTime.FromFileTime((Int64)(result.Properties["PwdLastSet"][0]));  
 public string lastReset(DateTime pwordLastReset)
 {
     if (DateTime.Now.AddHours(24) <= passwordLastSet)
     {
         return "try again later";
     }
     else
     {
         return "all is good";
     }
 }

Change your string to DateTime if you want to compare it with current time.如果要将字符串与当前时间进行比较,请将字符串更改为 DateTime。

Just use this就用这个

if (DateTime.Now <= passwordLastSet)
{
    return "try again later";
}
else
{
    return "all is good";
}

If you want to check it greater than 24 change both to TimeSpan and compare it.如果要检查它是否大于 24,请将两者更改为 TimeSpan 并进行比较。

if (DateTime.Now.Subtract(passwordLastSet).TotalHours < 24)
    Console.WriteLine("Try again");
else
    Console.WriteLine("all is good");

You can also use TotalDays < 1您还可以使用 TotalDays < 1

To be pedantic:学究气:

  • You need to check for special cases before converting to DateTime - for example pwdLastSet can be zero , so you should check this before attempting to convert.您需要在转换为 DateTime 之前检查特殊情况 - 例如pwdLastSet 可以为零,因此您应该在尝试转换之前检查这一点。

  • pwdLastSet is stored as UTC - so that converting to local time using DateTime.FromFileTime might return an ambiguous time . pwdLastSet存储为 UTC,因此使用DateTime.FromFileTime转换为本地时间可能会返回不明确的时间

    So it would be better to use DateTime.FromFileTimeUtc and compare with DateTime.UtcNow .所以最好使用DateTime.FromFileTimeUtc并与DateTime.UtcNow进行比较。

Depending on exactly what you want to achieve, you may also want to check the userAccountControl flags - something like the following (untested):根据您想要实现的确切目标,您可能还需要检查 userAccountControl 标志 - 类似于以下内容(未经测试):

    [Flags]
    private enum AdsUserFlags
    {
        Script = 0x1,
        AccountDisabled = 0x2,
        HomeDirectoryRequired = 0x8,
        AccountLockedOut = 0x10,
        PasswordNotRequired = 0x20,
        PasswordCannotChange = 0x40,
        EncryptedTextPasswordAllowed = 0x80,
        TempDuplicateAccount = 0x100,
        NormalAccount = 0x200,
        InterDomainTrustAccount = 0x800,
        WorkstationTrustAccount = 0x1000,
        ServerTrustAccount = 0x2000,
        PasswordDoesNotExpire = 0x10000,
        MnsLogonAccount = 0x20000,
        SmartCardRequired = 0x40000,
        TrustedForDelegation = 0x80000,
        AccountNotDelegated = 0x100000,
        UseDesKeyOnly = 0x200000,
        DontRequirePreauth = 0x400000,
        PasswordExpired = 0x800000,
        TrustedToAuthenticateForDelegation = 0x1000000,
        NoAuthDataRequired = 0x2000000
    }

    ...
    AdsUserFlags userAccountControl = (AdsUserFlags)result.Properties["userAccountControl"][0];
    long lastReset = (long)result.Properties["PwdLastSet"][0];

    if (lastReset == 0L)
    {
        if ((userAccountControl & AdsUserFlags.PasswordDoesNotExpire) == 0)
        {
            // ... user must set password at next login
        }
        else
        {
            // ... presumably password has never been reset
        }
    }
    else
    {
        DateTime lastResetUtc = DateTime.FromFileTimeUtc(lastReset);
        // ... etc - compare with DateTime.UtcNow
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM