C# / Intermediate: Composition and Asynchronous Work / Lesson 14
Instants, offsets, and durations
Compare .NET DateTimeOffset instants and test local-time policies.
On this page
Lesson goals
- Compare instants with explicit offsets.
- Test duration and local-time boundaries.
- Distinguish instant comparison from local representation and recurrence.
- Test duration, parsing and zone-transition policies with fixed fixtures.
Define the time contract
An instant, a local calendar time, and an elapsed duration answer different questions. DateTimeOffset combines a local clock value with an offset and identifies an instant; TimeSpan represents an interval. Specify the meaning at the API boundary before comparing or storing values. A local appointment may also need a named time zone, because an offset alone does not contain future daylight-saving rules.
Compare equivalent instants
Two DateTimeOffset values with different clock readings and offsets can refer to the same instant. The example constructs fixed values and compares them, without using the machine’s current clock or local zone. Convert to UTC for a shared instant representation when appropriate, but retain any separately required zone identifier for future local scheduling. Avoid assuming a fixed offset identifies a region.
using System;
public class Program
{
public static void Main()
{
var a = new DateTimeOffset(2026, 1, 1, 12, 0, 0, TimeSpan.FromHours(2));
var b = new DateTimeOffset(2026, 1, 1, 10, 0, 0, TimeSpan.Zero);
Console.WriteLine(a == b);
}
}
True
Both values identify 10:00 UTC.
Measure an interval
Subtracting instants produces a TimeSpan. The second example uses fixed UTC instants one hour apart, so its result does not depend on system time. Choose an explicit unit when displaying or accepting a duration. A calendar day in a named time zone can have a different elapsed length around clock changes, so do not substitute a fixed 24-hour interval for every local calendar rule.
using System;
public class Program
{
public static void Main()
{
var start = new DateTimeOffset(2026, 1, 1, 10, 0, 0, TimeSpan.Zero);
var end = start.AddHours(1);
Console.WriteLine((end - start).TotalMinutes);
}
}
60
The fixed interval is sixty minutes.
Interpret local time
Parsing external dates needs a documented format, culture, and zone policy. An offset-bearing value can be converted to an instant, while an unqualified local value may be ambiguous or invalid at a time-zone transition. Reject or resolve those cases according to explicit requirements. Avoid relying on a server’s local zone or current culture to interpret machine-readable records.
Test time boundaries
Test equivalent instants with different offsets, a duration boundary, malformed input, and any ambiguous or invalid local time the application supports. Use fixed values or an injected clock rather than waiting for real time. Verify both the stored instant and the displayed local value when each is part of the contract. A passing UTC-only test does not establish correct local scheduling behavior.
Choose the temporal meaning first
An instant identifies one point on the timeline; a local date and time describes a calendar reading; a duration measures an interval. They answer different questions and should not be substituted merely because they can be formatted similarly. DateTimeOffset combines a clock reading and an offset from UTC to identify an instant. Subtracting its offset from the local reading gives the corresponding UTC reading, including any date rollover. TimeSpan represents an interval, not a time-zone rule or a calendar month. For a future recurring appointment such as every Monday at 09:00, store the local recurrence, named zone and transition policy as well as any resolved occurrence. An offset describes one relationship to UTC; many zones can share it at one moment and change later. Decide what must be preserved at the API boundary.
Compare instants and representations separately
Two DateTimeOffset values can have different clock readings and offsets while representing the same instant. Ordinary equality and ordering compare the represented UTC instants, whereas an application may also need to check the original offset or local presentation. ToOffset changes the displayed clock and offset while preserving the instant when the result is representable; it does not attach the rules of a named time zone. Converting to UTC is useful for ordering events across offsets, but discarding the original offset or zone can lose information required for an audit or user-facing appointment. Keep a named zone identifier separately when the domain requires future local scheduling. Reject invalid or missing required offsets rather than silently interpreting a machine-readable value using the host’s current local zone.
Define local transition policy
A named time zone supplies rules that map instants to local readings and can change its offset over time. When clocks move forward, some local readings may be invalid; when they move backward, some can be ambiguous and correspond to two instants. The application must choose whether to reject, shift, or resolve a particular occurrence, and document which offset or rule it selects. TimeZoneInfo provides checks for invalid and ambiguous local times, but the result depends on the selected zone and its available rule data. In tests, state the zone, date, rule-data assumptions and policy; for portable teaching, synthetic transition fixtures are not claims about a real jurisdiction. A DateTime whose Kind is Unspecified is useful for representing an unqualified wall-clock input to a chosen zone, but it is not itself a unique instant.
Measure duration in the right units
Subtracting DateTimeOffset values yields a TimeSpan between the represented instants. TotalMinutes and TotalHours express the whole interval in those units; Minutes and Hours are components and need not equal the total. Always state the units and whether a comparison includes its endpoint. A fixed 24-hour TimeSpan is not automatically the same as the next local calendar day in a zone with a clock transition: under an explicit spring-forward fixture, consecutive local midnights can be 23 elapsed hours apart. Calendar recurrence requires resolving the next local occurrence with the applicable zone rule rather than blindly adding a fixed number of elapsed hours. For measuring execution elapsed time, use an appropriate monotonic elapsed-time facility such as Stopwatch rather than assuming the system wall clock cannot be adjusted.
Parse and test with controlled evidence
Machine-readable input needs an explicit format, culture, offset and error policy. Use an appropriate ParseExact or TryParseExact overload with invariant culture where the protocol specifies it, and check the Boolean before using a parsed output. A successful parse does not settle whether a local time is valid or ambiguous in a named zone, nor does it supply the zone from a numeric offset. Test fixed equivalent and distinct instants, date rollover, malformed input, exact duration boundaries and both transition cases supported by the application. Inject a clock or use fixed values rather than waiting for the real clock; distinguish a fake clock test from integration with actual platform zone data. Store or report the UTC instant and the local representation separately when both are contractual. The following synthetic snippets are unexecuted locally and target C# 14, net10.0 and nullable analysis enabled.
20 worked examples
1. Equivalent offsets
Problem: Do these fixed values represent the same instant?
- Subtracting the -03:00 offset converts a to 12:00 UTC.
- b is already 12:00 UTC on the same date.
var a = new DateTimeOffset(2026, 2, 1, 9, 0, 0, TimeSpan.FromHours(-3));
var b = new DateTimeOffset(2026, 2, 1, 12, 0, 0, TimeSpan.Zero);
bool same = a == b;
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: same is true.
Why this result follows: DateTimeOffset equality compares the represented instant.
2. Same clock, different instants
Problem: Are these identical clock readings the same instant?
- a represents 08:00 UTC, while b represents 07:00 UTC.
- Their local clock components match but their UTC instants differ by one hour.
var a = new DateTimeOffset(2026, 2, 1, 9, 0, 0, TimeSpan.FromHours(1));
var b = new DateTimeOffset(2026, 2, 1, 9, 0, 0, TimeSpan.FromHours(2));
bool same = a == b;
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: same is false; b is earlier.
Why this result follows: The offset is needed to locate a local reading on the timeline.
3. UTC date rollover backward
Problem: What UTC date and time correspond to this reading?
- Subtracting two hours from 00:30 crosses midnight backward.
- The previous calendar date is March 1, at 22:30 UTC.
var local = new DateTimeOffset(2026, 3, 2, 0, 30, 0, TimeSpan.FromHours(2));
DateTimeOffset utc = local.ToUniversalTime();
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: utc is 2026-03-01 22:30 +00:00.
Why this result follows: Offset conversion can change the calendar date while preserving the instant.
4. UTC date rollover forward
Problem: Which UTC day contains this negative-offset reading?
- Subtracting a negative three-hour offset adds three hours to the clock reading.
- 23:30 plus three hours crosses into the next date at 02:30.
var local = new DateTimeOffset(2026, 3, 1, 23, 30, 0, TimeSpan.FromHours(-3));
DateTimeOffset utc = local.ToUniversalTime();
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: utc is 2026-03-02 02:30 +00:00.
Why this result follows: The sign of the offset matters when converting to UTC.
5. Convert offset without changing instant
Problem: What representation results from converting this instant to +03:00?
- The desired representation uses an offset three hours ahead of UTC.
- Adding that offset to 10:00 UTC gives a 13:00 local clock reading.
var utc = new DateTimeOffset(2026, 4, 1, 10, 0, 0, TimeSpan.Zero);
var local = utc.ToOffset(TimeSpan.FromHours(3));
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: local is 2026-04-01 13:00 +03:00 and equals utc as an instant.
Why this result follows: ToOffset changes representation while preserving the represented instant.
6. Equality versus exact representation
Problem: How do instant equality and exact equality differ here?
- The conversion preserves the UTC instant, so ordinary equality is true.
- The offsets and clock representations differ, so EqualsExact is false.
var a = new DateTimeOffset(2026, 4, 1, 10, 0, 0, TimeSpan.Zero);
var b = a.ToOffset(TimeSpan.FromHours(1));
bool sameInstant = a == b;
bool exact = a.EqualsExact(b);
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: sameInstant is true and exact is false.
Why this result follows: Exact representation comparison retains a distinction that instant equality ignores.
7. Ordering across offsets
Problem: Which of these two fixed events occurred first?
- a is 08:30 UTC after subtracting its two-hour offset.
- b is 09:00 UTC, thirty minutes later than a.
var a = new DateTimeOffset(2026, 5, 1, 10, 30, 0, TimeSpan.FromHours(2));
var b = new DateTimeOffset(2026, 5, 1, 9, 0, 0, TimeSpan.Zero);
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: a occurs first.
Why this result follows: Ordering uses the represented instants, not the displayed clock alone.
8. Elapsed interval across offsets
Problem: How many minutes elapsed between these fixed values?
- The start instant is 08:00 UTC.
- The end is 09:30 UTC, so the elapsed interval is ninety minutes.
var start = new DateTimeOffset(2026, 5, 1, 10, 0, 0, TimeSpan.FromHours(2));
var end = new DateTimeOffset(2026, 5, 1, 9, 30, 0, TimeSpan.Zero);
double minutes = (end - start).TotalMinutes;
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: minutes is 90.
Why this result follows: Subtraction compares the instants after accounting for offsets.
9. Negative duration
Problem: What happens when the end instant is earlier than the start?
- Both values use UTC, and end is thirty minutes earlier.
- Subtracting start from end yields a negative interval.
var start = new DateTimeOffset(2026, 5, 1, 12, 0, 0, TimeSpan.Zero);
var end = new DateTimeOffset(2026, 5, 1, 11, 30, 0, TimeSpan.Zero);
TimeSpan delta = end - start;
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: delta.TotalMinutes is -30.
Why this result follows: A TimeSpan can represent a negative difference.
10. Duration components versus totals
Problem: What do Hours and TotalHours report for a 27-hour interval?
- Twenty-seven hours contain one full day and three remaining hours.
- Hours reports the hour component, while TotalHours reports the whole interval in hours.
TimeSpan duration = TimeSpan.FromHours(27);
int component = duration.Hours;
double total = duration.TotalHours;
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: component is 3 and total is 27.
Why this result follows: A component is not the total duration in that unit.
11. Minutes component versus total
Problem: How many minutes are represented by this 90-minute span?
- Ninety minutes equal one hour and thirty remaining minutes.
- Minutes gives the remaining component while TotalMinutes gives the entire interval.
TimeSpan duration = TimeSpan.FromMinutes(90);
int component = duration.Minutes;
double total = duration.TotalMinutes;
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: component is 30 and total is 90.
Why this result follows: TimeSpan exposes both components and totals for different purposes.
12. Inclusive expiration boundary
Problem: Is the item expired exactly at its deadline?
- The contract in this expression includes equality at the deadline.
- now and deadline represent the same instant.
var deadline = new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero);
var now = deadline;
bool expired = now >= deadline;
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: expired is true.
Why this result follows: The >= operator makes the boundary inclusive.
13. Parsing a fixed offset
Problem: What instant does this invariant-format input identify?
- The text matches the specified date, time and numeric-offset pattern.
- Subtracting +02:00 from 14:30 gives 12:30 UTC.
string text = "2026-07-01T14:30:00+02:00";
bool ok = DateTimeOffset.TryParseExact(text, "yyyy-MM-dd'T'HH:mm:sszzz", CultureInfo.InvariantCulture, DateTimeStyles.None, out var value);
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: ok is true and value represents 2026-07-01 12:30 UTC.
Why this result follows: Explicit format and culture avoid relying on ambient parsing conventions.
14. Malformed fixed-format input
Problem: Does this text match the required complete format?
- The input does not contain the required date, time and offset.
- TryParseExact reports failure rather than returning a successful instant.
string text = "not-a-date";
bool ok = DateTimeOffset.TryParseExact(text, "yyyy-MM-dd'T'HH:mm:sszzz", CultureInfo.InvariantCulture, DateTimeStyles.None, out var value);
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: ok is false; the output must not be treated as a parsed result.
Why this result follows: The Boolean carries parse success separately from the output value.
15. Synthetic invalid local time
Problem: What should a rejecting policy do with this local reading?
- The jump skips the local interval containing 02:30.
- The stated policy rejects readings in that nonexistent interval.
synthetic zone rule: clocks jump from 01:59 to 03:00
local appointment: 02:30 on transition day
policy: reject invalid local times
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: Reject the appointment and request a valid local time.
Why this result follows: A local reading can fail to map to any instant under a zone’s transition rule.
16. Synthetic ambiguous local time
Problem: How many candidate instants exist under this fall-back fixture?
- 01:30 at +02:00 maps to 23:30 UTC on the previous date.
- 01:30 at +01:00 maps to 00:30 UTC, one hour later.
synthetic zone: 01:30 occurs once at +02:00 and once at +01:00
policy: choose the earlier instant
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: There are two candidates; choose the +02:00 occurrence under this policy.
Why this result follows: An ambiguous wall-clock reading needs an explicit resolution rule.
17. Synthetic spring day duration
Problem: How long is this local calendar day in elapsed hours?
- The first midnight maps to 23:00 UTC on the preceding date.
- The next midnight maps to 22:00 UTC on the first local date, a difference of 23 hours.
synthetic zone: midnight starts at +01:00
next local midnight occurs at +02:00
local dates are consecutive
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: The elapsed interval is 23 hours.
Why this result follows: A calendar day can differ from a fixed 24-hour duration across a transition.
18. Synthetic fall day duration
Problem: How many elapsed hours span these fall-back midnights?
- The first midnight maps to 22:00 UTC on the preceding date.
- The next midnight maps to 23:00 UTC on the first local date, a difference of 25 hours.
synthetic zone: midnight starts at +02:00
next local midnight occurs at +01:00
local dates are consecutive
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: The elapsed interval is 25 hours.
Why this result follows: A backward clock change can lengthen an elapsed local calendar day.
19. Offset does not identify a zone
Problem: Can an observed +01:00 offset determine a unique future schedule?
- Both zones fit the current offset observation.
- Their future offsets differ under the stated synthetic rules.
fixture: zone A and zone B both use +01:00 today
future: A changes to +02:00; B stays +01:00
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: The offset alone cannot select the correct future zone rule.
Why this result follows: A numeric offset does not encode a named zone’s transition history or future rules.
20. Elapsed measurement and wall-clock adjustment
Problem: Which measurement should an elapsed-time test prefer?
- Wall-clock readings can be adjusted and their difference may not reflect elapsed execution.
- A monotonic elapsed-time facility such as Stopwatch is designed for measuring intervals.
synthetic trace: wall clock changes backward during a running operation
requirement: measure elapsed execution duration
Illustrative fragment or synthetic rule, not executed locally; it makes no claim about a real zone.
Expected result: Use a controlled elapsed-time abstraction or Stopwatch for the duration requirement.
Why this result follows: Elapsed measurement has a different contract from calendar time.
Common errors and fixes
Equivalent instants look different
- Why it happens
- Their local clock readings have different offsets.
- How to fix it
- Compare the represented instants.
- Prevent it next time
- Store an explicit offset or instant.
A local appointment shifts unexpectedly
- Why it happens
- An offset was treated as a complete time-zone rule.
- How to fix it
- Use the required named zone and transition policy.
- Prevent it next time
- Test transition cases.
A test depends on the machine clock
- Why it happens
- Current time was read directly.
- How to fix it
- Use fixed values or an injected clock.
- Prevent it next time
- Control time in tests.
20 practice exercises
1. Are 12:00 at +02:00 and 10:00 at +00:00 the same instant?
Show hint
Convert both readings to UTC before comparing.
Show worked solution
Yes. Subtracting the two-hour offset from the first clock reading gives 10:00 UTC.
Subtract the +02:00 offset from 12:00 to obtain 10:00 UTC. The second value is already 10:00 at +00:00, so both represent the same instant even though their clock readings differ.
Check your solution
- The first reading converts to 10:00 UTC.
- The two DateTimeOffset values compare as the same instant.
2. Why is an offset insufficient for a recurring local appointment?
Show hint
A recurrence uses rules for future dates, not just one observed offset.
Show worked solution
A region can change offsets over time. The schedule needs its named zone and a policy for transition cases.
A fixed offset does not identify a region or its future clock transitions. Store the local recurrence with the intended named zone and a policy for invalid or ambiguous occurrences; resolve each occurrence using the appropriate zone rules.
Check your solution
- The schedule retains a named zone as well as local time.
- The contract specifies how transition cases are handled.
3. A flight departs at 23:15 on March 1 at -04:00 and arrives at 07:45 UTC on March 2. Calculate elapsed time.
Show hint
Convert departure to UTC, including its date.
Show worked solution
Departure is 03:15 UTC on March 2 after subtracting the negative offset. Arrival at 07:45 UTC is four hours and thirty minutes later; the apparent local clock difference is not the elapsed duration.
Check your solution
- Departure converts to March 2 at 03:15 UTC.
- Elapsed time is 4 hours 30 minutes.
4. Two audit records show the same instant but different offsets. The audit requires preserving both the instant and submitted representation. Design the fields.
Show hint
Instant equality alone discards representation differences.
Show worked solution
Store a normalized instant for ordering and retain the submitted offset or original timestamp separately under the audit contract. Verify equal instants compare equal while the original representations remain distinguishable.
Check your solution
- Both records order at the same instant.
- Their original offsets or timestamp representations remain available.
5. Three same-day records are A=09:00 +01:00, B=09:00 +03:00 and C=06:00 UTC. Sort by instant, then alphabetic ID for ties.
Show hint
Convert each to UTC before applying the tie-breaker.
Show worked solution
A maps to 08:00 UTC; B and C both map to 06:00 UTC. Sort the tied earlier records alphabetically as B then C, followed by A. The secondary ID rule makes output deterministic without pretending equal instants are different.
Check your solution
- B and C tie at 06:00 UTC and appear in ID order.
- A occurs at 08:00 UTC and appears last.
6. An API stores only +01:00 for a weekly 09:00 appointment. Describe a sufficient recurring-schedule contract.
Show hint
A current offset is not a zone rule.
Show worked solution
Store the local recurrence and named zone, plus a policy for invalid and ambiguous occurrences. Resolve each future date using the relevant zone data and retain the resulting instant where auditability is required.
Check your solution
- The named zone and local recurrence are retained.
- Both invalid and ambiguous local-time policies are documented.
7. A synthetic zone skips local 02:00 through 02:59. The product policy rejects missing appointments. Design a test for 02:30 and 03:00.
Show hint
Test the skipped interval and the first valid time afterward.
Show worked solution
Under the stated synthetic rule, reject 02:30 because it does not map to an instant, and accept 03:00 if other constraints pass. Record the zone fixture and policy rather than relying on the host’s current rule database.
Check your solution
- 02:30 is rejected as invalid.
- 03:00 is outside the skipped interval.
8. A synthetic local 01:15 occurs at both +02:00 and +01:00. The policy chooses the later instant. Determine it.
Show hint
Convert both candidates to UTC before choosing.
Show worked solution
The +02:00 occurrence is 23:15 UTC on the preceding date; the +01:00 occurrence is 00:15 UTC on the local date. The later-instant policy therefore chooses +01:00.
Check your solution
- The two candidate instants differ by one hour.
- The +01:00 occurrence is selected.
9. A local date-time is Unspecified and no zone is supplied. Explain why it cannot identify a unique instant.
Show hint
A wall-clock reading lacks an offset or zone resolution.
Show worked solution
The same local reading can map to different UTC instants in different zones, or be invalid or ambiguous at a transition. Require a named zone and resolution policy or an explicit offset before asserting a unique instant.
Check your solution
- The input is not silently interpreted using the server zone.
- The API requires a documented resolution policy.
10. A duration is 1 day 2 hours 15 minutes. Compare Hours with TotalHours.
Show hint
One value is a component; the other includes the full interval.
Show worked solution
Hours is 2 because it is the hour component after the whole day. TotalHours is 26.25, including the day and quarter hour; choose the property matching the reporting contract.
Check your solution
- Hours is 2.
- TotalHours is 26.25.
11. A timer expires at exactly 30 minutes. Design tests immediately before, at and after the boundary.
Show hint
State whether equality is included.
Show worked solution
For an inclusive expiry policy, use fixed elapsed values such as 29:59, 30:00 and 30:01; expect active, expired and expired. Inject the time source or pass durations directly instead of waiting in real time.
Check your solution
- 29:59 is active.
- 30:00 and 30:01 are expired.
12. A machine-readable timestamp requires an offset and invariant fixed format. Design valid and invalid vectors.
Show hint
Validate the entire input rather than accepting a prefix.
Show worked solution
Use a documented TryParseExact format with InvariantCulture, such as a timestamp ending +02:00, and check its Boolean. Test a matching complete input and a missing-offset or trailing-garbage input as rejection under that format.
Check your solution
- The valid complete vector parses.
- Missing offset or trailing garbage is rejected.
13. A parsed offset timestamp is used to infer a region for next year. Why is that inference unsafe?
Show hint
Several zones can share an offset at one instant.
Show worked solution
An offset identifies the instant but not a unique zone or its future transition rules. Require the intended named zone separately for recurrence and test its supported rule data and transition policy.
Check your solution
- The zone is supplied rather than inferred from the offset.
- Future occurrences use the selected zone’s rules.
14. A synthetic local day begins at +02:00 and the next midnight at +01:00. A billing rule charges per calendar day, not 24 hours. Explain the test.
Show hint
Compare the calendar boundary and elapsed interval separately.
Show worked solution
The two midnights span 25 elapsed hours under the fixture but constitute one local calendar day. Assert one calendar-day charge and separately verify the 25-hour interval; do not substitute a fixed duration.
Check your solution
- The billing result is one calendar day.
- The elapsed interval is 25 hours.
15. A scheduled job adds 24 elapsed hours after each run but must always run at 09:00 local. Explain the transition risk and repair.
Show hint
Resolve the next local calendar occurrence under the named zone.
Show worked solution
A clock transition can make 24 elapsed hours land at a different local time. Compute the next local date at 09:00, apply the documented invalid or ambiguous-time policy with the named zone, then resolve its instant.
Check your solution
- Each scheduled occurrence targets 09:00 local under the policy.
- Transition cases have explicit resolution.
16. A duration of 150 minutes is reported using TimeSpan.Minutes. Explain the error and the correct total.
Show hint
Minutes is the component after whole hours.
Show worked solution
The span is two hours and thirty minutes, so Minutes is 30 while TotalMinutes is 150. Use the total property when the contract asks for the whole duration in minutes.
Check your solution
- Minutes is 30.
- TotalMinutes is 150.
17. A measurement uses wall-clock timestamps and the clock is adjusted backward during work. Design a reliable elapsed-time test.
Show hint
Separate elapsed measurement from calendar time.
Show worked solution
Inject an elapsed-time abstraction or use Stopwatch for the interval, and test with a controlled fake that advances elapsed time while wall time changes. Do not infer a negative execution duration from an adjusted wall-clock subtraction.
Check your solution
- The elapsed test is independent of wall-clock adjustment.
- The reported unit and boundary are explicit.
18. An application uses TimeZoneInfo for a local appointment. Design integration evidence without assuming every host has identical zone data.
Show hint
Record the actual zone and environment used.
Show worked solution
Use the application’s supported named zone and fixed transition vectors in an isolated integration environment, record the platform and rule-data assumptions, and assert the chosen invalid/ambiguous policy. Keep synthetic rule tests separate from actual zone-data tests.
Check your solution
- The test records zone and rule-data assumptions.
- Invalid and ambiguous cases use documented expected policies.
19. A DateTimeOffset is converted with ToOffset(+05:00). What must a test check if the application also records the original offset?
Show hint
Instant preservation and representation preservation are different claims.
Show worked solution
Assert that the converted value represents the same UTC instant and has offset +05:00. Separately assert that the stored original offset remains available if the audit contract requires it; ToOffset alone does not retain a zone identity.
Check your solution
- The instant is unchanged and the new offset is +05:00.
- The audit record retains the required original offset separately.
20. A test parses a malformed timestamp but then uses the out value despite TryParseExact returning false. Repair the test.
Show hint
The Boolean determines whether the output represents a successful parse.
Show worked solution
Assert false and follow the documented invalid-input path without using the output as a parsed instant. Add a separate valid fixed-format case that asserts the resulting UTC instant and offset.
Check your solution
- Malformed input reports failure without committing an instant.
- The valid case checks both instant and offset.
Key takeaways
- Instants, local times, and durations are distinct.
- Test offsets and transition policies explicitly.
- An offset identifies an instant but does not encode future zone rules.
- TimeSpan totals and components answer different questions.
- Transition and parsing policies require explicit tests.