InitLipton
InitLipton

Reputation: 2453

UILocalNotification Wrong time zone

When creating a new UILocalNotification and scheduling a notification, the time gets changed due to the timezone. I the date i set gets changed by UiLocalNotifcation to the timezone. which i don't want to happen

public static void RegisterLocalNotification(ServiceModel.Types.ParkingTicket parkingTicket)
    {

        if (parkingTicket == null || parkingTicket.ExpiringSoon) return;

        var startDate = parkingTicket.UtcStart.ToLocalTime();

        NSDate nsStartDate = startDate.AddMinutes(parkingTicket.Duration - 10).UtcDateTimeToNSDate();

        var notification = new UILocalNotification
        {
            FireDate = nsStartDate,
            TimeZone = null,
            AlertAction = Resources.Strings.ExtendTicket,
            AlertBody = string.Format
            (Resources.Strings.FormattedExpiringMessage, 
            parkingTicket.TimeLeft.Description(false),
            parkingTicket.Address,parkingTicket.Car.RegistrationNumber),
            RepeatInterval = 0,
            HasAction = true,
            UserInfo = GetDictionaryFromParkingTicket(parkingTicket),
            SoundName = UILocalNotification.DefaultSoundName,
            ApplicationIconBadgeNumber = 1
        };

        UIApplication.SharedApplication.ScheduleLocalNotification(notification);
    }

        public static NSDate UtcDateTimeToNSDate(this DateTime utcDateTime)
    {
        var reference = new DateTime(2001, 1, 1, 0, 0, 0);
        return NSDate.FromTimeIntervalSinceReferenceDate((utcDateTime - reference).TotalSeconds);
    }

I have tried using TimeZone = NSTimeZone.LocalTimeZone.

Upvotes: 1

Views: 178

Answers (1)

Rolf Bjarne Kvinge
Rolf Bjarne Kvinge

Reputation: 19335

You're converting a UTC date to a local date:

var startDate = parkingTicket.UtcStart.ToLocalTime ();

then you're treating the local date as a UTC date, thereby doing the conversion twice:

NSDate nsStartDate = startDate.AddMinutes(parkingTicket.Duration - 10).UtcDateTimeToNSDate();

Just do this instead:

var startDate = parkingTicket.UtcStart;
var nsStartDate = (NSDate) startDate.AddMinutes (parkingTicket.Duration - 10);

The explicit NSDate conversion will do the right thing.

Upvotes: 2

Related Questions