Search code examples
c#apijson.netinvision-power-board

Using an API to auto-post an event, Date and time are incorrect


I am using c# to consume an API for a Invision Board Forum. The idea is that the application will be used to post several events at once, instead of just doing one at a time.

I am using json.net to build my request, the user i am posting as has their timezone set as UTC, and i convert the date+time of the event to UTC as well. I have looked at the request parameters and the conversion at this point is correct.

When the event gets posted, the time is incorrect. For example, an event that should have been posted as happening on June 6 at 6pm, is posted on June 7 at 3am.

My question is the following: I am not sure at which point the time is getting converted. I should mention that the calendar has an auto-converter where it shows you the events in your local time, however as i mentioned before, the user i am posting as has their time zone set as UTC. I tried setting my local time as UTC as well but i don't know if i should know the time zone of our server (that would be quite difficult, as we are using a hosting service) or what is missing so the time posted is the correct one.

Here is a snippet where i handle the date:

date = Convert.ToDateTime(date1) + time;           
DateTime dateConverted = date.ToUniversalTime();  
string d1=dateConverted.ToString("o", CultureInfo.InvariantCulture);
dt.Rows.Add(d1, events[b]); 
request.AddParameter("calendar", calendarId); 
request.AddParameter("title", row["EVENT"]+ "["+eventName+"]"); 
request.AddParameter("description", eventName);
request.AddParameter("start",row["DATE"]); 

And a link to the InvisionBoard api documentation: https://invisioncommunity.com/developers/rest-api?endpoint=calendar/events/POSTindex

thanks for reading :)

Edit: I built the date now like this:

DateTime dateF = new DateTime(2020, date.Month, date.Day, time.Hours, time.Minutes,0); 

And it gives me a different time (Thanks jakdep, i bounced this problem with a lot of people and no one caught the 'why are you adding time' thing). Still, if i post, say, 11am, i convert it to UTC but it shows me 11am on the calendar, which is the original time (in another time zone), not 2pm which is what i would have to see in my timezone :/


Solution

  • EDIT: Yay, finally works. What i did is convert the time between the time of the event and my local time, and from then convert to UTC:

    DateTime cdateF = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dateF,sourceTimeZone, tzone);
    DateTime dateConverted = cdateF.ToUniversalTime();
    

    It WAS taking the timezone from where i am and converted from there, which is why even with the UTC conversion, the time kept being wrong. It is now posting the correct time :D Thanks for the help :D