public override object Execute(IReadOnlyList <object> args) { var date = ArgUtil.GetDateArg(args[0], "date", Name); TimeSpan offset; if (args[1] is int || args[1] is long) { var minutes = ArgUtil.GetInt32Arg(args[1], "time-zone", Name); offset = new TimeSpan(0, minutes, 0); } else { var offsetStr = ArgUtil.GetStrArg(args[1], "time-zone", Name); var m = _tzOffsetRegex.Match(offsetStr); if (m.Success) { var sign = m.Groups[1].Value == "+" ? 1 : -1; var hour = int.Parse(m.Groups[2].Value); var minute = int.Parse(m.Groups[3].Value); offset = new TimeSpan(sign * hour, sign * minute, 0); } else { throw new Exception($"{Name.ToUpper()}: \"{offsetStr}\" is not a valid time zone offset."); } } var newDate = new DateTimeOffset(date.DateTime, offset); return(DateTimeUtil.FormatDateTimeOffset(newDate)); }
public override object Execute(IReadOnlyList <object> args) { var datePart = ArgUtil.GetDatePartArg(args[0], "date-part", Name); var date = ArgUtil.GetDateArg(args[1], "date", Name); return(DateTimeUtil.FormatDateTimeOffset(DateTimeUtil.TruncateDate(datePart, date))); }
public override object Execute(IReadOnlyList <object> args) { var datePart = ArgUtil.GetDatePartArg(args[0], "date-part", Name); var date = ArgUtil.GetDateArg(args[1], "date", Name); return(DateTimeUtil.GetDatePart(date, datePart)); }
public override object Execute(IReadOnlyList <object> args) { var date = ArgUtil.GetDateArg(args[0], "start-date", Name); var lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); return(DateTimeUtil.FormatDate(lastDayOfMonth)); }
public override object Execute(IReadOnlyList <object> args) { var date = ArgUtil.GetDateArg(args[0], "start-date", Name); var monthsToAdd = ArgUtil.GetInt32Arg(args[1], "months-to-add", Name); date = date.AddMonths(monthsToAdd); var lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); return(DateTimeUtil.FormatDate(lastDayOfMonth)); }
public override object Execute(IReadOnlyList <object> args) { var datePart = ArgUtil.GetDatePartArg(args[0], "date-part", Name); var startDate = ArgUtil.GetDateArg(args[1], "start-date", Name); var endDate = ArgUtil.GetDateArg(args[2], "end-date", Name); // datediff() counts the number of calendar/clock boundaries crossed, which is different than // the amount of time between the two dates. for instance, if startDate is one second before // midnight and endDate is one second after, that's one day boundary crossed. if (startDate == endDate) { return(0); } startDate = DateTimeUtil.TruncateDate(datePart, startDate); endDate = DateTimeUtil.TruncateDate(datePart, endDate); var span = endDate - startDate; var totalMonths = (endDate.Year * 12L + endDate.Month) - (startDate.Year * 12L + startDate.Month); switch (datePart) { case DatePart.Year: return((long)endDate.Year - startDate.Year); case DatePart.Quarter: return(totalMonths / 3); case DatePart.Month: return(totalMonths); case DatePart.DayOfYear: case DatePart.Day: case DatePart.DayOfWeek: return((long)span.TotalDays); case DatePart.Week: return((long)span.TotalDays / 7); case DatePart.Hour: return((long)span.TotalHours); case DatePart.Minute: return((long)span.TotalMinutes); case DatePart.Second: return((long)span.TotalSeconds); case DatePart.Millisecond: return((long)span.TotalMilliseconds); case DatePart.TzOffset: throw new Exception( $"{Name.ToUpper()}: The \"date-part\" argument must not be 'TZOFFSET'."); default: throw new Exception("Internal error: unrecognized DatePart."); } }
public override object Execute(IReadOnlyList <object> args) { var datePart = ArgUtil.GetDatePartArg(args[0], "date-part", Name); var date = ArgUtil.GetDateArg(args[1], "date", Name); var cal = CultureInfo.CurrentCulture.Calendar; var dtf = CultureInfo.CurrentCulture.DateTimeFormat; switch (datePart) { case DatePart.Month: return(dtf.GetMonthName(cal.GetMonth(date))); case DatePart.DayOfWeek: return(dtf.GetDayName(cal.GetDayOfWeek(date))); default: return(DateTimeUtil.GetDatePart(date, datePart).ToString()); } }
public override object Execute(IReadOnlyList <object> args) { var datePart = ArgUtil.GetDatePartArg(args[0], "date-part", Name); var number = ArgUtil.GetInt32Arg(args[1], "number", Name); var date = ArgUtil.GetDateArg(args[2], "date", Name); var newDate = datePart switch { DatePart.Year => date.AddYears(number), DatePart.Quarter => date.AddMonths(number * 3), DatePart.Month => date.AddMonths(number), DatePart.DayOfYear or DatePart.Day or DatePart.DayOfWeek => date.AddDays(number), DatePart.Week => date.AddDays(7 * number), DatePart.Hour => date.AddHours(number), DatePart.Minute => date.AddMinutes(number), DatePart.Second => date.AddSeconds(number), DatePart.Millisecond => date.AddMilliseconds(number), _ => throw new Exception($"Internal error: unrecognized DatePart."), }; return(DateTimeUtil.FormatDateTime(newDate)); }
public override object Execute(IReadOnlyList <object> args) { var datePart = ArgUtil.GetDatePartArg(args[0], "date-part", Name); var number = ArgUtil.GetInt32Arg(args[1], "number", Name); var date = ArgUtil.GetDateArg(args[2], "date", Name); var newDate = date; switch (datePart) { case DatePart.Year: newDate = date.AddYears(number); break; case DatePart.Quarter: newDate = date.AddMonths(number * 3); break; case DatePart.Month: newDate = date.AddMonths(number); break; case DatePart.DayOfYear: case DatePart.Day: case DatePart.DayOfWeek: newDate = date.AddDays(number); break; case DatePart.Week: newDate = date.AddDays(7 * number); break; case DatePart.Hour: newDate = date.AddHours(number); break; case DatePart.Minute: newDate = date.AddMinutes(number); break; case DatePart.Second: newDate = date.AddSeconds(number); break; case DatePart.Millisecond: newDate = date.AddMilliseconds(number); break; case DatePart.TzOffset: throw new Exception( $"{Name.ToUpper()}: The \"date-part\" argument must not be 'TZOFFSET'."); default: throw new Exception($"Internal error: unrecognized DatePart."); } return(DateTimeUtil.FormatDateTimeOffset(newDate)); }
public override object Execute(IReadOnlyList <object> args) { var input = ArgUtil.GetDateArg(args[0], "input", Name); return(DateTimeUtil.FormatDateTimeOffset(input)); }