[go: up one dir, main page]

Skip to content

Commit

Permalink
Always specify a timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
tbartelmess committed May 16, 2020
1 parent 77e0b1a commit 5ccf29e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Sources/SwiftIcal/Component.swift
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,18 @@ extension VEvent: LibicalComponentConvertible {
func libicalComponent() -> LibicalComponent {
let comp = icalcomponent_new_vevent()

let dtstampProperty = icalproperty_new_dtstamp(dtstamp.icalTime(utc: true))
let dtstampProperty = icalproperty_new_dtstamp(dtstamp.icalTime(timeZone: .utc))
icalcomponent_add_property(comp, dtstampProperty)

let dtstartProperty = icalproperty_new_dtstart(dtstart.date!.icalTime())
let dtstartProperty = icalproperty_new_dtstart(dtstart.date!.icalTime(timeZone: dtstart.timeZone ?? .utc))

if let timezone = dtstart.timeZone {
icalproperty_add_parameter(dtstartProperty, icalparameter_new_tzid(String(cString: icaltimezone_tzid_prefix()!) + timezone.identifier))
}
icalcomponent_add_property(comp, dtstartProperty)

if let dtend = dtend {
let dtendProperty = icalproperty_new_dtend(dtend.date!.icalTime())
let dtendProperty = icalproperty_new_dtend(dtend.date!.icalTime(timeZone: dtend.timeZone ?? .utc))
if let timezone = dtend.timeZone {
icalproperty_add_parameter(dtendProperty, icalparameter_new_tzid(timezone.identifier))
}
Expand All @@ -395,7 +395,7 @@ extension VEvent: LibicalComponentConvertible {
}

icalcomponent_add_property(comp, transparency.libicalProperty())
icalcomponent_add_property(comp, icalproperty_new_created(created.icalTime(utc: true)))
icalcomponent_add_property(comp, icalproperty_new_created(created.icalTime(timeZone: .utc)))

attendees?.forEach({ (attendee) in
icalcomponent_add_property(comp, attendee.libicalProperty())
Expand Down
14 changes: 5 additions & 9 deletions Sources/SwiftIcal/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ extension TimeZone {


extension Date {
func icalTime(in calendar: Calendar = .autoupdatingCurrent, utc: Bool = false) -> icaltimetype {
var timeZone = calendar.timeZone
if utc {
timeZone = TimeZone(secondsFromGMT: 0)!
}
func icalTime(in calendar: Calendar = .autoupdatingCurrent, timeZone: TimeZone) -> icaltimetype {
let components = calendar.dateComponents(in: timeZone, from: self)
let day = components.day ?? 0
let month = components.month ?? 0
Expand All @@ -82,11 +78,11 @@ extension Date {
let minute = components.minute ?? 0
let second = components.second ?? 0

var timezone: UnsafeMutablePointer<icaltimezone>? = nil
if utc {
timezone = icaltimezone_get_utc_timezone()
var zone: UnsafeMutablePointer<icaltimezone>? = nil
if timeZone == .utc {
zone = icaltimezone_get_utc_timezone()
}
return icaltimetype(year: Int32(year), month: Int32(month), day: Int32(day), hour: Int32(hour), minute: Int32(minute), second: Int32(second), is_date: 0, is_daylight: 0, zone: timezone)
return icaltimetype(year: Int32(year), month: Int32(month), day: Int32(day), hour: Int32(hour), minute: Int32(minute), second: Int32(second), is_date: 0, is_daylight: 0, zone: zone)
}
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/SwiftIcal/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
//

import Foundation
extension TimeZone {
static public let utc = TimeZone(identifier: "GMT")!
}

public let SantiagoTimezone = TimeZone(identifier: "America/Santiago")!

extension Date {
/// Convenience to return date components.
///
/// - parameter timezone: `TimeZone` used to generate the date components. By default the current timezone is used.
/// - parameter calendar: `Calendar` used to generate the data components. By default the gregorian calendar is used. Currently SwiftIcal can only supports Gregorian calendars.
public func components(in timezone: TimeZone = SantiagoTimezone,
public func components(in timezone: TimeZone = .utc,
calendar: Calendar = Calendar(identifier: .gregorian)) -> DateComponents {
var components = calendar.dateComponents(in: timezone, from: self)
components.calendar = calendar
Expand Down
10 changes: 5 additions & 5 deletions Tests/SwiftIcalTests/EventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftIcal
import XCTest


let testTimezone = TimeZone(identifier: "America/Santiago")!
let testTimezone = TimeZone(identifier: "Europe/Berlin")!
extension Date {

var components: DateComponents {
Expand Down Expand Up @@ -50,8 +50,8 @@ class EventTests: XCTestCase {
VERSION:2.0
BEGIN:VEVENT
DTSTAMP:19700101T000000Z
DTSTART;TZID=America/Santiago:20200509T110000
DTEND;TZID=America/Santiago:20200509T120000
DTSTART;TZID=Europe/Berlin:20200509T110000
DTEND;TZID=Europe/Berlin:20200509T120000
SUMMARY:Hello World
UID:TEST-UID
TRANSP:OPAQUE
Expand Down Expand Up @@ -79,8 +79,8 @@ class EventTests: XCTestCase {
VERSION:2.0
BEGIN:VEVENT
DTSTAMP:19700101T000000Z
DTSTART;TZID=America/Santiago:20200509T110000
DTEND;TZID=America/Santiago:20200509T120000
DTSTART;TZID=Europe/Berlin:20200509T110000
DTEND;TZID=Europe/Berlin:20200509T120000
SUMMARY:Hello World
UID:TEST-UID
TRANSP:OPAQUE
Expand Down

0 comments on commit 5ccf29e

Please sign in to comment.