private static Job CreateJob(int index, PartialDateRange dates) { return(new Job { Id = Guid.NewGuid(), Company = string.Format(CompanyFormat, index), Title = string.Format(TitleFormat, index), Description = string.Format(DescriptionFormat, index), Dates = dates, }); }
public static bool IsValidTenureForDisplay(this PartialDateRange dateTimeRange) { if (dateTimeRange == null || dateTimeRange.Start == null) return false; if (dateTimeRange.Start > dateTimeRange.End) return false; if (dateTimeRange.End != null) { try { var dtStart = new DateTime(dateTimeRange.Start.Value.Year, dateTimeRange.Start.Value.Month ?? 1, dateTimeRange.Start.Value.Day ?? 1); var dtEnd = new DateTime(dateTimeRange.End.Value.Year, dateTimeRange.End.Value.Month ?? 1, dateTimeRange.End.Value.Day ?? 1); var duration = DateTime.MinValue + (dtEnd - dtStart); } catch (Exception) { return false; } } return true; }
public static string GetTenureDisplayText(this PartialDateRange dateTimeRange) { if (!IsValidTenureForDisplay(dateTimeRange)) return "Unknown"; DateTime? duration; var dtStart = new DateTime(dateTimeRange.Start.Value.Year, dateTimeRange.Start.Value.Month ?? 1, dateTimeRange.Start.Value.Day ?? 1); if (dateTimeRange.End == null) { if (DateTime.Now > dtStart) { // End = "Current" duration = DateTime.MinValue + (DateTime.Now - dtStart); } else { return "Unknown"; } } else { var dtEnd = new DateTime(dateTimeRange.End.Value.Year, dateTimeRange.End.Value.Month ?? 1, dateTimeRange.End.Value.Day ?? 1); duration = DateTime.MinValue + (dtEnd - dtStart); } var years = duration.Value.Year - 1; var months = duration.Value.Month - 1; if (years == 0) { if (months == 0) return "<1 month"; return string.Format("{0} {1}", months, months == 1 ? "month" : "months"); } return string.Format("{0} {1}", Math.Round((double)(years + months / 12) * 10) / 10, years == 1 ? "year" : "years"); }
public static void MapTo(this PartialDateRange partialDateRange, IHavePartialDateRangeEntity entity) { if (partialDateRange == null) { entity.startDate = null; entity.startDateParts = null; entity.endDate = null; entity.endDateParts = null; entity.isCurrent = false; return; } // Convert the dates first to deal with errors etc. DateTime? startDate; DateParts?startDateParts; DateTime? endDate; DateParts?endDateParts; partialDateRange.Start.MapTo(out startDate, out startDateParts); partialDateRange.End.MapTo(out endDate, out endDateParts); // 1st possibility, no start or end date. if (startDate == null && endDate == null) { entity.startDate = null; entity.startDateParts = null; entity.endDate = null; entity.endDateParts = null; entity.isCurrent = true; return; } // 2nd possibility, a start date but no end date. if (startDate != null && endDate == null) { entity.startDate = startDate; entity.startDateParts = (byte?)startDateParts; entity.endDate = null; entity.endDateParts = null; entity.isCurrent = true; return; } // 3rd possibility, no start date but does have an end date. if (startDate == null) { entity.startDate = null; entity.startDateParts = null; entity.endDate = endDate; entity.endDateParts = (byte?)endDateParts; entity.isCurrent = false; return; } // 4th possibility, start date and end date. entity.startDate = startDate; entity.startDateParts = (byte?)startDateParts; entity.endDate = endDate; entity.endDateParts = (byte?)endDateParts; entity.isCurrent = false; }