mangstadt / biweekly

biweekly is an iCalendar library written in Java.
BSD 2-Clause "Simplified" License
323 stars 44 forks source link

Disable Zulu time when timezones specified #20

Closed nicky-isaacs closed 8 years ago

nicky-isaacs commented 8 years ago

I have multiple VTIMEZONE objects specified in my calendar, all of which are being used by VEVENTS. I do not want to specify a timezone for the calendar as a whole, but rather on individual events.

Perhaps I am mistaken, but it seems as though specifying a timezone property on a VEvent object should cause it to be serialized without a Zulu time. An example, using Scala but should work same in Java:

val timezoneIds = List("America/Seattle")

// Create some timezones
val timezones: List[VTimezone] = buildTimezones(timezoneIds)

// Create a DateStart
val dtStart = new DateStart(startTime)
val dtEnd = new DateEnd(endTime)

// Create timezone params and add to start and end
val params = new ICalParameters()
params.setTimezoneId(timezoneIds(0)) // Example, use the first timezone
dtStart.setParameters(params)
dtEnd.setParameters(params)

// Add the start and end to an event
val event = new VEvent()
event.setProperty(dtStart)
event.setProperty(dtEnd)

val calendar = new iCalendar()

calendar.addEvent(event)

val writer = new ICalWriter(buffer, ICalVersion.V2_0)

// Add the timezone
writer.getTimezoneInfo.assign(timezones(0), TimeZone.getTimeZone(timezoneIds(0))

// Write to a string
val buffer = new ByteArrayOutputStream()
val written = f(writer)
written.flush()
written.close()
val calString = new String(buffer.toByteArray)

// calStrig now contains a VTimezone object, and VEvent object in the format
// DTSTART;TZID=America/Seattle:20160505T150000Z

There is mention in the "Working with Timezones" page about using icalWriter.getTimezoneInfo().setDefaultTimeZone(tz); to set timezones for the calendar as a whole. I do not need to specify a global timezone as all events have a timezone specified, however if that is the best solution I could specify a bunk timezone to prevent the Zulu time specification.

Hope this makes sense, happy to provide more clarification

mangstadt commented 8 years ago

Nick,

Adding a timezone ID parameter to a property will NOT cause it to be formatted in that timezone. To format a property in a specific timezone, you have to use the writer's TimezoneInfo object.

ICalendar ical = new ICalendar();
VEvent event = new VEvent();
DateStart dtstart = ...
event.setProperty(dtstart);
DateEnd dtend = ...
event.setProperty(dtend);
ical.addEvent(event);

ICalWriter writer = ...
VTimezone vtimezone = ...
TimeZone timezone = TimeZone.getTimeZone(...);
writer.getTimezoneInfo.assign(vtimezone, timezone);
writer.getTimezoneInfo().setTimeZone(dtstart, timezone);
writer.getTimezoneInfo().setTimeZone(dtend, timezone);
writer.write(ical);

As it stands now, there's no way to assign a timezone to an event as a whole. You have to individually assign each of the event's date/time properties to the timezone. But maybe I will add this feature. Thanks for the idea!

nicky-isaacs commented 8 years ago

Mike,

Ah! thanks for the info. Will try that. Will look into a PR to support this

Cheers,

Nick

mangstadt commented 8 years ago

Closing. See https://github.com/mangstadt/biweekly/issues/36