当前位置: 首页>>代码示例>>C#>>正文


C# TimeZoneInfo.GetUtcOffset方法代码示例

本文整理汇总了C#中System.TimeZoneInfo.GetUtcOffset方法的典型用法代码示例。如果您正苦于以下问题:C# TimeZoneInfo.GetUtcOffset方法的具体用法?C# TimeZoneInfo.GetUtcOffset怎么用?C# TimeZoneInfo.GetUtcOffset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.TimeZoneInfo的用法示例。


在下文中一共展示了TimeZoneInfo.GetUtcOffset方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Dump

        private static void Dump(TimeZoneInfo zone, Options options, TextWriter writer)
        {
            writer.Write($"{zone.Id}\n");

            // This will be a bit odd using Windows time zones, as most have permanent
            // daylight saving rules... but for tz data, it should be okay.
            var initial = new DateTimeOffset(2, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
            var initialOffset = zone.GetUtcOffset(initial);
            var initialDaylight = zone.IsDaylightSavingTime(initial);
            writer.Write("Initially:           {0} {1} {2}\n",
                (initialOffset.Ticks >= 0 ? "+" : "-") + initialOffset.ToString("hh':'mm':'ss", CultureInfo.InvariantCulture),
                initialDaylight ? "daylight" : "standard",
                initialDaylight ? zone.DaylightName : zone.StandardName);

            int fromYear = options.FromYear ?? 1800;
            DateTimeOffset start = new DateTimeOffset(fromYear, 1, 1, 0, 0, 0, TimeSpan.Zero);
            DateTimeOffset end = new DateTimeOffset(options.ToYear, 1, 1, 0, 0, 0, TimeSpan.Zero);

            DateTimeOffset? transition = GetNextTransition(zone, start.AddTicks(-1), end);
            while (transition != null)
            {
                var offset = zone.GetUtcOffset(transition.Value);
                var isDaylight = zone.IsDaylightSavingTime(transition.Value);
                // It's unfortunate that TimeZoneInfo doesn't support the idea of different names
                // for different periods in history. Never mind - this is better than nothing,
                // for diagnostic purposes.
                writer.Write("{0} {1} {2} {3}\n",
                    transition.Value.ToString("yyyy-MM-dd HH:mm:ss'Z'", CultureInfo.InvariantCulture),
                    (offset.Ticks >= 0 ? "+" : "-") + offset.ToString("hh':'mm':'ss", CultureInfo.InvariantCulture),
                    isDaylight ? "daylight" : "standard",
                    isDaylight ? zone.DaylightName : zone.StandardName);
                transition = GetNextTransition(zone, transition.Value, end);
            }
            writer.Write("\n");
        }
开发者ID:nodatime,项目名称:tzvalidate,代码行数:35,代码来源:Program.cs

示例2: GetUtcOffset

        /// <summary>
        /// TimeZoneInfo.GetUtcOffset(DateTimeOffset) is not supported under mono
        /// </summary>
        /// <param name="dateTimeOffset"></param>
        /// <param name="timeZoneInfo"></param>
        /// <returns></returns>
        public static TimeSpan GetUtcOffset(DateTimeOffset dateTimeOffset, TimeZoneInfo timeZoneInfo)
        {
            if (QuartzEnvironment.IsRunningOnMono)
            {
                return timeZoneInfo.GetUtcOffset(dateTimeOffset.UtcDateTime);
            }

            return timeZoneInfo.GetUtcOffset(dateTimeOffset);
        }
开发者ID:quartznet,项目名称:quartznet,代码行数:15,代码来源:TimeZoneUtil.cs

示例3: GetGMTOffset

		/// <summary>
		/// This method will return a signed four digit integer indicating the
		/// GMT offset for the given TimeZoneInfo when applied to the given DateTime.
		/// This is the HL7 Offset format in integer representation.
		/// </summary>
		public static int GetGMTOffset(TimeZoneInfo timeZone, DateTime time)
		{
			var gmtOffSet = timeZone.GetUtcOffset(time);

			//return the offset value HL7 format
			return gmtOffSet.Hours*100 + gmtOffSet.Minutes;
		}
开发者ID:RickIsWright,项目名称:nHapi,代码行数:12,代码来源:DataTypeUtil.cs

示例4: DetectStandardOffset

 private static TimeSpan DetectStandardOffset(TimeZoneInfo zone, TimeZoneInfo.AdjustmentRule rule)
 {
     var offset = zone.GetUtcOffset(rule.DateStart);
     if (zone.IsDaylightSavingTime(rule.DateStart))
     {
         offset -= rule.DaylightDelta;
     }
     return offset;
 }
开发者ID:mattwarren,项目名称:DemoCode,代码行数:9,代码来源:MainForm.cs

示例5: CalcTimeZoneOffset

        public static double CalcTimeZoneOffset(TimeZoneInfo tzi, DateTime userNow, out string timezoneName)
        {
            double offset = tzi.GetUtcOffset(userNow).TotalHours;
            var hour = (int)Math.Truncate(offset);
            string oldname = tzi.DisplayName;
            oldname = oldname.Remove(0, oldname.IndexOf(')') + 1);
            timezoneName = "(UTC" + hour.ToString("+00;-00") + ":" + ((int)((offset - hour) * 60)).ToString("00") + ") " + oldname;

            return offset;
        }
开发者ID:ashish-antil,项目名称:Products,代码行数:10,代码来源:TimeUtils.cs

示例6: TimeZoneToXml

 public static XElement TimeZoneToXml(TimeZoneInfo aTimeZoneInfo, DateTime aNow)
 {
     return new XElement("timezone",
         new XElement("name", aTimeZoneInfo.DisplayName),
         new XElement("daylightName", aTimeZoneInfo.DaylightName),
         new XElement("standardName", aTimeZoneInfo.StandardName),
         new XElement("hasDaylight", aTimeZoneInfo.SupportsDaylightSavingTime ? "yes" : "no"),
         new XElement("currentOffset", OffsetToString(aTimeZoneInfo.GetUtcOffset(aNow))),
         new XElement("isDaylight", aTimeZoneInfo.IsDaylightSavingTime(aNow) ? "yes" : "no"));
 }
开发者ID:weeble,项目名称:ohos,代码行数:10,代码来源:TimeZoneSerializer.cs

示例7: ServerTimeZoneInfo

 public ServerTimeZoneInfo(TimeZoneInfo timeZone, TimeZoneInfo localTimeZone, DateTime now, DateTime utcNow)
 {
     ServerTimeZone = string.Format(CultureInfo.InvariantCulture, "{0} ({1})",
                                    localTimeZone.StandardName,
                                    localTimeZone.GetUtcOffset(now));
     ServerTime = now.ToString("yyyy/MM/dd hh:mm tt", CultureInfo.InvariantCulture);
     ServerUtcTime = utcNow.ToString("yyyy/MM/dd hh:mm tt", CultureInfo.InvariantCulture);
     CurrentTime = TimeZoneInfo.ConvertTimeFromUtc(utcNow, timeZone).ToString("yyyy/MM/dd hh:mm tt",
                                                                              CultureInfo.InvariantCulture);
 }
开发者ID:ChrisPelatari,项目名称:SubText,代码行数:10,代码来源:ServerTimeZoneInfo.cs

示例8: ConvertDateTimeOffsetTo

        public DateTimeOffset ConvertDateTimeOffsetTo(TimeZoneInfo timeZoneInfo, DateTimeOffset dateTimeOffset, int hour = 0, int minute = 0, int second = 0)
        {
            return new DateTimeOffset(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, hour, minute, second, timeZoneInfo.GetUtcOffset(dateTimeOffset));

            //both of these implemenations lose the ability to specificy the hour, minute and second unless the given dateTimeOffset value being passed into this method
            //already has those values set
            //1.
            //return TimeZoneInfo.ConvertTime(dateTimeOffset, timeZoneInfo);
            //2.
            //var timeSpan = timeZoneInfo.GetUtcOffset(dateTimeOffset);
            //return dateTimeOffset.ToOffset(timeSpan);
        }
开发者ID:ChrisDobby,项目名称:allReady,代码行数:12,代码来源:DateTimeOffsetConverter.cs

示例9: ToRfc2822Date

        ///<summary>
        ///Converts a regular DateTime to a RFC822 date string.
        ///</summary>
        ///<returns>The specified date formatted as a RFC822 date string.</returns>
        public static string ToRfc2822Date(this DateTime date,TimeZoneInfo timeZoneInfo)
        {
            int offset = timeZoneInfo.GetUtcOffset(date).Hours;
            string timeZone = "+" + offset.ToString().PadLeft(2, '0');

            if (offset < 0)
            {
                int i = offset * -1;
                timeZone = "-" + i.ToString().PadLeft(2, '0');
            }

            return date.ToString("ddd, dd MMM yyyy HH:mm:ss" + timeZone.PadRight(5, '0'), CultureInfo.InvariantCulture);
        }
开发者ID:AlexandreRoba,项目名称:InsidedApiPoc,代码行数:17,代码来源:DateTimeExtensions.cs

示例10: GetNextTransition

 private static DateTimeOffset? GetNextTransition(TimeZoneInfo zone, DateTimeOffset start, DateTimeOffset end)
 {
     TimeSpan startOffset = zone.GetUtcOffset(start);
     bool startDaylight = zone.IsDaylightSavingTime(start);
     DateTimeOffset now = start.AddDays(1);
     while (now <= end)
     {
         if (zone.GetUtcOffset(now) != startOffset || zone.IsDaylightSavingTime(now) != startDaylight)
         {
             // Right, so there's a transition strictly after now - (one day), and less than or equal to now. Binary search...
             long upperInclusiveTicks = now.Ticks;
             long lowerExclusiveTicks = now.AddDays(-1).Ticks;
             while (upperInclusiveTicks > lowerExclusiveTicks + 1)
             {
                 long candidateTicks = (upperInclusiveTicks + lowerExclusiveTicks) / 2;
                 var candidateDto = new DateTimeOffset(candidateTicks, TimeSpan.Zero);
                 if (zone.GetUtcOffset(candidateDto) != startOffset || zone.IsDaylightSavingTime(candidateDto) != startDaylight)
                 {
                     // Still seeing a difference: look earlier
                     upperInclusiveTicks = candidateTicks;
                 }
                 else
                 {
                     // Same as at start of day: look later
                     lowerExclusiveTicks = candidateTicks;
                 }
             }
             // If we turn out to have hit the end point, we're done without a final transition.
             return upperInclusiveTicks == end.Ticks
                 ? (DateTimeOffset?)null
                 : new DateTimeOffset(upperInclusiveTicks, TimeSpan.Zero);
         }
         now = now.AddDays(1);
     }
     return null;
 }
开发者ID:nodatime,项目名称:tzvalidate,代码行数:36,代码来源:Program.cs

示例11: ReadStringWithTimeZone

 public static DateTimeOffset ReadStringWithTimeZone(DateTime EnteredDate, TimeZoneInfo tzi)
 {
     DateTimeOffset cvUTCToTZI = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, tzi);
     DateTimeOffset cvParsedDate = DateTimeOffset.MinValue;
     DateTimeOffset.TryParse(EnteredDate + " " + cvUTCToTZI.ToString("zzz"), out cvParsedDate);
     if (tzi.SupportsDaylightSavingTime)
     {
         TimeSpan getDiff = tzi.GetUtcOffset(cvParsedDate);
         string MakeFinalOffset = (getDiff.Hours < 0 ? "-" : "+") + (getDiff.Hours > 9 ? "" : "0") + getDiff.Hours + ":" + (getDiff.Minutes > 9 ? "" : "0") + getDiff.Minutes;
         DateTimeOffset.TryParse(EnteredDate + " " + MakeFinalOffset, out cvParsedDate);
         return cvParsedDate;
     }
     else
     {
         return cvParsedDate;
     }
 }
