Lesson 9
This lesson will show you how to handle date and time. Also we’ll learn how to compare and clone objects well.
Agenda
- DateTime, DateTimeOffset and TimeSpan
- TimeZone and TimeZoneInfo
- Equality Comparison
- Override Equals
- ICloneable
- References
- Presentation
DateTime, DateTimeOffset and TimeSpan
DateTime and DateTimeOffset are immutable structs for representing a date, and optionally, a time. They have a resolution of 100 ns and a range covering the years 0001 through 9999
DateTimeOffset was added in Framework 3.5 and is functionally similar to DateTime. Its distinguishing feature is that it also stores a UTC offset; this allows more meaningful results when comparing values across different time zones.
TypeSpan just represents a time interval:
DateTime.Now is always unique.
TimeZone and TimeZoneInfo
The TimeZone and TimeZoneInfo classes provide information on time zone names UTC offsets, and daylight saving time rules. TimeZoneInfo is the more powerful of the two and was introduced in Framework 3.5. The biggest difference between the two types is that TimeZone lets you access the current local time zone, whereas TimeZoneInfo provides access to all the world’s time zones. Further, TimeZoneInfo exposes a richer (although at times, more awkward) rules-based model for describing daylight saving time
TimeZoneInfo class can return a full list of time zones.
Example on how to gwt some local time in some time zone:
Equality Comparison
Value equality: two values are equivalent in some sense
Referential equality: two references refer to exactly the same object
null-safe equality comparison
The double type’s == operator enforces that one NaN can never equal anything else — even another NaN. This is most natural from a mathematical perspective, and it reflects the underlying CPU behavior
Override Equals
Equals method can be overriden:
ICloneable
.NET Framework has ICloneable in order to implement cloning object functonality. Example:
References
- Datetime vs. DatetimeOffset
- TimeSpan
- TimeZoneInfo
- FindsyStemTimezoneById
- Equals
- Equals Overloading
- IClonable