public static bool TryParse(DateTime?startDate, DateTime?endDate, out SharedCalendarEvent?calendarEvent) { if (startDate.HasValue && endDate.HasValue) { DateTime StartTime = startDate.Value.ToUniversalTime(); DateTime EndTime = endDate.Value.ToUniversalTime(); int MoonMonth; MoonPhase MoonPhase; DateTime PhaseStartTime; DateTime PhaseEndTime; double ProgressToFullMoon; DateTime NextFullMoonTime; PhaseCalculator.DateTimeToMoonPhase(StartTime, out MoonMonth, out MoonPhase, out PhaseStartTime, out PhaseEndTime, out ProgressToFullMoon, out NextFullMoonTime); if (PhaseStartTime == StartTime && PhaseEndTime == EndTime) { calendarEvent = new SharedCalendarEvent(MoonPhase, MoonMonth, PhaseStartTime, PhaseEndTime); return(true); } } calendarEvent = null; return(false); }
private bool ReadExistingEvents(out List <SharedCalendarEvent> existingEvents) { existingEvents = new List <SharedCalendarEvent>(); try { // Create Google Calendar API service. using CalendarService service = new CalendarService(new BaseClientService.Initializer() { HttpClientInitializer = CredentialToken, ApplicationName = ApplicationName }); // Define parameters of requestList. EventsResource.ListRequest requestList = service.Events.List(SelectedCalendarEntry.Id); requestList.TimeMin = DateTime.Now - TimeSpan.FromHours(1); requestList.ShowDeleted = false; requestList.SingleEvents = true; requestList.MaxResults = 100; requestList.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime; // List events. Events events = requestList.Execute(); if (events.Items == null) { return(false); } foreach (var eventItem in events.Items) { DateTime?StartDate = eventItem.Start.DateTime; DateTime?EndDate = eventItem.End.DateTime; if (SharedCalendarEvent.TryParse(StartDate, EndDate, out SharedCalendarEvent? NewEvent) && NewEvent != null) { existingEvents.Add(NewEvent); } } return(true); } catch (Exception e) { Debug.Print(e.Message); } return(false); }
public void PostSharedEvents(ICollection <MushroomInfo> mushroomInfoList) { if (mushroomInfoList == null) { return; } if (!IsEventActive) { return; } if (CredentialToken == null) { ObtainCredentialToken(true); } if (CredentialToken == null || SelectedCalendarEntry == SharedCalendarEntry.None) { return; } if (!ReadExistingEvents(out List <SharedCalendarEvent> ExistingEvents)) { return; } DateTime Now = MainWindow.Now(); DateTime NextEventTime = ExistingEvents.Count > 0 ? ExistingEvents[ExistingEvents.Count - 1].PhaseEndTime + TimeSpan.FromHours(1) : Now; DateTime MaxEventTime = Now + TimeSpan.FromDays(UpcomingDays); List <SharedCalendarEvent> MissingEvents = new List <SharedCalendarEvent>(); for (;;) { int MoonMonth; MoonPhase MoonPhase; DateTime PhaseStartTime; DateTime PhaseEndTime; double ProgressToFullMoon; DateTime NextFullMoonTime; PhaseCalculator.DateTimeToMoonPhase(NextEventTime, out MoonMonth, out MoonPhase, out PhaseStartTime, out PhaseEndTime, out ProgressToFullMoon, out NextFullMoonTime); if (PhaseStartTime >= MaxEventTime) { break; } PhaseStartTime = new DateTime(PhaseStartTime.Year, PhaseStartTime.Month, PhaseStartTime.Day, PhaseStartTime.Hour, 0, 0, PhaseStartTime.Kind); PhaseEndTime = new DateTime(PhaseEndTime.Year, PhaseEndTime.Month, PhaseEndTime.Day, PhaseEndTime.Hour, 0, 0, PhaseEndTime.Kind); SharedCalendarEvent NewEvent = new SharedCalendarEvent(MoonPhase, MoonMonth, PhaseStartTime, PhaseEndTime); MissingEvents.Add(NewEvent); NextEventTime = PhaseEndTime + TimeSpan.FromHours(1); } if (MissingEvents.Count > 0) { WriteMissingEvents(MissingEvents, mushroomInfoList); } }