Skip to content

Mistake in sample for DateTimeOffset.Implicit #2355

@Crown0815

Description

@Crown0815

In the documentation DateTimeOffset.Implicit there is an example showing the various kinds of implicit conversions. The example looks like this:

DateTimeOffset timeWithOffset; 
timeWithOffset = new DateTime(1008, 7, 3, 18, 45, 0);
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = DateTime.UtcNow;
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = DateTime.SpecifyKind(DateTime.Now, 
                                      DateTimeKind.Unspecified);
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = new DateTime(2008, 1, 1, 2, 30, 0) + 
                 new TimeSpan(1, 0, 0, 0);
Console.WriteLine(timeWithOffset.ToString());
// The example produces the following output if run on 3/20/2007 
// at 6:25 PM on a computer in the U.S. Pacific Daylight Time zone:
//       7/3/2008 6:45:00 PM -07:00
//       3/21/2007 1:25:52 AM +00:00
//       3/20/2007 6:25:52 PM -07:00
//       1/2/2008 2:30:00 AM -08:00      

However there appear to be multiple mistakes in this example:

  1. In line 2 the year in code is set as 1008, while in the supposed output it should be 2008
  2. In the last line the output is given to be 1/2/2008 2:30:00 AM -08:00, although the expected output should be 1/2/2008 2:30:00 AM -07:00 in my opinion.

Therefore, the actual example should read:

DateTimeOffset timeWithOffset; 
timeWithOffset = new DateTime(2008, 7, 3, 18, 45, 0);
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = DateTime.UtcNow;
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = DateTime.SpecifyKind(DateTime.Now, 
                                      DateTimeKind.Unspecified);
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = new DateTime(2008, 1, 1, 2, 30, 0) + 
                 new TimeSpan(1, 0, 0, 0);
Console.WriteLine(timeWithOffset.ToString());
// The example produces the following output if run on 3/20/2007 
// at 6:25 PM on a computer in the U.S. Pacific Daylight Time zone:
//       7/3/2008 6:45:00 PM -07:00
//       3/21/2007 1:25:52 AM +00:00
//       3/20/2007 6:25:52 PM -07:00
//       1/2/2008 2:30:00 AM -07:00      

Unfortunately I was not able to find the original source file for this example, otherwise I would have made the changes in a pull request already.

Metadata

Metadata

Assignees

Labels

doc-bugProblem with the content; needs to be fixed

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions