/// <summary>Parses <see cref="WbTime"/> from its string representation.</summary> /// <inheritdoc cref="WbTime(int,int,int,int,int,int,int,int,int,WikibaseTimePrecision,Uri)"/> /// <param name="dateTime">The string representation of the date and time. Currently only ISO-8601 format is supported.</param> public static WbTime Parse(string dateTime, int before, int after, int timeZone, WikibaseTimePrecision precision, Uri calendarModel) { if (dateTime == null) { throw new ArgumentNullException(nameof(dateTime)); } if (calendarModel == null) { throw new ArgumentNullException(nameof(calendarModel)); } var dateTimeMatch = ISO8601Matcher.Match(dateTime); if (!dateTimeMatch.Success) { throw new ArgumentException("Invalid ISO-8601 time format.", nameof(dateTime)); } var year = Convert.ToInt32(dateTimeMatch.Groups["Y"].Value); var month = Convert.ToInt32(dateTimeMatch.Groups["M"].Value); var day = Convert.ToInt32(dateTimeMatch.Groups["D"].Value); var hour = Convert.ToInt32(dateTimeMatch.Groups["H"].Value); var minute = Convert.ToInt32(dateTimeMatch.Groups["m"].Value); var second = Convert.ToInt32(dateTimeMatch.Groups["S"].Value); return(new WbTime(year, month, day, hour, minute, second, before, after, timeZone, precision, calendarModel)); }
/// <inheritdoc cref="WbTime(int,int,int,int,int,int,int,int,int,WikibaseTimePrecision,Uri)"/> /// <summary>Constructs a <see cref="WbTime"/> instance from <see cref="DateTimeOffset"/>.</summary> /// <param name="dateTime">The date, time, and time zone.</param> public static WbTime FromDateTimeOffset(DateTimeOffset dateTime, int before, int after, WikibaseTimePrecision precision, Uri calendarModel) { return(new WbTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Minute, before, after, (int)dateTime.Offset.TotalMinutes, precision, calendarModel)); }
/// <summary>Constructs a <see cref="WbTime"/> instance from <see cref="DateTime"/>.</summary> /// <inheritdoc cref="WbTime(int,int,int,int,int,int,int,int,int,WikibaseTimePrecision,Uri)"/> /// <remarks>This overload uses 0 as <see cref="TimeZone"/> if <see cref="DateTime.Kind"/> /// is <see cref="DateTimeKind.Utc"/>, and uses local time zone otherwise.</remarks> public static WbTime FromDateTime(DateTime dateTime, int before, int after, WikibaseTimePrecision precision, Uri calendarModel) { if (calendarModel == null) { throw new ArgumentNullException(nameof(calendarModel)); } var timeZone = dateTime.Kind == DateTimeKind.Utc ? 0 : (int)TimeZoneInfo.Local.BaseUtcOffset.TotalMinutes; return(new WbTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Minute, before, after, timeZone, precision, calendarModel)); }
/// <summary>Initialize a new instance of <see cref="WbTime"/> with the specified time point, time zone, /// precision and calendar model.</summary> /// <param name="year">The year (-999999999 through 999999999).</param> /// <param name="month">The month (positive integer).</param> /// <param name="day">The day (positive integer).</param> /// <param name="hour">The hour (0 through 23).</param> /// <param name="minute">The hour (0 through 59).</param> /// <param name="second">The hour (0 through 59).</param> /// <param name="before">Number of units before the given time it could be, if uncertain. The unit is given by <paramref name="precision"/>.</param> /// <param name="after">Number of units after the given time it could be, if uncertain. The unit is given by <paramref name="precision"/>.</param> /// <param name="timeZone">Time zone offset in minutes. See <see cref="TimeZone"/> for more information.</param> /// <param name="precision">The unit of the precision of the time.</param> /// <param name="calendarModel">The entity URI of the calendar model.</param> /// <exception cref="ArgumentNullException"><paramref name="calendarModel"/> is <c>null</c>.</exception> /// <exception cref="ArgumentOutOfRangeException">One or more numeric parameters are out of range.</exception> public WbTime(int year, int month, int day, int hour, int minute, int second, int before, int after, int timeZone, WikibaseTimePrecision precision, Uri calendarModel) { if (calendarModel == null) { throw new ArgumentNullException(nameof(calendarModel)); } if (precision < WikibaseTimePrecision.YearE9 || precision > WikibaseTimePrecision.Second) { throw new ArgumentOutOfRangeException(nameof(precision)); } if (precision >= WikibaseTimePrecision.Month && month <= 0) { throw new ArgumentOutOfRangeException(nameof(month)); } if (precision >= WikibaseTimePrecision.Day && day <= 0) { throw new ArgumentOutOfRangeException(nameof(day)); } if (precision >= WikibaseTimePrecision.Hour && (hour < 0 || hour >= 24)) { throw new ArgumentOutOfRangeException(nameof(hour)); } if (precision >= WikibaseTimePrecision.Minute && (minute < 0 || minute >= 60)) { throw new ArgumentOutOfRangeException(nameof(minute)); } if (precision >= WikibaseTimePrecision.Second && (second < 0 || second >= 60)) { throw new ArgumentOutOfRangeException(nameof(second)); } Year = year; Month = month; Day = day; Hour = hour; Minute = minute; Second = second; Before = before; After = after; TimeZone = timeZone; Precision = precision; CalendarModel = calendarModel; }
/// <inheritdoc cref="FromDateTimeOffset(DateTimeOffset,int,int,WikibaseTimePrecision,Uri)"/> /// <summary>Constructs a <see cref="WbTime"/> instance from <see cref="DateTimeOffset"/>, using the appropriate calendar model.</summary> /// <param name="dateTime">The date, time, and time zone.</param> public static WbTime FromDateTimeOffset(DateTimeOffset dateTime, int before, int after, WikibaseTimePrecision precision) { return(FromDateTimeOffset(dateTime, before, after, precision, GetCalendarModel(dateTime.Year, dateTime.Month))); }
/// <inheritdoc cref="FromDateTime(DateTime,int,int,WikibaseTimePrecision,Uri)"/> /// <remarks>This overload uses 0 as <see cref="TimeZone"/> if <see cref="DateTime.Kind"/> /// is <see cref="DateTimeKind.Utc"/>, and uses local time zone otherwise.</remarks> public static WbTime FromDateTime(DateTime dateTime, int before, int after, WikibaseTimePrecision precision) { return(FromDateTime(dateTime, before, after, precision, null)); }
/// <summary>Constructs a <see cref="WbTime"/> instance from <see cref="DateTime"/>, using the appropriate calendar model.</summary> /// <inheritdoc cref="FromDateTime(DateTime,int,int,WikibaseTimePrecision,Uri)"/> /// <remarks>This overload uses 0 as <see cref="TimeZone"/> if <see cref="DateTime.Kind"/> /// is <see cref="DateTimeKind.Utc"/>, and uses local time zone otherwise.</remarks> public static WbTime FromDateTime(DateTime dateTime, WikibaseTimePrecision precision) { return(FromDateTime(dateTime, 0, 0, precision, GetCalendarModel(dateTime.Year, dateTime.Month))); }