本文整理匯總了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");
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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"));
}
示例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);
}
示例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);
}
示例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);
}
示例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;
}
示例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;
}
}
示例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);
}
}
}
示例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);
}
示例14: convertTimeZoneTimeToGmt
private static DateTime convertTimeZoneTimeToGmt(DateTime timeZoneTime_, TimeZoneInfo tz_)
{
var offset = tz_.GetUtcOffset(timeZoneTime_.Date);
return timeZoneTime_.Add(-offset);
}
示例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':
//.........這裏部分代碼省略.........