Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / CompMod / System / ComponentModel / DateTimeOffsetConverter.cs / 1 / DateTimeOffsetConverter.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
DevDiv Bugs 181818: DateTimeOffset should have a type converter just like DateTime.
This converter should behave just like DateTimeConverter only it should convert DateTimeOffsets.
The code was copied from DateTimeConverter and adapted for DateTimeOffset.
*/
namespace System.ComponentModel {
using Microsoft.Win32;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters;
using System.Security.Permissions;
///
/// Provides a type converter to convert
/// objects to and from various other representations.
///
[HostProtection(SharedState = true)]
public class DateTimeOffsetConverter : TypeConverter {
///
/// Gets a value indicating whether this converter can
/// convert an object in the given source type to a
/// object using the
/// specified context.
///
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
if (sourceType == typeof(string)) {
return true;
}
return base.CanConvertFrom(context, sourceType);
}
///
/// Gets a value indicating whether this converter can
/// convert an object to the given destination type using the context.
///
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if (destinationType == typeof(InstanceDescriptor)) {
return true;
}
return base.CanConvertTo(context, destinationType);
}
///
/// Converts the given value object to a
/// object.
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
string text = ((string)value).Trim();
if (text.Length == 0) {
return DateTimeOffset.MinValue;
}
try {
// See if we have a culture info to parse with. If so, then use it.
//
DateTimeFormatInfo formatInfo = null;
if (culture != null ) {
formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
}
if (formatInfo != null) {
return DateTimeOffset.Parse(text, formatInfo);
}
else {
return DateTimeOffset.Parse(text, culture);
}
}
catch (FormatException e) {
throw new FormatException(SR.GetString(SR.ConvertInvalidPrimitive, (string)value, "DateTimeOffset"), e);
}
}
return base.ConvertFrom(context, culture, value);
}
///
/// Converts the given value object to a
/// object
/// using the arguments.
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
// logic is exactly as in DateTimeConverter, only the offset pattern ' zzz' is added to the default
// ConvertToString pattern.
if (destinationType == typeof(string) && value is DateTimeOffset) {
DateTimeOffset dto = (DateTimeOffset) value;
if (dto == DateTimeOffset.MinValue) {
return string.Empty;
}
if (culture == null) {
culture = CultureInfo.CurrentCulture;
}
DateTimeFormatInfo formatInfo = null;
formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
string format;
if (culture == CultureInfo.InvariantCulture) {
// note: the y/m/d format used when there is or isn't a time component is not consistent
// when the culture is invariant, because for some reason InvariantCulture's date format is
// MM/dd/yyyy. However, this matches the behavior of DateTimeConverter so it is preserved here.
if (dto.TimeOfDay.TotalSeconds == 0) {
// pattern just like DateTimeConverter when DateTime.TimeOfDay.TotalSeconds==0
// but with ' zzz' offset pattern added.
return dto.ToString("yyyy-MM-dd zzz", culture);
}
else {
return dto.ToString(culture);
}
}
if (dto.TimeOfDay.TotalSeconds == 0) {
// pattern just like DateTimeConverter when DateTime.TimeOfDay.TotalSeconds==0
// but with ' zzz' offset pattern added.
format = formatInfo.ShortDatePattern + " zzz";
}
else {
// pattern just like DateTimeConverter when DateTime.TimeOfDay.TotalSeconds!=0
// but with ' zzz' offset pattern added.
format = formatInfo.ShortDatePattern + " " + formatInfo.ShortTimePattern + " zzz";
}
return dto.ToString(format, CultureInfo.CurrentCulture);
}
if (destinationType == typeof(InstanceDescriptor) && value is DateTimeOffset) {
DateTimeOffset dto = (DateTimeOffset)value;
if (dto.Ticks == 0) {
// Make a special case for the empty DateTimeOffset
//
ConstructorInfo ctr = typeof(DateTimeOffset).GetConstructor(new Type[] {typeof(Int64)});
if (ctr != null) {
return new InstanceDescriptor(ctr, new object[] {
dto.Ticks });
}
}
ConstructorInfo ctor = typeof(DateTimeOffset).GetConstructor(new Type[] {
typeof(int), typeof(int), typeof(int), typeof(int),
typeof(int), typeof(int), typeof(int), typeof(TimeSpan) });
if (ctor != null) {
return new InstanceDescriptor(ctor, new object[] {
dto.Year, dto.Month, dto.Day, dto.Hour, dto.Minute, dto.Second, dto.Millisecond, dto.Offset });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
DevDiv Bugs 181818: DateTimeOffset should have a type converter just like DateTime.
This converter should behave just like DateTimeConverter only it should convert DateTimeOffsets.
The code was copied from DateTimeConverter and adapted for DateTimeOffset.
*/
namespace System.ComponentModel {
using Microsoft.Win32;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters;
using System.Security.Permissions;
///
/// Provides a type converter to convert
/// objects to and from various other representations.
///
[HostProtection(SharedState = true)]
public class DateTimeOffsetConverter : TypeConverter {
///
/// Gets a value indicating whether this converter can
/// convert an object in the given source type to a
/// object using the
/// specified context.
///
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
if (sourceType == typeof(string)) {
return true;
}
return base.CanConvertFrom(context, sourceType);
}
///
/// Gets a value indicating whether this converter can
/// convert an object to the given destination type using the context.
///
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if (destinationType == typeof(InstanceDescriptor)) {
return true;
}
return base.CanConvertTo(context, destinationType);
}
///
/// Converts the given value object to a
/// object.
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
string text = ((string)value).Trim();
if (text.Length == 0) {
return DateTimeOffset.MinValue;
}
try {
// See if we have a culture info to parse with. If so, then use it.
//
DateTimeFormatInfo formatInfo = null;
if (culture != null ) {
formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
}
if (formatInfo != null) {
return DateTimeOffset.Parse(text, formatInfo);
}
else {
return DateTimeOffset.Parse(text, culture);
}
}
catch (FormatException e) {
throw new FormatException(SR.GetString(SR.ConvertInvalidPrimitive, (string)value, "DateTimeOffset"), e);
}
}
return base.ConvertFrom(context, culture, value);
}
///
/// Converts the given value object to a
/// object
/// using the arguments.
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
// logic is exactly as in DateTimeConverter, only the offset pattern ' zzz' is added to the default
// ConvertToString pattern.
if (destinationType == typeof(string) && value is DateTimeOffset) {
DateTimeOffset dto = (DateTimeOffset) value;
if (dto == DateTimeOffset.MinValue) {
return string.Empty;
}
if (culture == null) {
culture = CultureInfo.CurrentCulture;
}
DateTimeFormatInfo formatInfo = null;
formatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
string format;
if (culture == CultureInfo.InvariantCulture) {
// note: the y/m/d format used when there is or isn't a time component is not consistent
// when the culture is invariant, because for some reason InvariantCulture's date format is
// MM/dd/yyyy. However, this matches the behavior of DateTimeConverter so it is preserved here.
if (dto.TimeOfDay.TotalSeconds == 0) {
// pattern just like DateTimeConverter when DateTime.TimeOfDay.TotalSeconds==0
// but with ' zzz' offset pattern added.
return dto.ToString("yyyy-MM-dd zzz", culture);
}
else {
return dto.ToString(culture);
}
}
if (dto.TimeOfDay.TotalSeconds == 0) {
// pattern just like DateTimeConverter when DateTime.TimeOfDay.TotalSeconds==0
// but with ' zzz' offset pattern added.
format = formatInfo.ShortDatePattern + " zzz";
}
else {
// pattern just like DateTimeConverter when DateTime.TimeOfDay.TotalSeconds!=0
// but with ' zzz' offset pattern added.
format = formatInfo.ShortDatePattern + " " + formatInfo.ShortTimePattern + " zzz";
}
return dto.ToString(format, CultureInfo.CurrentCulture);
}
if (destinationType == typeof(InstanceDescriptor) && value is DateTimeOffset) {
DateTimeOffset dto = (DateTimeOffset)value;
if (dto.Ticks == 0) {
// Make a special case for the empty DateTimeOffset
//
ConstructorInfo ctr = typeof(DateTimeOffset).GetConstructor(new Type[] {typeof(Int64)});
if (ctr != null) {
return new InstanceDescriptor(ctr, new object[] {
dto.Ticks });
}
}
ConstructorInfo ctor = typeof(DateTimeOffset).GetConstructor(new Type[] {
typeof(int), typeof(int), typeof(int), typeof(int),
typeof(int), typeof(int), typeof(int), typeof(TimeSpan) });
if (ctor != null) {
return new InstanceDescriptor(ctor, new object[] {
dto.Year, dto.Month, dto.Day, dto.Hour, dto.Minute, dto.Second, dto.Millisecond, dto.Offset });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Processor.cs
- RemotingException.cs
- XmlObjectSerializerReadContextComplex.cs
- DataSourceSelectArguments.cs
- BufferModesCollection.cs
- ControlCachePolicy.cs
- URLIdentityPermission.cs
- WindowsTooltip.cs
- ScaleTransform3D.cs
- XamlFxTrace.cs
- BooleanExpr.cs
- selecteditemcollection.cs
- BindingContext.cs
- ErrorTolerantObjectWriter.cs
- WebBrowserContainer.cs
- HwndTarget.cs
- TransformerConfigurationWizardBase.cs
- ResourceDictionary.cs
- DelegateSerializationHolder.cs
- BorderSidesEditor.cs
- Message.cs
- PenLineCapValidation.cs
- XmlParser.cs
- GifBitmapEncoder.cs
- IDictionary.cs
- ResourcesChangeInfo.cs
- ExpressionDumper.cs
- SQLGuid.cs
- ImageCodecInfoPrivate.cs
- Label.cs
- HiddenField.cs
- UIInitializationException.cs
- XPathAxisIterator.cs
- DependencyObjectProvider.cs
- GenericEnumerator.cs
- ReflectionHelper.cs
- NumberFunctions.cs
- JpegBitmapDecoder.cs
- DropDownList.cs
- ValidatorUtils.cs
- TrimSurroundingWhitespaceAttribute.cs
- TabRenderer.cs
- RegexCompilationInfo.cs
- FlatButtonAppearance.cs
- CompositeFontInfo.cs
- DataSysAttribute.cs
- EUCJPEncoding.cs
- SapiInterop.cs
- PasswordBoxAutomationPeer.cs
- TypeEnumerableViewSchema.cs
- DataGridRowAutomationPeer.cs
- PopOutPanel.cs
- FormattedTextSymbols.cs
- TransactionContextValidator.cs
- XmlAttributeProperties.cs
- ImportContext.cs
- HttpCookieCollection.cs
- StateFinalizationActivity.cs
- ObjectStorage.cs
- SpinLock.cs
- WorkflowOperationErrorHandler.cs
- NonParentingControl.cs
- TagPrefixInfo.cs
- HtmlImage.cs
- CTreeGenerator.cs
- InvalidProgramException.cs
- MediaTimeline.cs
- InheritanceAttribute.cs
- FamilyTypeface.cs
- EntityClassGenerator.cs
- PrintDialogException.cs
- TickBar.cs
- NotFiniteNumberException.cs
- WebPartChrome.cs
- BitVector32.cs
- BindingExpression.cs
- NetworkInformationPermission.cs
- METAHEADER.cs
- Positioning.cs
- DataGridViewAdvancedBorderStyle.cs
- ShaperBuffers.cs
- UriTemplateVariableQueryValue.cs
- NamespaceInfo.cs
- AnimationClock.cs
- SecurityTokenResolver.cs
- Renderer.cs
- DictionaryEntry.cs
- OleDbSchemaGuid.cs
- SettingsContext.cs
- WebPartVerb.cs
- DataGridViewRowsAddedEventArgs.cs
- Win32SafeHandles.cs
- CapabilitiesPattern.cs
- ColorInterpolationModeValidation.cs
- WebPartTracker.cs
- InputScopeAttribute.cs
- CodeSubDirectoriesCollection.cs
- ScrollProperties.cs
- FixedNode.cs
- ZipIOExtraFieldElement.cs