Android Question adding an entry to Calendar

Colin Evans

Active Member
Licensed User
Longtime User
Hi, having difficulty understanding my problem and why it appears.
Basically, I'm adding an entry to my google calendar being in the UK the BST (British Summer Time) begins on the 27th of March until 30th October 2022, if I create a diary entry before the 27th of March the entry is added correctly, as is an entry added after 30th October but I add one between these dates it adds the entry to the day before, i.e. if I add one for today (16th April) the entry adds to the 15th April.

The save module is below, and I'm struggling to understand why this is happening, any help greatly received

B4X:
Sub btnSave_Click
    Dim chkDay As Int
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat =  "HH:mm"
    Dim ChkDate As String = DateTime.Date(Main.DateRet)
    If chkAllDay.Checked = True Then
        Dim    tfrom As Long =  Main.DateRet
        Dim tTo As Long = Main.DateRet
        chkDay=1
    Else
        If btnTimeFrom.xLBL.Text = "" Then 'needs better error checking
            Msgbox("No time entered or All day not checked", "Time(s) or All day required")
            Return
        End If
        Dim    tfrom As Long =  DateTime.datetimeParse(ChkDate,btnTimeFrom.xLBL.Text)
        Dim tTo As Long = DateTime.datetimeParse(ChkDate,btnTimeTo.xLBL.Text)
        chkDay=0
    End If
    Log("chkDate " & ChkDate  & " tTo " & tTo & " tfrom " & tfrom)
    Dim econValues As List
    econValues.Initialize
    econValues.Add(Main.wmCalendar1.econ.ALL_DAY)
    econValues.Add(Main.wmCalendar1.econ.DESCRIPTION)
    econValues.Add(Main.wmCalendar1.econ.DTEND)
    econValues.Add(Main.wmCalendar1.econ.DTSTART)
    econValues.Add(Main.wmCalendar1.econ.TITLE)
    econValues.Add(Main.wmCalendar1.econ.ORGANIZER)
    econValues.Add(Main.wmCalendar1.econ.LOCATION)
    econValues.Add(Main.wmCalendar1.econ.EVENT_END_TIMEZONE)
    econValues.Add(Main.wmCalendar1.econ.EVENT_TIMEZONE)
        Dim theEvent As EventInfo
        theEvent.Initialize
        theEvent.allDay = chkDay
        theEvent.description = txtDescription.Text
        theEvent.visible = 1
        theEvent.dateTimeEnd = tTo
        theEvent.dateTimeStart = tfrom
        theEvent.title = txtTitle.Text
        theEvent.organizer = txtOrganiser.Text
        theEvent.location = txtLocation.Text
        theEvent.timeZone = "UTC"
        theEvent.endTimeZone = "UTC"
        Log("the event " & theEvent)
    Dim newEventID As Int = Main.wmCalendar1.AddEvent(Main.ScalendarID, theEvent, econValues)
    If newEventID < 0 Then
        Msgbox("The Entry could not be added", "DITL Calendar")
    Else
        
        Msgbox("The Entry has been added", "DITL Calendar")
    End If
    StartActivity(Main)
End Sub
 

walt61

Active Member
Licensed User
Longtime User
Hi @Colin Evans , I think it's a timezone issue. You're specifying 'UTC' as timezone, and your calendar will display the data as per the device locale (I read somewhere that it stores all data as UTC and then handles the display that way). I suggest to first test with timezone values from https://unicode-org.github.io/cldr-staging/charts/37/supplemental/zone_tzid.html, e.g. 'GMT' or 'Europe/London' (I don't know which one would be accepted) and see how that turns out.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi @Colin Evans , I think it's a timezone issue. You're specifying 'UTC' as timezone, and your calendar will display the data as per the device locale (I read somewhere that it stores all data as UTC and then handles the display that way). I suggest to first test with timezone values from https://unicode-org.github.io/cldr-staging/charts/37/supplemental/zone_tzid.html, e.g. 'GMT' or 'Europe/London' (I don't know which one would be accepted) and see how that turns out.
Hi, many thanks for your reply, I have tried various timezones but to no avail, i.e. GMT or GB, I believe the problem is to do with my configuration of saving an all-day event, if I put two times in it saves okay to the correct day but when I select an all-day event it saves to the previous day, so not sure what I should be saving the event as, I will continue to trial and error, or get rid of the all-day option :)
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Bummer :) I have (potential) good news (for you) and bad news (for me): on my device (timezone CET) the behaviour is the same so there must indeed be something fishy going on. I'll try some more tests - could take a couple of days - and will come back!
Thanks, I will try also and will update if I find a solution, thanks again for taking the time to have a look
 
Upvote 0

walt61

Active Member
Licensed User
Longtime User

Attachments

  • wmCalendarDemo.zip
    21.9 KB · Views: 160
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Found it ! See attached project (which I'll also attach to my original thread's first post). As per https://developer.android.com/reference/android/provider/CalendarContract.Events#writing-to-events : "If allDay is set to 1 eventTimezone must be "UTC" and the time must correspond to a midnight boundary." Methods 'TodayStartAsUTCticks' and 'ButtonAddEvent_Click' have been updated to reflect this and now I'm getting the event in my calendar for 'today' instead of 'yesterday'.
Brilliant, just looked at your code and it's very easy to follow and obviously solves my problem, thank you very much, I will update my code in the share section once I've finished it off, thanks again:)
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
The latest code attached still needs a bit of work but getting there slowly, many thanks walt61 wouldn't have got this far without your help
 

Attachments

  • ditl calendar test.zip
    174.6 KB · Views: 166
Upvote 0
Top