本文整理汇总了C#中DateTime.Add方法的典型用法代码示例。如果您正苦于以下问题:C# DateTime.Add方法的具体用法?C# DateTime.Add怎么用?C# DateTime.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTime
的用法示例。
在下文中一共展示了DateTime.Add方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FrequencyParameter_NoArgPassed_ThruDate
public void FrequencyParameter_NoArgPassed_ThruDate()
{
FrequencyParameter testParameter = new FrequencyParameter();
DateTime expectedResult = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
expectedResult = expectedResult.Add(new TimeSpan(1, 0, 0, 0));
Assert.AreEqual(expectedResult, testParameter.ThruDate);
}
示例2: FrequencyParameter_NoArgPassed_ReportingPeriod
public void FrequencyParameter_NoArgPassed_ReportingPeriod()
{
FrequencyParameter testParameter = new FrequencyParameter();
DateTime fromResult = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
DateTime thruResult = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
thruResult = thruResult.Add(new TimeSpan(1, 0, 0, 0));
TimeSpan expectedResult = thruResult.Subtract(fromResult);
Assert.AreEqual(expectedResult, testParameter.ReportingPeriod);
}
示例3: RolePrincipal
public RolePrincipal (IIdentity identity)
{
if (identity == null)
throw new ArgumentNullException ("identity");
this._identity = identity;
this._cookiePath = RoleManagerConfig.CookiePath;
this._issueDate = DateTime.Now;
this._expireDate = _issueDate.Add (RoleManagerConfig.CookieTimeout);
}
示例4: Default
public static DateTimeOffset Default(DateTime dt, TimeZoneInfo timeZone)
{
if (dt.Kind != DateTimeKind.Unspecified)
{
var dto = new DateTimeOffset(dt);
return TimeZoneInfo.ConvertTime(dto, timeZone);
}
if (timeZone.IsAmbiguousTime(dt))
{
var earlierOffset = timeZone.GetUtcOffset(dt.AddDays(-1));
return new DateTimeOffset(dt, earlierOffset);
}
if (timeZone.IsInvalidTime(dt))
{
var earlierOffset = timeZone.GetUtcOffset(dt.AddDays(-1));
var laterOffset = timeZone.GetUtcOffset(dt.AddDays(1));
var transitionGap = laterOffset - earlierOffset;
return new DateTimeOffset(dt.Add(transitionGap), laterOffset);
}
return new DateTimeOffset(dt, timeZone.GetUtcOffset(dt));
}
示例5: OnIncrement
/// <summary>
/// Performs addition of a date time and a time span in a global context.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="timeSpan">The time span.</param>
/// <returns>The DateTime after incrementing by TimeSpan.</returns>
public virtual DateTime OnIncrement(DateTime value, TimeSpan timeSpan)
{
// special case: value is at DateTime.MaxValue
if (DateTime.MaxValue.Date == value.Date && value.TimeOfDay.Add(timeSpan) > TimeSpan.FromDays(1))
{
return value.AddDays(-1).Add(timeSpan);
}
return value.Add(timeSpan);
}
示例6: DecryptTicket
void DecryptTicket (string encryptedTicket)
{
if (encryptedTicket == null || encryptedTicket == String.Empty)
throw new ArgumentException ("Invalid encrypted ticket", "encryptedTicket");
byte [] ticketBytes = GetBytesFromBase64 (encryptedTicket);
byte [] decryptedTicketBytes = null;
CookieProtection cookieProtection = RoleManagerConfig.CookieProtection;
if (cookieProtection == CookieProtection.All) {
decryptedTicketBytes = MachineKeySectionUtils.VerifyDecrypt (MachineConfig, ticketBytes);
} else if (cookieProtection == CookieProtection.Encryption) {
decryptedTicketBytes = MachineKeySectionUtils.Decrypt (MachineConfig, ticketBytes);
} else if (cookieProtection == CookieProtection.Validation) {
decryptedTicketBytes = MachineKeySectionUtils.Verify (MachineConfig, ticketBytes);
}
if (decryptedTicketBytes == null)
throw new HttpException ("ticket validation failed");
MemoryStream ticket = new MemoryStream (decryptedTicketBytes);
BinaryReader reader = new BinaryReader (ticket);
// version
_version = reader.ReadInt32 ();
// issued date
_issueDate = new DateTime (reader.ReadInt64 ());
// expire date
_expireDate = new DateTime (reader.ReadInt64 ());
// cookie path
_cookiePath = reader.ReadString ();
// roles
string roles = reader.ReadString ();
if (!Expired) {
InitializeRoles (roles);
//update ticket if less than half of CookieTimeout remaining.
if (Roles.CookieSlidingExpiration){
if (_expireDate-DateTime.Now < TimeSpan.FromTicks (RoleManagerConfig.CookieTimeout.Ticks/2)) {
_issueDate = DateTime.Now;
_expireDate = DateTime.Now.Add (RoleManagerConfig.CookieTimeout);
SetDirty ();
}
}
} else {
// issue a new ticket
_issueDate = DateTime.Now;
_expireDate = _issueDate.Add (RoleManagerConfig.CookieTimeout);
}
}
示例7: DecryptTicket
void DecryptTicket (string encryptedTicket)
{
if (encryptedTicket == null || encryptedTicket == String.Empty)
throw new ArgumentException ("Invalid encrypted ticket", "encryptedTicket");
byte [] ticketBytes = GetBytesFromBase64 (encryptedTicket);
byte [] decryptedTicketBytes = null;
CookieProtection cookieProtection = RoleManagerConfig.CookieProtection;
if (cookieProtection == CookieProtection.All || cookieProtection == CookieProtection.Encryption) {
ICryptoTransform decryptor;
decryptor = TripleDES.Create ().CreateDecryptor (
MachineKeySectionUtils.DecryptionKey192Bits (MachineConfig),
InitVector);
decryptedTicketBytes = decryptor.TransformFinalBlock (ticketBytes, 0, ticketBytes.Length);
}
else
decryptedTicketBytes = ticketBytes;
if (cookieProtection == CookieProtection.All || cookieProtection == CookieProtection.Validation) {
byte [] validationBytes = MachineKeySectionUtils.ValidationKeyBytes (MachineConfig);
byte [] rolesWithValidationBytes = null;
byte [] tmpValidation = null;
int hashSize = (MachineConfig.Validation == MachineKeyValidation.MD5) ? 16 : 20; //md5 is 16 bytes, sha1 is 20 bytes
rolesWithValidationBytes = new byte [decryptedTicketBytes.Length - hashSize + validationBytes.Length];
Buffer.BlockCopy (decryptedTicketBytes, 0, rolesWithValidationBytes, 0, decryptedTicketBytes.Length - hashSize);
Buffer.BlockCopy (validationBytes, 0, rolesWithValidationBytes, decryptedTicketBytes.Length - hashSize, validationBytes.Length);
switch (MachineConfig.Validation) {
case MachineKeyValidation.MD5:
tmpValidation = MD5.Create ().ComputeHash (rolesWithValidationBytes);
break;
case MachineKeyValidation.TripleDES:
case MachineKeyValidation.SHA1:
tmpValidation = SHA1.Create ().ComputeHash (rolesWithValidationBytes);
break;
}
for (int i = 0; i < tmpValidation.Length; i++) {
if (i >= decryptedTicketBytes.Length ||
tmpValidation [i] != decryptedTicketBytes [i + decryptedTicketBytes.Length - hashSize])
throw new HttpException ("ticket validation failed");
}
}
MemoryStream ticket = new MemoryStream (decryptedTicketBytes);
BinaryReader reader = new BinaryReader (ticket);
// version
_version = reader.ReadInt32 ();
// issued date
_issueDate = new DateTime (reader.ReadInt64 ());
// expire date
_expireDate = new DateTime (reader.ReadInt64 ());
// cookie path
_cookiePath = reader.ReadString ();
// roles
string roles = reader.ReadString ();
if (!Expired) {
InitializeRoles (roles);
//update ticket if less than half of CookieTimeout remaining.
if (Roles.CookieSlidingExpiration){
if (_expireDate-DateTime.Now < TimeSpan.FromTicks (RoleManagerConfig.CookieTimeout.Ticks/2)) {
_issueDate = DateTime.Now;
_expireDate = DateTime.Now.Add (RoleManagerConfig.CookieTimeout);
SetDirty ();
}
}
} else {
// issue a new ticket
_issueDate = DateTime.Now;
_expireDate = _issueDate.Add (RoleManagerConfig.CookieTimeout);
}
}
示例8: FrequencyParameter_NullPassed
public void FrequencyParameter_NullPassed()
{
DateTime testFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
DateTime testThru = testFrom.Add(new TimeSpan(1, 0, 0, 0));
FrequencyParameter testParameter = new FrequencyParameter(null);
Assert.Greater(testParameter.Errors.Count, 0);
Assert.AreEqual(testFrom, testParameter.FromDate);
Assert.AreEqual(testThru, testParameter.ThruDate);
}