public void TimeZoneFromName()
    {
        var obj  = NSTimeZone.FromName(NSTimeZone.KnownTimeZoneNames[0]);
        var name = obj.Name;

        Assert.AreEqual(obj.Name, name);
    }
    public void NewTimeZone()
    {
        var obj = new NSTimeZone();

        Assert.AreNotEqual(IntPtr.Zero, obj.ClassHandle);
        Assert.AreNotEqual(IntPtr.Zero, obj.Handle);
    }
    public void TimeZoneSame()
    {
        var a = new NSTimeZone();
        var b = Runtime.GetNSObject <NSTimeZone>(a.Handle);

        Assert.AreSame(a, b);
    }
Exemple #4
0
        void UpdateDateFromModel(bool animate)
        {
            if (_picker.Date.ToDateTime().Date != Element.Date.Date)
            {
                _picker.SetDate(Element.Date.ToNSDate(), animate);
            }

            // Can't use Element.Format because it won't display the correct format if the region and language are set differently
            if (string.IsNullOrWhiteSpace(Element.Format) || Element.Format.Equals("d", StringComparison.OrdinalIgnoreCase))
            {
                NSDateFormatter dateFormatter = new NSDateFormatter();
                dateFormatter.TimeZone = NSTimeZone.FromGMT(0);

                if (Element.Format?.Equals("D", StringComparison.Ordinal) == true)
                {
                    dateFormatter.DateStyle = NSDateFormatterStyle.Long;
                    var strDate = dateFormatter.StringFor(_picker.Date);
                    Control.Text = strDate;
                }
                else
                {
                    dateFormatter.DateStyle = NSDateFormatterStyle.Short;
                    var strDate = dateFormatter.StringFor(_picker.Date);
                    Control.Text = strDate;
                }
            }
            else if (Element.Format.Contains('/', StringComparison.Ordinal))
            {
                Control.Text = Element.Date.ToString(Element.Format, CultureInfo.InvariantCulture);
            }
            else
            {
                Control.Text = Element.Date.ToString(Element.Format);
            }
        }
        public override EEARegionStatus ValidateViaTimezone()
        {
            try
            {
                string response = NSTimeZone.GetLocalTimeZoneName();
                Debug.Log("[ValidateViaTimezone]. Response: " + (response ?? "null"));

                if (response == null)
                {
                    return(EEARegionStatus.Unknown);
                }

                if (response.Contains("Europe"))
                {
                    return(EEARegionStatus.InEEA);
                }

                return(EEARegionStatus.NotInEEA);
            }
            catch (Exception e)
            {
                Debug.Log("[ValidateViaTimezone]. Error: " + e.Message);
                return(EEARegionStatus.Unknown);
            }
        }
Exemple #6
0
        public static DateTime ToDateTimeUTC(this NSDate nsdate)
        {
            NSCalendar calendar = NSCalendar.CurrentCalendar;
            var        date     = calendar.Components(NSCalendarUnit.Year | NSCalendarUnit.Month | NSCalendarUnit.Day | NSCalendarUnit.Hour | NSCalendarUnit.Minute | NSCalendarUnit.Second, nsdate);

            date.TimeZone = NSTimeZone.FromName(@"UTC");
            return(new DateTime((int)date.Year, (int)date.Month, (int)date.Day, (int)date.Hour, (int)date.Minute, (int)date.Second, DateTimeKind.Utc));
        }
    public void TimeZoneAbbreviation()
    {
        var obj1 = NSTimeZone.FromName(NSTimeZone.KnownTimeZoneNames[0]);
        var abbr = obj1.Abbreviation();
        var obj2 = NSTimeZone.FromAbbreviation(abbr);

        Assert.AreEqual(obj1.Abbreviation(), obj2.Abbreviation());
    }
        public void TestOrdinality(int year, int month, int day, NSCalendarUnit smaller, NSCalendarUnit larger, int expected)
        {
            var cal  = new NSCalendar(NSCalendarType.Gregorian);
            var date = new DateTime(year, month, day, 0, 0, 0, DateTimeKind.Utc);
            var dt   = (NSDate)date;

            cal.TimeZone = NSTimeZone.FromName("Europe/Madrid");
            Assert.AreEqual((nuint)expected, cal.Ordinality(smaller, larger, dt), $"Ordinality");
        }
 public static NSDateComponents ToNSDateComponents(this DateTimeOffset date)
 => new NSDateComponents()
 {
     Year     = date.Year,
     Minute   = date.Minute,
     Second   = date.Second,
     Hour     = date.Hour,
     Month    = date.Month,
     Day      = date.Day,
     TimeZone = NSTimeZone.FromGMT((nint)date.Offset.TotalSeconds)
 };
        /// <summary>
        /// Converts the UTCT o local time zone.
        /// </summary>
        /// <returns>The UTCT o local time zone.</returns>
        /// <param name="dateTimeUtc">Date time UTC.</param>
        public DateTime ConvertUtcToLocalTimeZone(DateTime dateTimeUtc)
        {
            var sourceTimeZone       = new NSTimeZone("UTC");
            var destinationTimeZone  = NSTimeZone.LocalTimeZone;
            var sourceDate           = DateUtility.DateTimeToNativeDate(dateTimeUtc);
            var sourceGmtOffset      = (int)sourceTimeZone.SecondsFromGMT(sourceDate);
            var destinationGmtOffset = (int)destinationTimeZone.SecondsFromGMT(sourceDate);
            var interval             = destinationGmtOffset - sourceGmtOffset;
            var destinationDate      = sourceDate.AddSeconds(interval);
            var dateTime             = DateUtility.NativeDateToDateTime(destinationDate);

            return(dateTime);
        }
Exemple #11
0
        public void DateByAddingComponentsTest()
        {
            var    cal   = new NSCalendar(NSCalendarType.Gregorian);
            var    now   = (NSDate)DateTime.Now;
            var    comps = new NSDateComponents();
            NSDate date;

            comps.Day      = 2;
            comps.TimeZone = NSTimeZone.FromAbbreviation("UTC");
            cal.TimeZone   = comps.TimeZone;
            date           = cal.DateByAddingComponents(comps, now, NSDateComponentsWrappingBehavior.None);
            Assert.AreEqual(now.SecondsSinceReferenceDate + 3600 * 24 * 2, date.SecondsSinceReferenceDate, "a");
        }
        private static DateTime NSDateToDateTime(MonoTouch.Foundation.NSDate sourceDate)
        {
            NSTimeZone sourceTimeZone = new NSTimeZone("UTC");
            NSTimeZone destinationTimeZone = NSTimeZone.LocalTimeZone;

            int sourceGMTOffset = sourceTimeZone.SecondsFromGMT(sourceDate);
            int destinationGMTOffset = destinationTimeZone.SecondsFromGMT(sourceDate);
            int interval = destinationGMTOffset - sourceGMTOffset;

            var destinationDate = sourceDate.AddSeconds(interval);

            var dateTime = new DateTime(2001, 1, 1, 0, 0, 0).AddSeconds(destinationDate.SecondsSinceReferenceDate);
            return dateTime;
        }
        private void AddEventForReal(DateTime startDate, DateTime endDate, string title, string location, string description, Action <bool> callback, string id)
        {
            var store    = EventStore;
            var calendar = store.DefaultCalendarForNewEvents;

            if (calendar == null)
            {
                callback(false);
                Utils.EnsureInvokedOnMainThread(() =>
                                                new UIAlertView("No Calendars", "This is rather embarrassing for us to tell you, but you don't seem to have a calendar. Please configure a calendar to add session.", null, "OK", null).Show()
                                                );
                return;
            }



            if (EventExists(startDate, title, id))
            {
                callback(true);
                return;
            }

            var newEvent = EKEvent.FromStore(store);

            newEvent.Title        = title;
            newEvent.Notes        = description;
            newEvent.Calendar     = calendar;
            newEvent.TimeZone     = NSTimeZone.FromName("US/Eastern");
            newEvent.StartDate    = startDate.ToNSDate();
            newEvent.EndDate      = endDate.ToNSDate();
            newEvent.AllDay       = true;
            newEvent.Location     = location;
            newEvent.Availability = EKEventAvailability.Busy;
            NSError error;

            store.SaveEvent(newEvent, EKSpan.ThisEvent, true, out error);

            if (error == null)
            {
                callback(true);
            }
            else
            {
                callback(false);
                Utils.EnsureInvokedOnMainThread(() =>
                                                new UIAlertView("Sorry about the mess.", "Something went berserk and we couldn't add the session to your calendar, but it should be fixed now. Try again, please!", null, "OK", null).Show()
                                                );
            }
        }
Exemple #14
0
        public DateTime ConvertUTCToLocalTimeZone(DateTime dateTimeUtc)
        {
            NSTimeZone sourceTimeZone      = new NSTimeZone("UTC");
            NSTimeZone destinationTimeZone = NSTimeZone.LocalTimeZone;
            NSDate     sourceDate          = DateTimeToNativeDate(dateTimeUtc);

            int sourceGMTOffset      = (int)sourceTimeZone.SecondsFromGMT(sourceDate);
            int destinationGMTOffset = (int)destinationTimeZone.SecondsFromGMT(sourceDate);
            int interval             = sourceGMTOffset - destinationGMTOffset;

            var destinationDate = dateTimeUtc.AddSeconds(interval);

            //var destinationDate = sourceDate.AddSeconds(interval);
            //var dateTime = NativeDateToDateTime(destinationDate);
            return(destinationDate);
        }
Exemple #15
0
        public static NSDate ToNSDateUTC(this DateTime date)
        {
            NSCalendar       calendar = NSCalendar.CurrentCalendar;
            NSDateComponents comps    = new NSDateComponents();

            comps.TimeZone = NSTimeZone.FromName(@"UTC");
            comps.Day      = date.Day;
            comps.Month    = date.Month;
            comps.Year     = date.Year;
            comps.Hour     = date.Hour;
            comps.Minute   = date.Minute;
            comps.Second   = date.Second;
            var value = calendar.DateFromComponents(comps);

            return(value);
        }
Exemple #16
0
        public void TestComponentsInTimeZone()
        {
            RequiresIos8();

            NSTimeZone currentZone = NSTimeZone.LocalTimeZone;
            NSTimeZone otherZone   = new NSTimeZone("Asia/Bangkok");

            if (currentZone.SecondsFromGMT(NSDate.Now) == otherZone.SecondsFromGMT(NSDate.Now))
            {
                Assert.Inconclusive("Same time zone, change Asia/Bangkok");
            }

            NSDateComponents components = NSCalendar.CurrentCalendar.ComponentsInTimeZone(otherZone, NSDate.Now);

            Assert.IsTrue(components.Hour != NSCalendar.CurrentCalendar.Components(NSCalendarUnit.Hour, NSDate.Now).Hour, "Different time zones should have different hours");
        }
        public void DateComponentsTest()
        {
            var cal = new NSCalendar(NSCalendarType.Gregorian);
            var now = DateTime.UtcNow;
            NSDateComponents comps;

            cal.TimeZone = NSTimeZone.FromName("UTC");
            comps        = cal.Components(NSCalendarUnit.Year | NSCalendarUnit.Month | NSCalendarUnit.Day, (NSDate)now);
            Assert.AreEqual((nint)now.Year, comps.Year, "a year");
            Assert.AreEqual((nint)now.Month, comps.Month, "a month");
            Assert.AreEqual((nint)now.Day, comps.Day, "a day");

            var dayCompare = now;

            comps = cal.Components(NSCalendarUnit.Hour, (NSDate)dayCompare.AddHours(-1), (NSDate)dayCompare, NSDateComponentsWrappingBehavior.None);
            Assert.AreEqual((nint)1, comps.Hour, "b hour");
        }
Exemple #18
0
        public UIDateField(RectangleF frame) : base(frame)
        {
            this.dateFormat = "D";

            this.picker               = new UIDatePicker();
            this.picker.Mode          = UIDatePickerMode.Date;
            this.picker.TimeZone      = NSTimeZone.FromAbbreviation("GMT");
            this.picker.ValueChanged += this.PickerValueChanged;

            this.label = new UILabel(this.Bounds);
            this.label.AutoresizingMask       = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin;
            this.label.BackgroundColor        = UIColor.Clear;
            this.label.UserInteractionEnabled = false;
            this.AddSubview(this.label);

            this.Date = DateTime.Today;
        }
Exemple #19
0
        protected virtual void SetupDatePicker(UIDatePicker datePicker, UITextField textField)
        {
            datePicker.Frame           = new CGRect(0, 0, DeviceInfo.ScreenWidth, 220);
            datePicker.BackgroundColor = Theme.ColorPalette.Background.ToUIColor();
            datePicker.Mode            = UIDatePickerMode.Date;
            datePicker.TimeZone        = NSTimeZone.FromName(@"UTC");
            datePicker.Calendar        = NSCalendar.CurrentCalendar;
            datePicker.ValueChanged   += (sender, e) =>
            {
                var picker = sender as UIDatePicker;
                if (picker != null)
                {
                    SetDate(picker.Date);
                }
            };

            var accessoryView = new UIView()
                                .WithFrame(0, 0, DeviceInfo.ScreenWidth, 44)
                                .WithBackground(Theme.ColorPalette.Background.ToUIColor())
                                .WithSubviews(
                new UIButton(UIButtonType.Custom)
                .WithFrame(DeviceInfo.ScreenWidth - 90, 0, 90, 44)
                .WithTune(tune =>
            {
                tune.SetupStyle(ThemeConfig.TextButton);
                tune.SetTitle("Готово", UIControlState.Normal);
                tune.TouchUpInside += (sender, e) => textField.EndEditing(true);
            }),
                new UIView().WithFrame(0, 0, DeviceInfo.ScreenWidth, 1).WithBackground(Theme.ColorPalette.Separator.ToUIColor()),
                new UIView().WithFrame(0, 43, DeviceInfo.ScreenWidth, 1).WithBackground(Theme.ColorPalette.Separator.ToUIColor())
                );

            textField.InputView           = datePicker;
            textField.InputAccessoryView  = accessoryView;
            textField.ShouldBeginEditing += (tF) =>
            {
                SetDate(datePicker.Date);
                return(true);
            };

            textField.EditingChanged += (sender, e) =>
            {
                textField.Text = (DataContext as SignUpItemDatePickerViewModel)?.Value ?? string.Empty;
            };
        }
