Esempio n. 1
0
        private void Can_Use_NodaTime_OffsetDateTime_In_Document(OffsetDateTime odt)
        {
            using (var documentStore = NewDocumentStore())
            {
                documentStore.ConfigureForNodaTime();

                using (var session = documentStore.OpenSession())
                {
                    session.Store(new Foo {
                        Id = "foos/1", OffsetDateTime = odt
                    });
                    session.SaveChanges();
                }

                using (var session = documentStore.OpenSession())
                {
                    var foo = session.Load <Foo>("foos/1");

                    Assert.Equal(odt, foo.OffsetDateTime);
                }

                var json = documentStore.DatabaseCommands.Get("foos/1").DataAsJson;
                Debug.WriteLine(json.ToString(Formatting.Indented));
                var expected = odt.ToDateTimeOffset().ToString("o");
                Assert.Equal(expected, json.Value <string>("OffsetDateTime"));
            }
        }
        private void Can_Use_NodaTime_OffsetDateTime_In_Document(OffsetDateTime odt)
        {
            using (var documentStore = NewDocumentStore())
            {
                using (var session = documentStore.OpenSession())
                {
                    session.Store(new Foo {
                        Id = "foos/1", OffsetDateTime = odt
                    });
                    session.SaveChanges();
                }

                using (var session = documentStore.OpenSession())
                {
                    var foo = session.Load <Foo>("foos/1");

                    Assert.Equal(odt, foo.OffsetDateTime);
                }

                using (var session = documentStore.OpenSession())
                {
                    var command = new GetDocumentsCommand("foos/1", null, false);
                    session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);
                    var json = (BlittableJsonReaderObject)command.Result.Results[0];
                    System.Diagnostics.Debug.WriteLine(json.ToString());
                    var expected = odt.ToDateTimeOffset().ToString("o");
                    json.TryGet("OffsetDateTime", out string value);
                    Assert.Equal(expected, value);
                }
            }
        }
Esempio n. 3
0
        public void ToDateTimeOffset()
        {
            LocalDateTime  local  = new LocalDateTime(2012, 10, 6, 1, 2, 3);
            Offset         offset = Offset.FromHours(1);
            OffsetDateTime odt    = new OffsetDateTime(local, offset);

            DateTimeOffset expected = new DateTimeOffset(DateTime.SpecifyKind(new DateTime(2012, 10, 6, 1, 2, 3), DateTimeKind.Unspecified),
                                                         TimeSpan.FromHours(1));
            DateTimeOffset actual = odt.ToDateTimeOffset();

            Assert.AreEqual(expected, actual);
        }
Esempio n. 4
0
        public void Init()
        {
            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
            }.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
            _dateTimeOffset = _offsetDateTime.ToDateTimeOffset();
            _jsonSerializer = JsonSerializer.CreateDefault();

            _resolver = CompositeResolver.Create(new NodaTimeResolver(DateTimeZoneProviders.Tzdb),
                                                 StandardResolver.Default);
        }
