本文整理汇总了C#中DateInterval类的典型用法代码示例。如果您正苦于以下问题:C# DateInterval类的具体用法?C# DateInterval怎么用?C# DateInterval使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DateInterval类属于命名空间,在下文中一共展示了DateInterval类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DateAdd
public static DateTime DateAdd(DateInterval Interval, double Number, DateTime DateValue)
{
int years = (int) Math.Round(Conversion.Fix(Number));
switch (Interval)
{
case DateInterval.Year:
return CurrentCalendar.AddYears(DateValue, years);
case DateInterval.Quarter:
return DateValue.AddMonths(years * 3);
case DateInterval.Month:
return CurrentCalendar.AddMonths(DateValue, years);
case DateInterval.DayOfYear:
case DateInterval.Day:
case DateInterval.Weekday:
return DateValue.AddDays((double) years);
case DateInterval.WeekOfYear:
return DateValue.AddDays(years * 7.0);
case DateInterval.Hour:
return DateValue.AddHours((double) years);
case DateInterval.Minute:
return DateValue.AddMinutes((double) years);
case DateInterval.Second:
return DateValue.AddSeconds((double) years);
}
throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", new string[] { "Interval" }));
}
示例2: CompareTo_DateIntervalWithSameStartAndSameEndDates_ReturnsZero
public void CompareTo_DateIntervalWithSameStartAndSameEndDates_ReturnsZero()
{
// Arrange
var dateInterval1 = new DateInterval(now, inOneHour);
var dateInterval2 = new DateInterval(now, inOneHour);
// Act
int compareToValue = dateInterval1.CompareTo(dateInterval2); // Shortest duration comes first
// Assert
Assert.True(compareToValue == 0);
}
示例3: Collapse
public static IList<ITask> Collapse(this IEnumerable<ITask> taskCollection)
{
var taskList = taskCollection.ToList();
if (taskCollection == null)
{
return null;
}
var output = new Collection<ITask>();
// Grabbing all time split points and sorting them
List<DateTime> timeEvents = taskList.SelectMany(task => new[] { task.StartTime, task.EndTime })
.Distinct()
.OrderBy(x => x)
.ToList();
for (var i = 0; i < timeEvents.Count - 1; i++)
{
var newSpan = new DateInterval(timeEvents[i], timeEvents[i + 1]);
var overlappingTasks = taskList.Where(x => x.DateInterval.Overlaps(newSpan));
if (overlappingTasks.Any())
{
var quantityPerHour = overlappingTasks.Sum(x => x.QuantityPerHour);
output.Add(Task.CreateUsingQuantityPerHour(newSpan, quantityPerHour));
}
}
return output;
}
示例4: DateDiff
/// <summary>
/// Devuelve un valor Long que especifica el número de
/// intervalos de tiempo entre dos valores Date.
/// </summary>
/// <param name="interval">Obligatorio. Valor de enumeración
/// DateInterval o expresión String que representa el intervalo
/// de tiempo que se desea utilizar como unidad de diferencia
/// entre Date1 y Date2.</param>
/// <param name="date1">Obligatorio. Date. Primer valor de
/// fecha u hora que se desea utilizar en el cálculo.</param>
/// <param name="date2">Obligatorio. Date. Segundo valor de
/// fecha u hora que se desea utilizar en el cálculo.</param>
/// <returns></returns>
public static long DateDiff(DateInterval interval, DateTime date1, DateTime date2)
{
long rs = 0;
TimeSpan diff = date2.Subtract(date1);
switch (interval)
{
case DateInterval.Day:
case DateInterval.DayOfYear:
rs = (long)diff.TotalDays;
break;
case DateInterval.Hour:
rs = (long)diff.TotalHours;
break;
case DateInterval.Minute:
rs = (long)diff.TotalMinutes;
break;
case DateInterval.Month:
rs = (date2.Month - date1.Month) + (12*DateTimeExtension.DateDiff(DateInterval.Year, date1, date2));
break;
case DateInterval.Quarter:
rs = (long)Math.Ceiling((double)(DateTimeExtension.DateDiff(DateInterval.Month, date1, date2) / 3.0));
break;
case DateInterval.Second:
rs = (long)diff.TotalSeconds;
break;
case DateInterval.Weekday:
case DateInterval.WeekOfYear:
rs = (long)(diff.TotalDays/7);
break;
case DateInterval.Year:
rs = date2.Year - date1.Year;
break;
}//switch
return rs;
}
示例5: DateDiff
internal static long DateDiff(DateInterval Interval, System.DateTime StartDate, System.DateTime EndDate)
{
long lngDateDiffValue = 0;
System.TimeSpan TS = new System.TimeSpan(EndDate.Ticks - StartDate.Ticks);
switch (Interval)
{
case DateInterval.Day:
lngDateDiffValue = (long)TS.Days;
break;
case DateInterval.Hour:
lngDateDiffValue = (long)TS.TotalHours;
break;
case DateInterval.Minute:
lngDateDiffValue = (long)TS.TotalMinutes;
break;
case DateInterval.Month:
lngDateDiffValue = (long)(TS.Days / 30);
break;
case DateInterval.Quarter:
lngDateDiffValue = (long)((TS.Days / 30) / 3);
break;
case DateInterval.Second:
lngDateDiffValue = (long)TS.TotalSeconds;
break;
case DateInterval.Week:
lngDateDiffValue = (long)(TS.Days / 7);
break;
case DateInterval.Year:
lngDateDiffValue = (long)(TS.Days / 365);
break;
}
return (lngDateDiffValue);
}
示例6: Construction_DefaultToInclusive
public void Construction_DefaultToInclusive()
{
LocalDate start = new LocalDate(2000, 1, 1);
LocalDate end = new LocalDate(2001, 6, 19);
var interval = new DateInterval(start, end);
Assert.IsTrue(interval.Inclusive);
}
示例7: GetDateDiff
/// <summary>
/// Gets the difference between two dates based on an interval.
/// </summary>
/// <param name="interval">The interval to use (seconds, minutes, hours, days, months, years)</param>
/// <param name="date1">The date to use for comparison.</param>
/// <param name="date2">The date to compare against the first date.</param>
/// <returns></returns>
public static object GetDateDiff(DateInterval interval, DateTime date1, DateTime date2)
{
object returnValue = null;
TimeSpan t;
object diff = 0.0;
//returns negative value if date1 is more recent
t = date2 - date1;
switch (interval)
{
case DateInterval.Second:
diff = t.TotalSeconds;
break;
case DateInterval.Minute:
diff = t.TotalMinutes;
break;
case DateInterval.Hour:
diff = t.TotalHours;
break;
case DateInterval.Day:
diff = t.TotalDays;
break;
case DateInterval.Month:
diff = t.TotalDays / 365.25 * 12.0;
break;
case DateInterval.Year:
diff = t.TotalDays / 365.25;
break;
}
returnValue = Convert.ToInt32(diff);
return returnValue;
}
示例8: Length_Exclusive
public void Length_Exclusive()
{
LocalDate start = new LocalDate(2000, 1, 1);
LocalDate end = new LocalDate(2000, 2, 10);
var interval = new DateInterval(start, end, false);
Assert.AreEqual(40, interval.Length);
}
示例9: AccountForEfficiencies_FixedEnd_MultipleDateIntervalEfficiencies_ReturnsCorrectAnswer
public void AccountForEfficiencies_FixedEnd_MultipleDateIntervalEfficiencies_ReturnsCorrectAnswer()
{
// +------------+
// |0| |0| |0|
// Result:
// +------------------+
// |0| |0| |0|
// Arrange
var now = DateTime.Now;
var dateInterval = new DateInterval(now, now.AddDays(5));
var dateIntervalEfficiencies = new List<DateIntervalEfficiency>();
var efficiency1 = new DateIntervalEfficiency(now.AddDays(1), now.AddDays(2), 0);
var efficiency2 = new DateIntervalEfficiency(now.AddDays(3), now.AddDays(4), 0);
var efficiency3 = new DateIntervalEfficiency(now.AddDays(5), now.AddDays(6), 0);
dateIntervalEfficiencies.Add(efficiency1);
dateIntervalEfficiencies.Add(efficiency2);
dateIntervalEfficiencies.Add(efficiency3);
// Act
var newDateInterval = dateInterval.AccountForEfficiencies(dateIntervalEfficiencies, FixedEndPoint.Max);
var correctDateInterval = new DateInterval(dateInterval.Max.Value.Add(-dateInterval.Duration).Add(-efficiency1.Duration).Add(-efficiency2.Duration).Add(-efficiency3.Duration), dateInterval.Max.Value);
// Assert
Assert.AreEqual(correctDateInterval, newDateInterval);
}
示例10: DateDiff
public static long DateDiff(this DateTime startDate, DateTime endDate, DateInterval interval = DateInterval.Day)
{
long lngDateDiffValue = 0;
var timeSpan = new TimeSpan(endDate.Ticks - startDate.Ticks);
switch (interval)
{
case DateInterval.Second:
lngDateDiffValue = (long) timeSpan.TotalSeconds;
break;
case DateInterval.Minute:
lngDateDiffValue = (long) timeSpan.TotalMinutes;
break;
case DateInterval.Hour:
lngDateDiffValue = (long) timeSpan.TotalHours;
break;
case DateInterval.Day:
lngDateDiffValue = timeSpan.Days;
break;
case DateInterval.Week:
lngDateDiffValue = timeSpan.Days/7;
break;
case DateInterval.Month:
lngDateDiffValue = timeSpan.Days/30;
break;
case DateInterval.Quarter:
lngDateDiffValue = (timeSpan.Days/30)/3;
break;
case DateInterval.Year:
lngDateDiffValue = timeSpan.Days/365;
break;
}
return (lngDateDiffValue);
}
示例11: DateAdd
public static DateTime DateAdd(DateInterval interval, int value, DateTime date)
{
int ms = date.GetTime();
DateTime result;
switch (interval)
{
case DateInterval.Milliseconds:
result = new DateTime(ms + value);
break;
case DateInterval.Seconds:
result = new DateTime(ms + (value * 1000));
break;
case DateInterval.Minutes:
result = new DateTime(ms + (value * 1000 * 60));
break;
case DateInterval.Hours:
result = new DateTime(ms + (value * 1000 * 60 * 60));
break;
case DateInterval.Days:
result = new DateTime(ms + (value * 1000 * 60 * 60 * 24));
break;
default:
result = date;
break;
}
return result;
}
示例12: DateTimeSearchModel
public DateTimeSearchModel(string title, string columnName, DateInterval interval = DateInterval.Month)
{
this.title = title;
ColumnName = columnName;
endDate = DateTime.Now;
switch (interval)
{
case DateInterval.Day:
startDate = DateTime.Now.AddDays(-1);
break;
case DateInterval.Week:
startDate = DateTime.Now.AddDays(-7);
break;
case DateInterval.Month:
startDate = DateTime.Now.AddMonths(-1);
break;
case DateInterval.Quarter:
startDate = DateTime.Now.AddMonths(-3);
break;
case DateInterval.Year:
startDate = DateTime.Now.AddYears(-1);
break;
}
originalDate = startDate;
}
示例13: Construction_Properties
public void Construction_Properties()
{
LocalDate start = new LocalDate(2000, 1, 1);
LocalDate end = new LocalDate(2001, 6, 19);
var interval = new DateInterval(start, end, false);
Assert.AreEqual(start, interval.Start);
Assert.AreEqual(end, interval.End);
Assert.IsFalse(interval.Inclusive);
}
示例14: Setup
public void Setup()
{
now = DateTime.Now;
nowAndTenDaysInterval = new DateInterval(now, now.AddDays(10));
nowAndFiveDaysInterval = new DateInterval(now, now.AddDays(5));
twoDaysAndFiveDaysInterval = new DateInterval(now.AddDays(2), now.AddDays(5));
threeDaysAgoAndTwelveDaysInterval = new DateInterval(now.AddDays(-3), now.AddDays(12));
thirteenDaysAndFourteenDaysInterval = new DateInterval(now.AddDays(13), now.AddDays(14));
}
示例15: FixedStartPointNoOverlapsWithOffset
public void FixedStartPointNoOverlapsWithOffset(IAccountForEfficiencies accountForEfficienciesCalculator)
{
// 1% efficiency
var dateIntervalEfficiencies = DateIntervalCollectionGenerator.NoOverlaps(this.now, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1), this.numberOfIntervals)
.Select(interval => new DateIntervalEfficiency(interval, 1)).ToList();
var initialInterval = new DateInterval(this.now, dateIntervalEfficiencies.Last().Max.Value);
accountForEfficienciesCalculator.AccountForEfficiencies(initialInterval, dateIntervalEfficiencies, FixedEndPoint.Min);
}