开发者ID:yngvebn,项目名称:ArtistBasePage,代码行数:17,代码来源:Program.cs

示例12: Expression4Handler

        /// <summary>
        /// Handles with a given text matches the Expression4 field.
        /// </summary>
        /// <param name="nameValueCollection">A collection of values from the named capture groups.</param>
        /// <param name="results">The results.</param>
        private void Expression4Handler(NameValueCollection nameValueCollection, TimeZoneInfo timeZone, TextToScheduleResults results)
        {
            // every [n] (days|weeks|months|years) (from [date]) (at [time])

            string amountString = nameValueCollection["AMOUNT"];
            string intervalString = nameValueCollection["INTERVALUNIT"];

            var dateSpec = nameValueCollection["DATESPEC"];
            var dayOfWeekSpecs = nameValueCollection.GetValues("DAYOFWEEK");
            var timesToFire = nameValueCollection.GetValues("TIME");

            DateTime? triggerStartTime = null;

            if (dayOfWeekSpecs != null && GrammarHelper.GetIntervalUnitValueFromString(intervalString) != IntervalUnit.Week)
            {
                throw new Exception("You should only specify a day of the week, when you are using the \"week\" time interval.");
            }

            if (timesToFire == null) // if times are not specified assume midnight
                timesToFire = new string[] { "00:00" };

            foreach (var timeString in timesToFire)
            {
                if (dateSpec != null || timeString != null)
                    triggerStartTime = GrammarHelper.GetDateTimeFromDateSpecAndTime(dateSpec, timeString);

                if (dayOfWeekSpecs != null)
                {
                    var dayOfWeeks = GrammarHelper.GetDayOfWeekValues(dayOfWeekSpecs);
                    foreach (var dayOfWeek in dayOfWeeks)
                    {
                        if (triggerStartTime == null)
                            triggerStartTime = SystemTime.Now().DateTime;

                        DateTime dowTriggerStartTime = triggerStartTime.Value;
                        //seek
                        dowTriggerStartTime = SeekForwardToNextDayOfWeek(dowTriggerStartTime, dayOfWeek);

                        TriggerBuilder triggerBuilder = TriggerBuilder.Create();
                        triggerBuilder.WithSchedule(CreateScheduleWithAmountAndIntervalUnit(amountString, intervalString, timeZone));
                        triggerBuilder.StartAt(new DateTimeOffset(dowTriggerStartTime, timeZone.GetUtcOffset(dowTriggerStartTime)));

                        results.Add(triggerBuilder, null);
                    }
                }
                else
                {
                    TriggerBuilder triggerBuilder = TriggerBuilder.Create();
                    triggerBuilder.WithSchedule(CreateScheduleWithAmountAndIntervalUnit(amountString, intervalString, timeZone));

                    //start on from time
                    if (triggerStartTime != null)
                        triggerBuilder.StartAt(new DateTimeOffset(triggerStartTime.Value, timeZone.GetUtcOffset(triggerStartTime.Value)));

                    results.Add(triggerBuilder, null);
                }
            }
        }
