Esempio n. 1
0
 private void setTimeView(TextView tv, TimeConverterOutput tco)
 {
     if (tco.Success)
     {
         tv.Text = tco.TimeOutput.Value.ToString("yyyy-MM-dd HH:mm:ss");
     }
     else
     {
         if (!tco.TimeZoneFound)
         {
             tv.Text = "Invalid Time Zone Name!";
         }
         else
         {
             tv.Text = tco.Exception.Message;
         }
     }
 }
        public static TimeConverterOutput ConvertTime(DateTime inDate, string TimeZoneName)
        {
            TimeConverterOutput result = new TimeConverterOutput();

            try
            {
                DateTime     utc           = inDate.ToUniversalTime();
                TimeZoneInfo tzi           = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneName);
                DateTime     convertedTime = TimeZoneInfo.ConvertTimeFromUtc(utc, tzi);

                result = new TimeConverterOutput
                {
                    TimeOutput    = convertedTime,
                    Success       = true,
                    Exception     = null,
                    IsDST         = tzi.IsDaylightSavingTime(convertedTime),
                    TimeZoneFound = true
                };
            }catch (TimeZoneNotFoundException exc)
            {
                result = new TimeConverterOutput
                {
                    TimeOutput    = null,
                    Success       = false,
                    Exception     = exc,
                    IsDST         = false,
                    TimeZoneFound = false
                };
            }
            catch (Exception exc)
            {
                result = new TimeConverterOutput
                {
                    TimeOutput    = null,
                    Success       = false,
                    Exception     = exc,
                    IsDST         = false,
                    TimeZoneFound = true
                };
            }
            return(result);
        }
Esempio n. 3
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            Android.Support.V7.Widget.Toolbar toolbar = FindViewById <Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);

            FloatingActionButton fab = FindViewById <FloatingActionButton>(Resource.Id.fab);

            fab.Click += FabOnClick;

            TextView tvPacific  = FindViewById <TextView>(Resource.Id.pacTime);
            TextView tvMountain = FindViewById <TextView>(Resource.Id.mtnTime);
            TextView tvCentral  = FindViewById <TextView>(Resource.Id.ctrlTime);
            TextView tvEastern  = FindViewById <TextView>(Resource.Id.eastTime);
            TextView tvUTC      = FindViewById <TextView>(Resource.Id.utcTime);

            System.Timers.Timer Timer1 = new System.Timers.Timer();
            Timer1.Start();
            Timer1.Interval = 1000;
            Timer1.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
            {
                DateTime utcNow = DateTime.UtcNow;
                tvUTC.Text = utcNow.ToString("yyyy-MM-dd HH:mm:ss");

                TimeConverterOutput pacTime = TimeConverter.ConvertTime(utcNow, "America/Los_Angeles");
                TimeConverterOutput mtnTime = TimeConverter.ConvertTime(utcNow, "America/Boise");
                TimeConverterOutput ctlTime = TimeConverter.ConvertTime(utcNow, "America/Chicago");
                TimeConverterOutput estTime = TimeConverter.ConvertTime(utcNow, "America/New_York");

                RunOnUiThread(() =>
                {
                    setTimeView(tvPacific, pacTime);
                    setTimeView(tvMountain, mtnTime);
                    setTimeView(tvCentral, ctlTime);
                    setTimeView(tvEastern, estTime);
                });
                //Delete time since it will no longer be used.
            };
            Timer1.Enabled = true;
        }