Exemple #20
0
        public void ToUtcDateTime_ForNsDate_ConvertsToUtcDateTime(double secondsSinceNow)
        {
            var nsDate   = NSDate.FromTimeIntervalSinceNow(secondsSinceNow);
            var calendar = NSCalendar.CurrentCalendar;

            calendar.TimeZone = NSTimeZone.FromGMT(0);
            var utcComponents = CreateComponents(nsDate, calendar);

            var dateTime = nsDate.ToUtcDateTime();

            Assert.IsType <DateTime>(dateTime);
            Assert.Equal(DateTimeKind.Utc, dateTime.Kind);
            Assert.Equal(utcComponents.Year, dateTime.Year);
            Assert.Equal(utcComponents.Month, dateTime.Month);
            Assert.Equal(utcComponents.Day, dateTime.Day);
            Assert.Equal(utcComponents.Hour, dateTime.Hour);
            Assert.Equal(utcComponents.Minute, dateTime.Minute);
            Assert.Equal(utcComponents.Second, dateTime.Second);
        }
Exemple #21
0
        public static void ScheduleLocalNotification(MoodPrompt prompt)
        {
            if (prompt != null)
            {
                var keys     = new object[] { "PromptId" };
                var objects  = new object[] { prompt.Id };
                var userInfo = NSDictionary.FromObjectsAndKeys(objects, keys);
                var fireDate = DateTime.SpecifyKind(prompt.ActiveFrom, DateTimeKind.Utc);

                UILocalNotification notification = new UILocalNotification {
                    FireDate       = fireDate.ToLocalTime().ToNSDate(),
                    TimeZone       = NSTimeZone.FromName("UTC"),
                    AlertBody      = prompt.NotificationText,
                    RepeatInterval = 0,
                    AlertAction    = "Set my mood",
                    UserInfo       = userInfo
                };
                UIApplication.SharedApplication.ScheduleLocalNotification(notification);
            }
        }
Exemple #22
0
        public static void UpdateDate(this MauiDatePicker platformDatePicker, IDatePicker datePicker, UIDatePicker?picker)
        {
            if (picker != null && picker.Date.ToDateTime().Date != datePicker.Date.Date)
            {
                picker.SetDate(datePicker.Date.ToNSDate(), false);
            }

            string format = datePicker.Format ?? string.Empty;

            // Can't use VirtualView.Format because it won't display the correct format if the region and language are set differently
            if (picker != null && string.IsNullOrWhiteSpace(format) || format.Equals("d", StringComparison.OrdinalIgnoreCase))
            {
                NSDateFormatter dateFormatter = new NSDateFormatter
                {
                    TimeZone = NSTimeZone.FromGMT(0)
                };

                if (format.Equals("D", StringComparison.Ordinal) == true)
                {
                    dateFormatter.DateStyle = NSDateFormatterStyle.Long;
                    var strDate = dateFormatter.StringFor(picker?.Date);
                    platformDatePicker.Text = strDate;
                }
                else
                {
                    dateFormatter.DateStyle = NSDateFormatterStyle.Short;
                    var strDate = dateFormatter.StringFor(picker?.Date);
                    platformDatePicker.Text = strDate;
                }
            }
            else if (format.Contains('/', StringComparison.Ordinal))
            {
                platformDatePicker.Text = datePicker.Date.ToString(format, CultureInfo.InvariantCulture);
            }
            else
            {
                platformDatePicker.Text = datePicker.Date.ToString(format);
            }

            platformDatePicker.UpdateCharacterSpacing(datePicker);
        }