开发者ID:amazing-andrew,项目名称:Quartz.TextToSchedule,代码行数:63,代码来源:TextToSchedule.cs

示例13: Expression1Handler

        /// <summary>
        /// Handles with a given text matches the Expression1 field.
        /// </summary>
        /// <param name="nameValueCollection">A collection of values from the named capture groups.</param>
        /// <param name="results">The results.</param>
        private void Expression1Handler(NameValueCollection nameValueCollection, TimeZoneInfo timeZone, TextToScheduleResults results)
        {
            var amountString = nameValueCollection["AMOUNT"];
            var intervalUnitString = nameValueCollection["INTERVALUNIT"];
            var startDateString = nameValueCollection["DATESPEC"];

            var time = nameValueCollection["TIME"];
            var fromTime = nameValueCollection["FROMTIME"];
            var toTime = nameValueCollection["TOTIME"];

            var dayOfWeekSpecs = nameValueCollection.GetValues("DAYOFWEEK");
            var monthSpecs = nameValueCollection.GetValues("MONTH");

            DateTime? triggerStartTime = null;

            ICalendar calendar = null;

            //DAY OF WEEK SPECS
            if (dayOfWeekSpecs != null)
            {
                calendar = BuildCalendarOnDayOfWeek(calendar, dayOfWeekSpecs, timeZone);
            }

            //MONTH SPECS
            if (monthSpecs != null)
            {
                calendar = BuildCalendarOnMonths(calendar, monthSpecs, timeZone);
            }


            //TIME (single or range)

            //check for ranged time
            if (fromTime != null && toTime != null)
            {
                calendar = BuildCalendarOnTimeRange(calendar, fromTime, toTime, timeZone);

                //set the start date as the from time
                DateTime? fromTimeStartDate = GrammarHelper.GetTimeFromTimeString(fromTime);
                triggerStartTime = fromTimeStartDate;
            }
            //is regular time, process as single time provided
            else if (time != null)
            {
                DateTime? timeStartDate = GrammarHelper.GetTimeFromTimeString(time);
                triggerStartTime = timeStartDate;
            }

            //BUILD TRIGGER
            TriggerBuilder triggerBuilder = TriggerBuilder.Create();

            //set schedule
            triggerBuilder.WithSchedule(CreateScheduleWithAmountAndIntervalUnit(amountString, intervalUnitString, timeZone));


            //start on from time
            if (triggerStartTime != null)
                triggerBuilder.StartAt(new DateTimeOffset(triggerStartTime.Value, timeZone.GetUtcOffset(triggerStartTime.Value)));

            results.Add(triggerBuilder, calendar);
        }
