简体   繁体   中英

Difference between a DateTime object I create and DateTime.Now

I'm trying to use the Exchange 2007 API to query calendar availability for a specific user. My sample code is producing the following exception:

The time duration specified for FreeBusyViewOptions.TimeWindow is invalid.

Here's the sample code:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

service.AutodiscoverUrl("email@domain.com");

DateTime startTime = new DateTime(2012, 1, 6, 7, 0, 0);

TimeWindow tw = new TimeWindow(startTime, startTime.AddHours(8));

GetUserAvailabilityResults result = service.GetUserAvailability(new List<AttendeeInfo> { new AttendeeInfo("email@domain.com") }, tw, AvailabilityData.FreeBusyAndSuggestions);

The wierd thing is, if I replace my startTime assignment with the following it works:

DateTime startTime = DateTime.Now;

What's the difference between the DateTime object I created and the object produced by DateTime.Now. I've examined them in detail while debugging and can't find a difference.

Any ideas?

This actually appears to be an issue in the GetUserAvailability method as opposed to any DateTime manipulation.

According to the MSDN documentation :

The GetUserAvailability(Generic ,TimeWindow,AvailabilityData,AvailabilityOptions) method supports only time periods that are a minimum of 24 hours long and that begin and end at 12:00a.m. To restrict the results of the method to a shorter time period, you must filter the results on the client.

I find out that the specified TimeWindow must contain at least one midnight. But I do not know why.

Maybe it has something to do with the difference between your time zone and UTC, producing a negative time window. Try increasing from AddHours(8) to bigger values up to AddHours(24) and see what happens.

Specify the Kind to make it identical to Now:

     DateTime startTime = new DateTime(2012, 1, 6, 7, 0, 0, DateTimeKind.Local);

With some odds that you actually need Utc. Depends on server config probably.

The Kind is different. This may be what it is looking for.

new DateTime(2012, 1, 6, 7, 0, 0)

has a kind of "Unspecified".

While

DateTime.Now

has a Kind of "Local".

Try using ToLocalTime to set the kind to local:

DateTime startTime = new DateTime(2012, 1, 6, 7, 0, 0).ToLocalTime();

Look at the constructors and code for the class DateTime.

All of them alter the private variable:

private ulong dateData;

So all the constructors are the same and DateTime.Now is a public static method that returns an instance of the DateTime class that does the same thing.

The error message stated:

The time duration specified for FreeBusyViewOptions.TimeWindow is invalid.

That is because it is invalid!

You put in a future date, and it most likely checked for that. Try with a current date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM