/// <summary> /// Creates a new instance of Instant from: /// /// Year, Month, Day, Hour, Minute, Second, and Millisecond /// /// in UTC. /// </summary> public static Instant Utc(int year, int month, int day, int hour, int minute, int second, int millisecond) { // Create local date from the specified fields var localDateTime = new LocalDateTime(year, month, day, hour, minute, second, millisecond); // Convert to Instant in UTC timezone. var result = InstantUtil.Utc(localDateTime); return(result); }
/// <summary> /// Convert System.DateTime with Kind=Utc to Instant. /// /// Converts default constructed DateTime to Instant.Empty. /// /// Error message if the timezone is not UTC. /// </summary> public static Instant ToInstant(this DateTime value) { // Error message if the timezone is not UTC. if (value.Kind != DateTimeKind.Utc) { throw new Exception("DateTime can only be converted to Instant when its Kind=UTC."); } if (value != default) { // If not default constructed value, convert // to millisecond precision using fields return(InstantUtil.Utc( value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, value.Millisecond)); } else { // Converts default constructed DateTime // to Instant.Empty return(InstantUtil.Empty); } }