开发者ID:amazing-andrew,项目名称:Quartz.TextToSchedule,代码行数:66,代码来源:TextToSchedule.cs

示例14: convertTimeZoneTimeToGmt

    private static DateTime convertTimeZoneTimeToGmt(DateTime timeZoneTime_, TimeZoneInfo tz_)
    {
      var offset = tz_.GetUtcOffset(timeZoneTime_.Date);

      return timeZoneTime_.Add(-offset);
    }
开发者ID:heimanhon,项目名称:researchwork,代码行数:6,代码来源:TimeZoneHelper.cs

示例15: FormatDate

        internal static string FormatDate(string format, DateTime utc, TimeZoneInfo zone)
        {
            Debug.Assert(zone != null);

            if (format == null)
                return string.Empty;

            DateTime local = TimeZoneInfo.ConvertTimeFromUtc(utc, zone);

            // here we are creating output string
            StringBuilder result = new StringBuilder();
            bool escape = false;

            foreach (char ch in format)
            {
                if (escape)
                {
                    result.Append(ch);
                    escape = false;
                    continue;
                }

                switch (ch)
                {
                    case 'a':
                        // Lowercase Ante meridiem and Post meridiem - am or pm
                        result.Append(local.ToString("tt", DateTimeFormatInfo.InvariantInfo).ToLowerInvariant());
                        break;

                    case 'A':
                        // Uppercase Ante meridiem and Post meridiem - AM or PM
                        result.Append(local.ToString("tt", DateTimeFormatInfo.InvariantInfo));
                        break;

                    case 'B':
                        // Swatch Beat (Internet Time) - 000 through 999
                        result.AppendFormat("{0:000}", GetSwatchBeat(utc));
                        break;

                    case 'c':
                        {
                            // ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00
                            result.Append(local.ToString("yyyy-MM-dd'T'HH:mm:ss", DateTimeFormatInfo.InvariantInfo));

                            TimeSpan offset = zone.GetUtcOffset(local);
                            result.AppendFormat("{0}{1:00}:{2:00}", (offset.Ticks < 0) ? ""/*offset.Hours already < 0*/ : "+", offset.Hours, offset.Minutes);
                            break;
                        }

                    case 'd':
                        // Day of the month, 2 digits with leading zeros - 01 to 31
                        result.Append(local.ToString("dd", DateTimeFormatInfo.InvariantInfo));
                        break;

                    case 'e':
                        // Timezone identifier (added in PHP 5.1.0)
                        result.Append(zone.Id);
                        break;

                    case 'D':
                        // A textual representation of a day, three letters - Mon through Sun
                        result.Append(local.ToString("ddd", DateTimeFormatInfo.InvariantInfo));
                        break;

                    case 'F':
                        // A full textual representation of a month, such as January or March - January through December
                        result.Append(local.ToString("MMMM", DateTimeFormatInfo.InvariantInfo));
                        break;

                    case 'g':
                        // 12-hour format of an hour without leading zeros - 1 through 12
                        result.Append(local.ToString("%h", DateTimeFormatInfo.InvariantInfo));
                        break;

                    case 'G':
                        // 24-hour format of an hour without leading zeros - 0 through 23
                        result.Append(local.ToString("%H", DateTimeFormatInfo.InvariantInfo));
                        break;

                    case 'h':
                        // 12-hour format of an hour with leading zeros - 01 through 12
                        result.Append(local.ToString("hh", DateTimeFormatInfo.InvariantInfo));
                        break;

                    case 'H':
                        // 24-hour format of an hour with leading zeros - 00 through 23
                        result.Append(local.ToString("HH", DateTimeFormatInfo.InvariantInfo));
                        break;

                    case 'i':
                        // Minutes with leading zeros - 00 to 59
                        result.Append(local.ToString("mm", DateTimeFormatInfo.InvariantInfo));
                        break;

                    case 'I':
                        // Whether or not the date is in daylights savings time - 1 if Daylight Savings Time, 0 otherwise.
                        result.Append(zone.IsDaylightSavingTime(local) ? "1" : "0");
                        break;

                    case 'j':
//.........这里部分代码省略.........
开发者ID:proff,项目名称:Phalanger,代码行数:101,代码来源:DateTime.cs


注:本文中的System.TimeZoneInfo.GetUtcOffset方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。