Esempio n. 5
0
        public void ToDateTimeOffset_JulianCalendar()
        {
            // Non-Gregorian calendar systems are handled by converting to the same
            // date, just like the DateTime constructor does.
            LocalDateTime  local  = new LocalDateTime(2012, 10, 6, 1, 2, 3, CalendarSystem.Julian);
            Offset         offset = Offset.FromHours(1);
            OffsetDateTime odt    = new OffsetDateTime(local, offset);

            DateTimeOffset expected = new DateTimeOffset(
                DateTime.SpecifyKind(new DateTime(2012, 10, 6, 1, 2, 3, new JulianCalendar()), DateTimeKind.Unspecified),
                TimeSpan.FromHours(1));
            DateTimeOffset actual = odt.ToDateTimeOffset();

            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        /// Parse the string to a Date/Time using the valid SPI formats
        /// </summary>
        /// <param name="value">string representation of date/time</param>
        /// <returns>Date/Time</returns>
        /// <exception cref="Exception"></exception>
        public static DateTime ToDateTime(this string value)
        {
            var isoMatch = Regex.Match(value, @"((\+[0-9]{2}:[0-9]{2})|Z)$");

            if (isoMatch.Success)
            {
                var offset         = isoMatch.Groups[1].Value;
                var dateTimeString = value.Substring(0, value.Length - offset.Length);
                var parsedDateTime = IsoDateTimePattern.Parse(dateTimeString);
                if (!parsedDateTime.Success)
                {
                    throw new InvalidDateTimeFormatException($"Unable to parse {value} to DateTime", SupportedDateTimePatterns, parsedDateTime.Exception);
                }

                var parsedOffset = OffsetPattern.GeneralInvariantWithZ.Parse(offset);
                if (!parsedOffset.Success)
                {
                    throw new InvalidDateTimeFormatException($"Unable to parse {value} to DateTime", SupportedDateTimePatterns, parsedOffset.Exception);
                }

                var offsetDateTime = new OffsetDateTime(parsedDateTime.Value, parsedOffset.Value);
                return(offsetDateTime.ToDateTimeOffset().DateTime);
            }


            Exception lastException = null;

            foreach (var localDatePattern in SupportedDateTimePatterns)
            {
                try
                {
                    var parsed = localDatePattern.Parse(value);
                    if (parsed.Success)
                    {
                        return(parsed.Value.ToDateTimeUnspecified());
                    }

                    lastException = parsed.Exception;
                }
                catch (Exception ex)
                {
                    lastException = ex;
                }
            }

            throw new InvalidDateTimeFormatException($"Unable to parse {value} to DateTime",
                                                     SupportedDateTimePatterns, lastException);
        }
        public static DateTimeOffset ToDateTimeOffset(OffsetDateTime offsetDateTime)
        {
            if (offsetDateTime.Equals(MinOffsetDateTime))
            {
                return(DateTimeOffset.MinValue);
            }
            if (offsetDateTime.Equals(MaxOffsetDateTime))
            {
                return(DateTimeOffset.MaxValue);
            }


            if ((offsetDateTime.LocalDateTime <= MinDateTimeLocalDateTime) ||
                (offsetDateTime.LocalDateTime >= MaxDateTimeLocalDateTime))
            {
                throw new ArgumentOutOfRangeException(nameof(offsetDateTime),
                                                      "Time not within allowable range, MinValue, or MaxValue");
            }

            return(offsetDateTime.ToDateTimeOffset());
        }
Esempio n. 8
0
    static void ParseApple(string text)
    {
        Console.WriteLine($"Parsing {text}");
        ParseResult <ZonedDateTime> result = ApplePattern.Parse(text);

        if (!result.Success)
        {
            Console.WriteLine($"Parse failed! {result.Exception.Message}");
            return;
        }
        ZonedDateTime zonedValue = result.Value;

        Console.WriteLine($"ZonedDateTime: {zonedValue}");
        // OffsetDateTime is a Noda Time type...
        OffsetDateTime offsetValue = zonedValue.ToOffsetDateTime();

        Console.WriteLine($"OffsetDateTime: {offsetValue}");
        // DateTimeOffset is the BCL type...
        DateTimeOffset dto = offsetValue.ToDateTimeOffset();

        Console.WriteLine($"DateTimeOffset: {dto}");
        Console.WriteLine();
    }
Esempio n. 9
0
 public static DateTimeOffset Resolve(OffsetDateTime value)
 {
     return(value.ToDateTimeOffset());
 }
Esempio n. 10
0
 /// <summary>
 /// Performs conversion from source to destination type
 /// </summary>
 /// <param name="source">Source object</param>
 /// <param name="destination">Destination object</param>
 /// <param name="context">Resolution context</param>
 /// <returns>Destination object</returns>
 public DateTimeOffset?Convert(OffsetDateTime?source, DateTimeOffset?destination, ResolutionContext context)
 => source?.ToDateTimeOffset() ?? destination;
Esempio n. 11
0
        private void Can_Use_NodaTime_OffsetDateTime_In_Dynamic_Index1(OffsetDateTime odt)
        {
            using (var documentStore = NewDocumentStore())
            {
                documentStore.ConfigureForNodaTime();

                using (var session = documentStore.OpenSession())
                {
                    session.Store(new Foo {
                        Id = "foos/1", OffsetDateTime = odt
                    });
                    session.Store(new Foo {
                        Id = "foos/2", OffsetDateTime = OffsetDateTime.FromDateTimeOffset(odt.ToDateTimeOffset().AddMinutes(1))
                    });
                    session.Store(new Foo {
                        Id = "foos/3", OffsetDateTime = OffsetDateTime.FromDateTimeOffset(odt.ToDateTimeOffset().AddMinutes(2))
                    });
                    session.SaveChanges();
                }

                using (var session = documentStore.OpenSession())
                {
                    var q1 = session.Query <Foo>().Customize(x => x.WaitForNonStaleResults())
                             .Where(x => x.OffsetDateTime == odt);
                    var results1 = q1.ToList();
                    Assert.Equal(1, results1.Count);

                    // OffsetDateTime is not directly comparable.
                    // Depending on context, one will either convert to an Instant, or just compare the LocalDateTime component.
                    // These are both supported by custom query expression handlers

                    var q2 = session.Query <Foo>().Customize(x => x.WaitForNonStaleResults())
                             .Where(x => x.OffsetDateTime.ToInstant() > odt.ToInstant())
                             .OrderByDescending(x => x.OffsetDateTime.ToInstant());
                    var results2 = q2.ToList();
                    Assert.Equal(2, results2.Count);
                    Assert.True(results2[0].OffsetDateTime.ToInstant() > results2[1].OffsetDateTime.ToInstant());

                    var q3 = session.Query <Foo>().Customize(x => x.WaitForNonStaleResults())
                             .Where(x => x.OffsetDateTime.ToInstant() >= odt.ToInstant())
                             .OrderByDescending(x => x.OffsetDateTime.ToInstant());
                    var results3 = q3.ToList();
                    Assert.Equal(3, results3.Count);
                    Assert.True(results3[0].OffsetDateTime.ToInstant() > results3[1].OffsetDateTime.ToInstant());
                    Assert.True(results3[1].OffsetDateTime.ToInstant() > results3[2].OffsetDateTime.ToInstant());

                    var q4 = session.Query <Foo>().Customize(x => x.WaitForNonStaleResults())
                             .Where(x => x.OffsetDateTime.LocalDateTime > odt.LocalDateTime)
                             .OrderByDescending(x => x.OffsetDateTime.LocalDateTime);
                    var results4 = q4.ToList();
                    Assert.Equal(2, results4.Count);
                    Assert.True(results4[0].OffsetDateTime.LocalDateTime > results4[1].OffsetDateTime.LocalDateTime);

                    var q5 = session.Query <Foo>().Customize(x => x.WaitForNonStaleResults())
                             .Where(x => x.OffsetDateTime.LocalDateTime >= odt.LocalDateTime)
                             .OrderByDescending(x => x.OffsetDateTime.LocalDateTime);
                    var results5 = q5.ToList();
                    Assert.Equal(3, results5.Count);
                    Assert.True(results5[0].OffsetDateTime.LocalDateTime > results5[1].OffsetDateTime.LocalDateTime);
                    Assert.True(results5[1].OffsetDateTime.LocalDateTime > results5[2].OffsetDateTime.LocalDateTime);
                }
            }
        }
Esempio n. 12
0
        public void ToDateTimeOffset()
        {
            LocalDateTime local = new LocalDateTime(2012, 10, 6, 1, 2, 3);
            Offset offset = Offset.FromHours(1);
            OffsetDateTime odt = new OffsetDateTime(local, offset);

            DateTimeOffset expected = new DateTimeOffset(DateTime.SpecifyKind(new DateTime(2012, 10, 6, 1, 2, 3), DateTimeKind.Unspecified),
                TimeSpan.FromHours(1));
            DateTimeOffset actual = odt.ToDateTimeOffset();
            Assert.AreEqual(expected, actual);
        }
 protected override DateTimeOffset Wrap(OffsetDateTime value) => value.ToDateTimeOffset();
Esempio n. 14
0
 public RaidTime(OffsetDateTime dateTime)
 {
     _odt = dateTime;
     _dto = _odt.ToDateTimeOffset();
 }