14 | ///
15 | /// This attribute only applies to types, not fields:
16 | /// it's entirely feasible to have a readonly field of a mutable type, or a read/write
17 | /// field of an immutable type. In such cases for reference types (classes and interfaces)
18 | /// it's important to distinguish between the value of the variable (a reference) and the
19 | /// object it refers to. Value types are more complicated as in some cases the compiler
20 | /// will copy values before operating on them; however as all value types in Noda Time are
21 | /// immutable (aside from explicitly implemented serialization operations) this rarely causes
22 | /// an issue.
23 | ///
24 | ///
25 | /// Some types may be publicly immutable, but contain privately mutable
26 | /// aspects, e.g. caches. If it proves to be useful to indicate the kind of
27 | /// immutability we're implementing, we can add an appropriate property to this
28 | /// attribute.
29 | ///
30 | ///
31 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
32 | internal sealed class ImmutableAttribute : Attribute
33 | {
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/NodaTime/Calendars/CopticYearMonthDayCalculator.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013 The Noda Time Authors. All rights reserved.
2 | // Use of this source code is governed by the Apache License 2.0,
3 | // as found in the LICENSE.txt file.
4 |
5 | namespace NodaTime.Calendars
6 | {
7 | internal sealed class CopticYearMonthDayCalculator : FixedMonthYearMonthDayCalculator
8 | {
9 | internal CopticYearMonthDayCalculator()
10 | : base(1, 9715, -615558)
11 | {
12 | }
13 |
14 | protected override int CalculateStartOfYearDays(int year)
15 | {
16 | // Unix epoch is 1970-01-01 Gregorian which is 1686-04-23 Coptic.
17 | // Calculate relative to the nearest leap year and account for the
18 | // difference later.
19 |
20 | int relativeYear = year - 1687;
21 | int leapYears;
22 | if (relativeYear <= 0)
23 | {
24 | // Add 3 before shifting right since /4 and >>2 behave differently
25 | // on negative numbers.
26 | leapYears = (relativeYear + 3) >> 2;
27 | }
28 | else
29 | {
30 | leapYears = relativeYear >> 2;
31 | // For post 1687 an adjustment is needed as jan1st is before leap day
32 | if (!IsLeapYear(year))
33 | {
34 | leapYears++;
35 | }
36 | }
37 |
38 | int ret = relativeYear * 365 + leapYears;
39 |
40 | // Adjust to account for difference between 1687-01-01 and 1686-04-23.
41 | return ret + (365 - 112);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/NodaTime.Test/Utility/TickArithmeticTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 The Noda Time Authors. All rights reserved.
2 | // Use of this source code is governed by the Apache License 2.0,
3 | // as found in the LICENSE.txt file.
4 | using NodaTime.Utility;
5 | using NUnit.Framework;
6 |
7 | namespace NodaTime.Test.Utility
8 | {
9 | public class TickArithmeticTest
10 | {
11 | [Test]
12 | [TestCase(long.MinValue)]
13 | [TestCase(-NodaConstants.TicksPerDay - 1)]
14 | [TestCase(-NodaConstants.TicksPerDay)]
15 | [TestCase(-NodaConstants.TicksPerDay + 1)]
16 | [TestCase(-1)]
17 | [TestCase(0)]
18 | [TestCase(1)]
19 | [TestCase(NodaConstants.TicksPerDay - 1)]
20 | [TestCase(NodaConstants.TicksPerDay)]
21 | [TestCase(NodaConstants.TicksPerDay + 1)]
22 | [TestCase(long.MaxValue)]
23 | public void TicksToDaysAndTickOfDayAndBack(long ticks)
24 | {
25 | int days = TickArithmetic.TicksToDaysAndTickOfDay(ticks, out long tickOfDay);
26 |
27 | Assert.AreEqual(ticks, TickArithmetic.DaysAndTickOfDayToTicks(days, tickOfDay));
28 | }
29 |
30 | [Test]
31 | public void DaysAndTickOfDayToTicksUncheckedBoundaries()
32 | {
33 | // Only a useful test under debug, but this proves that the arithmetic won't overflow when used from
34 | // LocalDateTime or Instant. (In debug mode, we have
35 | TickArithmetic.BoundedDaysAndTickOfDayToTicks(CalendarSystem.Iso.MinDays, 0);
36 | TickArithmetic.BoundedDaysAndTickOfDayToTicks(CalendarSystem.Iso.MaxDays, NodaConstants.TicksPerDay - 1);
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/NodaTime.Test/Annotations/SecurityTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 The Noda Time Authors. All rights reserved.
2 | // Use of this source code is governed by the Apache License 2.0,
3 | // as found in the LICENSE.txt file.
4 |
5 | using NUnit.Framework;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Reflection;
9 | using System.Security;
10 |
11 | namespace NodaTime.Test.Annotations
12 | {
13 | public class SecurityTest
14 | {
15 | [Test]
16 | public void SecurityAttributesOnInterfaceImplementations()
17 | {
18 | var violations = new List