Exemple #23
0
        protected virtual void SetupDatePickerFor(UIDatePicker datePicker, UITextField textField)
        {
            datePicker.Frame           = new CGRect(0, 0, DeviceInfo.ScreenWidth, 220);
            datePicker.BackgroundColor = Theme.ColorPalette.Background.ToUIColor();

            datePicker.Mode     = UIDatePickerMode.Date;
            datePicker.TimeZone = NSTimeZone.FromName(@"UTC");
            datePicker.Calendar = NSCalendar.CurrentCalendar;

            datePicker.ValueChanged += (sender, e) =>
            {
                var picker = sender as UIDatePicker;
                if (picker != null)
                {
                    var date = picker.Date.ToDateTimeUTC();
                    textField.Text = string.Format(StringExtentionsMethods.StringDate(date), date);
                    textField.SendActionForControlEvents(UIControlEvent.ValueChanged);
                    textField.SendActionForControlEvents(UIControlEvent.EditingChanged);
                }
            };

            var accessoryView = new UIView()
                                .WithFrame(0, 0, DeviceInfo.ScreenWidth, 44)
                                .WithBackground(Theme.ColorPalette.Background.ToUIColor())
                                .WithSubviews(
                new UIButton(UIButtonType.Custom)
                .WithFrame(DeviceInfo.ScreenWidth - 90, 0, 90, 44)
                .WithTune(tune =>
            {
                tune.SetupStyle(ThemeConfig.ProductDetails.DetailsCell.MinMax.DoneButton);
                tune.SetTitle(Mvx.Resolve <ILocalizationService>().GetLocalizableString(ProductsConstants.RESX_NAME, "ProductCard_Done"), UIControlState.Normal);
                tune.TouchUpInside += (sender, e) => textField.EndEditing(true);
            }),
                new UIView().WithFrame(0, 0, DeviceInfo.ScreenWidth, 1).WithBackground(Theme.ColorPalette.Separator.ToUIColor()),
                new UIView().WithFrame(0, 43, DeviceInfo.ScreenWidth, 1).WithBackground(Theme.ColorPalette.Separator.ToUIColor())
                );

            textField.InputView          = datePicker;
            textField.InputAccessoryView = accessoryView;
        }
        private void UpdateGui()
        {
            if (ApplyFixSwitch.On)
            {
                // Local time zone data is cached after CurrentTimeZone is first used to retrieve time zone information.
                // If the system's local time zone subsequently changes, the CurrentTimeZone property does not reflect this change.
                // If you need to handle time zone changes while your application is running, use the TimeZoneInfo class and call its TimeZoneInfo.ClearCachedData method.
                // See: http://msdn.microsoft.com/en-us/library/system.timezone.currenttimezone(v=vs.110).aspx

                TimeZoneInfo.ClearCachedData();
            }

            xamarinTimeZoneLabel.Text  = TimeZoneInfo.Local.StandardName;
            xamarinUtcTimeLabel.Text   = _xamarinTimestamp.ToString("g");
            xamarinLocalTimeLabel.Text = _xamarinTimestamp.ToLocalTime().ToString("g");

            nativeTimeZoneLabel.Text = NSTimeZone.DefaultTimeZone.Abbreviation();

            var utcFormatter = new NSDateFormatter
            {
                DateStyle = NSDateFormatterStyle.Medium,
                TimeStyle = NSDateFormatterStyle.Short,
                TimeZone  = NSTimeZone.FromAbbreviation("UTC")
            };

            nativeUtcTimeLabel.Text = utcFormatter.ToString(_nativeTimestamp);

            var localFormatter = new NSDateFormatter
            {
                DateStyle = NSDateFormatterStyle.Medium,
                TimeStyle = NSDateFormatterStyle.Short,
                TimeZone  = NSTimeZone.SystemTimeZone
            };

            nativeLocalTimeLabel.Text = localFormatter.ToString(_nativeTimestamp);
        }
Exemple #25
0
    public void TimeZoneDispose()
    {
        var obj = new NSTimeZone();

        obj.Dispose();
    }
Exemple #26
0
 protected MvxBaseUIDatePickerTargetBinding(object target, PropertyInfo targetPropertyInfo)
     : base(target, targetPropertyInfo)
 {
     _systemTimeZone = NSTimeZone.SystemTimeZone;
 }
Exemple #27
0
 public virtual void SetTimeZone(NSTimeZone timeZone)
 {
     throw new PlatformNotSupportedException(Constants.WatchKitRemoved);
 }
Exemple #28
0
 public virtual void SetTimeZone(NSTimeZone timeZone)
 {
     throw new PlatformNotSupportedException("The WatchKit framework has been removed from iOS");
 }