IsAmbiguousTime() public method

public IsAmbiguousTime ( System.DateTime dateTime ) : bool
dateTime System.DateTime
return bool
Ejemplo n.º 1
0
		static internal TimeSpan GetUtcOffsetFromUtc (DateTime time, TimeZoneInfo zone, out Boolean isDaylightSavings, out Boolean isAmbiguousLocalDst)
		{
			isDaylightSavings = false;
			isAmbiguousLocalDst = false;
			TimeSpan baseOffset = zone.BaseUtcOffset;

			if (zone.IsAmbiguousTime (time)) {
				isAmbiguousLocalDst = true;
				return baseOffset;
			}

			return zone.GetUtcOffset (time, out isDaylightSavings);
		}
Ejemplo n.º 2
0
        public static TimeSpan GetUtcOffset(DateTime dateTime, TimeZoneInfo timeZoneInfo)
        {
            // Unlike the default behavior of TimeZoneInfo.GetUtcOffset, it is prefered to choose
            // the DAYLIGHT time when the input is ambiguous, because the daylight instance is the
            // FIRST instance, and time moves in a forward direction.

            TimeSpan offset = timeZoneInfo.IsAmbiguousTime(dateTime)
                ? timeZoneInfo.GetAmbiguousTimeOffsets(dateTime).Max()
                : timeZoneInfo.GetUtcOffset(dateTime);

            return offset;
        }
Ejemplo n.º 3
0
        //This function identify how many periods has a given date
        private int IdentifyPeriodsByDate(DateTime localDate, TimeZoneInfo timeZone)
        {
            DateTime initDate = localDate.Date;
            DateTime initDateNextDay = initDate.AddDays(1);

            //Inicialmente la variable de retorno tendrá el valor 24 ya que son los periodos normales que tienen casi todos los días
            //(excepto los de cambio de hora)
            int ret = 24;

            bool endFor = false;

            for (DateTime date = initDate; date < initDateNextDay && !endFor; date = date.AddMinutes(5)) // La fecha se incrementa de 5 en 5 min debido a que en ciertas
            // zonas geograficas el cambio de hora puede producirse en horas
            // distintas a horas en punto. (Ejemplo: Producirse el cambio a las
            // 15:30 en una cierta zona, a las 17:45 en otra, etc.)
            {
                //Para evitar que al tratar de convertir en Utc salte una excepción acerca de si su tipo es local y falta atributo sourceTimezone
                date = DateTime.SpecifyKind(date, DateTimeKind.Unspecified);

                //Si date es identificado como ambigüo significa que en esa fecha y hora hay cambio de hora (cuando que quita 1 hora) por lo que ese día
                //tendrá 25 periodos (1 más por la hora repedida del cambio de hora)
                if (timeZone.IsAmbiguousTime(date))
                {
                    endFor = true;
                    ret = 25;
                }
                else
                {
                    try
                    {
                        //Se realiza esta conversión a UTC buscando si salta la excepción.
                        //Esto es porque el día que hay cambio de hora (cuando se añade 1 hora) la hora a la que se le añade "no existe"
                        //por lo que al intentar convertirla a UTC saltará la excepción y sabremos asi que ese día solo tiene 23 periodos
                        //(1 menos por la hora que "no existe")
                        TimeZoneInfo.ConvertTimeToUtc(date, timeZone);
                    }
                    catch (Exception)
                    {
                        endFor = true;
                        ret = 23;
                    }

                }
            }

            return ret;
        }
Ejemplo n.º 4
0
        private void ProcessMeters(double totalPower,Plant plant, IList<Meter> meterList, IList<SigmaMeasure> sigmaMeasureList, DateTime date, TimeZoneInfo timeZone, int periodsOfDate, string executionResume)
        {
            foreach (SigmaMeasure sigmameasure in sigmaMeasureList)
            {
                Logger.Info(string.Format("Sigma measures ProdutionDate '{0}' and numPeriod {1}", sigmameasure.ProductionDate, sigmameasure.NumPeriod));
            }

            bool[] periodsWithData = new bool[24];
            foreach (var meter in meterList)
            {
                Logger.Info(string.Format("Analyzing sigma measures of meter ID '{0}' and local date {1}", meter.IdMeter, date.ToShortDateString()));

                //Get list of sigma measures of a meter with id 'idMeter' where sigma measure production date (day, month and year) are equal to 'date' var
                var queryDay = from SigmaMeasure in sigmaMeasureList
                               where SigmaMeasure.ProductionDate.Day == date.Day
                               && SigmaMeasure.ProductionDate.Month == date.Month
                               && SigmaMeasure.ProductionDate.Year == date.Year
                               && SigmaMeasure.Id_Meter.Equals(meter.IdMeter)
                               orderby SigmaMeasure.NumPeriod ascending
                               select SigmaMeasure;

                if (queryDay.Count() == 0)
                {
                    Logger.Warn(string.Format("{0} results", queryDay.Count()));
                    executionResume = string.Format("{0}\n\t{1} results", executionResume, queryDay.Count());
                }
                else
                {
                    if (queryDay.Count()==25)
                        periodsWithData = new bool[25];

                    Logger.Info(string.Format("{0} results", queryDay.Count()));
                    Logger.Debug(string.Format("1.Entro"));
                    foreach (SigmaMeasure sm in queryDay)
                    {
                        periodsWithData[sm.NumPeriod] = true;
                    }
                    Logger.Debug(string.Format("2.Entro"));
                    DateTime localDate = date.Date;
                    localDate = DateTime.SpecifyKind(localDate, DateTimeKind.Unspecified);
                    Logger.Debug(string.Format("Entro en initial date '{0}' for converting to UTC with timezone '{1}'", localDate, timeZone));
                    DateTime initialUtcDate = new DateTime();

                    //If localDate is ambiguous date (in daylight saving time when it is converted to UTC could return 2 datetimes,
                    //.Net method 'ConvertTimeToUtc' return second option. This code lines fix it)
                    if (timeZone.IsAmbiguousTime(localDate))
                    {
                        Logger.Warn(string.Format("Ambiguous local time found: {0}.", localDate));
                        executionResume = string.Format("{0}\n\tAmbiguous local time found: {1}.", executionResume, localDate);
                        initialUtcDate = TimeZoneInfo.ConvertTimeToUtc(localDate, timeZone);
                        initialUtcDate.AddHours(-1);
                        Logger.Warn("Ambiguous local time fixed in UTC");
                        executionResume = string.Format("{0}\n\tAmbiguous local time fixed in UTC", executionResume);
                    }
                    else
                    {
                        Logger.Debug(string.Format("Entro en initial date '{0}' for converting to UTC with timezone '{1}'", localDate,timeZone));
                        initialUtcDate = TimeZoneInfo.ConvertTimeToUtc(localDate, timeZone);
                        Logger.Debug(string.Format("Local date '{0}' has been converted to UTC: '{1}'",localDate,initialUtcDate));
                    }

                    int lastNumPeriod = ProcessSigmaMeasure(totalPower, meter, queryDay, initialUtcDate, localDate, executionResume,plant);

                    for (int i = lastNumPeriod + 1; i < periodsOfDate; i++)
                    {
                        Logger.Warn(string.Format("Missing Period Number {0} in meter with ID '{1}' and local date {2}", i, meter.IdMeter, localDate.ToShortDateString()));
                        executionResume = string.Format("{0}\n\tMissing Period Number {1} in meter with ID '{2}' and local date {3}", executionResume, i, meter.IdMeter, localDate.ToShortDateString());
                    }

                }
            }

            String missingPeriods = "";
            for (int i = 0; i < periodsWithData.Count(); i++)
            {
                if (periodsWithData[i] == false)
                {
                    if (missingPeriods.Equals(""))
                    {
                        missingPeriods = string.Format("{0}", i);
                    }
                    else
                    {
                        missingPeriods = string.Format("{0},{1}", missingPeriods, i);
                    }
                }
            }
            if (!missingPeriods.Equals(""))
            {
                if (!idPlantWritedInReport)
                {
                    _wiseReport.AppendFormat("{0};", plant.Id);
                    idPlantWritedInReport = true;
                }
                _wiseReport.AppendFormat("{0};", date.ToString(CultureInfo.GetCultureInfo("es-ES")));
                _wiseReport.AppendFormat("{0};", missingPeriods);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Rounds the time to the start of the hour in the specified time zone.
        /// </summary>
        /// <param name="time">The time in UTC.</param>
        /// <param name="timeZone">The time zone. (Can be <see langword="null"/>.)</param>
        /// <param name="localTime">Out: The time in the specified time zone.</param>
        /// <returns>
        /// The start of the day in the specified time zone. The returned value is given in UTC.
        /// </returns>
        /// <remarks>
        /// <para>
        /// The method returns <paramref name="time"/> unchanged if <paramref name="timeZone"/> is
        /// <see cref="TimeZoneInfo.Utc"/> or <see langword="null"/>.
        /// </para>
        /// <para>
        /// <strong>Important:</strong> The returned <paramref name="localTime"/> is valid (i.e. has
        /// an equivalent in UTC), but it may be ambiguous (i.e. has more than one equivalent in
        /// UTC)!
        /// </para>
        /// </remarks>
        private static DateTime RoundToHour(DateTime time, TimeZoneInfo timeZone, out DateTime localTime)
        {
            Debug.Assert(time.Kind == DateTimeKind.Utc, "Time is expected to be UTC.");
            if (timeZone == null || timeZone.Equals(TimeZoneInfo.Utc))
            {
                localTime = time;
                return time;
            }

            // Convert time to specified time zone.
            localTime = TimeZoneInfo.ConvertTime(time, timeZone);

            // Add half an hour for rounding.
            localTime = localTime.AddMinutes(30);

#if SILVERLIGHT
            // Silverlight supports only conversion Local <-> UTC:
            localTime = new DateTime(localTime.Year, localTime.Month, localTime.Day, localTime.Hour, 0, 0, DateTimeKind.Local);
            while (timeZone.IsInvalidTime(localTime))
                localTime = localTime.AddHours(1);

            // When switching back from Daylight Saving Time to normal time, the time in the
            // local time zone is ambiguous and can be mapped to different time values in UTC.
            if (timeZone.IsAmbiguousTime(localTime))
            {
                // Map the local time to the time in UTC which is closest to the original value.
                TimeSpan[] offsets = timeZone.GetAmbiguousTimeOffsets(localTime);
                TimeSpan minDistance = TimeSpan.MaxValue;
                DateTime closestTime = new DateTime();
                foreach (var offset in offsets)
                {
                    DateTime timeUtc = localTime - offset;
                    TimeSpan distance = (timeUtc - time).Duration();
                    if (distance < minDistance)
                    {
                        minDistance = distance;
                        closestTime = timeUtc;
                    }
                }

                time = DateTime.SpecifyKind(closestTime, DateTimeKind.Utc);
            }
            else
            {
                time = TimeZoneInfo.ConvertTime(localTime, TimeZoneInfo.Utc);
            }
#else
            localTime = new DateTime(localTime.Year, localTime.Month, localTime.Day, localTime.Hour, 0, 0);
            while (timeZone.IsInvalidTime(localTime))
                localTime = localTime.AddHours(1);

            // When switching back from Daylight Saving Time to normal time, the time in the 
            // local time zone is ambiguous and can be mapped to different time values in UTC.  
            if (timeZone.IsAmbiguousTime(localTime))
            {
                // Map the local time to the time in UTC which is closest to the original value.
                TimeSpan[] offsets = timeZone.GetAmbiguousTimeOffsets(localTime);
                TimeSpan minDistance = TimeSpan.MaxValue;
                DateTime closestTime = new DateTime();
                foreach (var offset in offsets)
                {
                    DateTime timeUtc = localTime - offset;
                    TimeSpan distance = (timeUtc - time).Duration();
                    if (distance < minDistance)
                    {
                        minDistance = distance;
                        closestTime = timeUtc;
                    }
                }

                time = DateTime.SpecifyKind(closestTime, DateTimeKind.Utc);
            }
            else
            {
                time = TimeZoneInfo.ConvertTime(localTime, timeZone, TimeZoneInfo.Utc);
            }
#endif

            return time;
        }