Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Base / System / Windows / Markup / DateTimeValueSerializer.cs / 1 / DateTimeValueSerializer.cs
/****************************************************************************\ * * File: DateTimeValueSerializer.cs * \***************************************************************************/ using System.Globalization; using System.Text; namespace System.Windows.Markup { //+------------------------------------------------------------------------------------- // // DateTimeValueSerializer // // This class converts DateTime values to/from string. We don't use the DateTimeConverter // because it doesn't support custom cultures, and in Xaml we require the converter to // support en-us culture. // //+------------------------------------------------------------------------------------- ////// ValueSerializer for DateTime. /// public class DateTimeValueSerializer : ValueSerializer { ////// Initializes a new instance of the public DateTimeValueSerializer() { } ///class. /// /// Indicate that we do convert DateTime's from string. /// public override bool CanConvertFromString(string value, IValueSerializerContext context) { return true; } ////// Indicate that we do convert a DateTime to string. /// public override bool CanConvertToString(object value, IValueSerializerContext context) { // Validate the input type if ( !(value is DateTime)) { return false; } return true; } ////// Converts the given value object to a public override object ConvertFromString(string value, IValueSerializerContext context) { // Validate and clean up input. if( value == null ) { throw GetConvertFromException(value); } if( value.Length == 0 ) { return DateTime.MinValue; } // Get a DateTimeFormatInfo CultureInfo culture = CultureInfo.GetCultureInfo("en-US"); DateTimeFormatInfo dateTimeFormatInfo; dateTimeFormatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); // Set the formatting style for round-tripping and to trim the string. DateTimeStyles dateTimeStyles = DateTimeStyles.RoundtripKind | DateTimeStyles.NoCurrentDateDefault | DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite; // Create the DateTime, using the DateTimeInfo if possible, and the culture otherwise. if (dateTimeFormatInfo != null) { return DateTime.Parse(value, dateTimeFormatInfo, dateTimeStyles); } else { // The culture didn't have a DateTimeFormatInfo. return DateTime.Parse(value, culture, dateTimeStyles); } } ///. /// /// Converts the given value object to a public override string ConvertToString(object value, IValueSerializerContext context) { if( value == null || !(value is DateTime)) { throw GetConvertToException( value, typeof(string) ); } DateTime dateTime = (DateTime)value; // Build up the format string to be used in DateTime.ToString() StringBuilder formatString = new StringBuilder("yyyy-MM-dd"); if (dateTime.TimeOfDay.TotalSeconds == 0) { // The time portion of this DateTime is exactly at midnight. // We don't include the time component if the Kind is unspecified. // Otherwise, we're going to be including the time zone info, so'll // we'll have to include the time. if( dateTime.Kind != DateTimeKind.Unspecified ) { formatString.Append("'T'HH':'mm"); } } else { // We're going to write out at least the hours/minutes formatString.Append("'T'HH':'mm"); if (dateTime.TimeOfDay.Seconds != 0 || dateTime.TimeOfDay.Milliseconds != 0) { // We have seconds, so we'll write them out too. formatString.Append("':'ss"); if (dateTime.TimeOfDay.Milliseconds != 0) { // And we have milliseconds formatString.Append("'.'FFFFFFF"); } } } // Add the format specifier that indicates we want the DateTimeKind to be // included in the output formulation -- UTC gets written out with a "Z", // and Local gets written out with e.g. "-08:00" for Pacific Standard Time. formatString.Append("K"); // We've finally got our format string built, we can create the string. return dateTime.ToString(formatString.ToString(), CultureInfo.GetCultureInfo("en-US") ); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. /****************************************************************************\ * * File: DateTimeValueSerializer.cs * \***************************************************************************/ using System.Globalization; using System.Text; namespace System.Windows.Markup { //+------------------------------------------------------------------------------------- // // DateTimeValueSerializer // // This class converts DateTime values to/from string. We don't use the DateTimeConverter // because it doesn't support custom cultures, and in Xaml we require the converter to // support en-us culture. // //+------------------------------------------------------------------------------------- ///using the arguments. /// /// ValueSerializer for DateTime. /// public class DateTimeValueSerializer : ValueSerializer { ////// Initializes a new instance of the public DateTimeValueSerializer() { } ///class. /// /// Indicate that we do convert DateTime's from string. /// public override bool CanConvertFromString(string value, IValueSerializerContext context) { return true; } ////// Indicate that we do convert a DateTime to string. /// public override bool CanConvertToString(object value, IValueSerializerContext context) { // Validate the input type if ( !(value is DateTime)) { return false; } return true; } ////// Converts the given value object to a public override object ConvertFromString(string value, IValueSerializerContext context) { // Validate and clean up input. if( value == null ) { throw GetConvertFromException(value); } if( value.Length == 0 ) { return DateTime.MinValue; } // Get a DateTimeFormatInfo CultureInfo culture = CultureInfo.GetCultureInfo("en-US"); DateTimeFormatInfo dateTimeFormatInfo; dateTimeFormatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo)); // Set the formatting style for round-tripping and to trim the string. DateTimeStyles dateTimeStyles = DateTimeStyles.RoundtripKind | DateTimeStyles.NoCurrentDateDefault | DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite; // Create the DateTime, using the DateTimeInfo if possible, and the culture otherwise. if (dateTimeFormatInfo != null) { return DateTime.Parse(value, dateTimeFormatInfo, dateTimeStyles); } else { // The culture didn't have a DateTimeFormatInfo. return DateTime.Parse(value, culture, dateTimeStyles); } } ///. /// /// Converts the given value object to a public override string ConvertToString(object value, IValueSerializerContext context) { if( value == null || !(value is DateTime)) { throw GetConvertToException( value, typeof(string) ); } DateTime dateTime = (DateTime)value; // Build up the format string to be used in DateTime.ToString() StringBuilder formatString = new StringBuilder("yyyy-MM-dd"); if (dateTime.TimeOfDay.TotalSeconds == 0) { // The time portion of this DateTime is exactly at midnight. // We don't include the time component if the Kind is unspecified. // Otherwise, we're going to be including the time zone info, so'll // we'll have to include the time. if( dateTime.Kind != DateTimeKind.Unspecified ) { formatString.Append("'T'HH':'mm"); } } else { // We're going to write out at least the hours/minutes formatString.Append("'T'HH':'mm"); if (dateTime.TimeOfDay.Seconds != 0 || dateTime.TimeOfDay.Milliseconds != 0) { // We have seconds, so we'll write them out too. formatString.Append("':'ss"); if (dateTime.TimeOfDay.Milliseconds != 0) { // And we have milliseconds formatString.Append("'.'FFFFFFF"); } } } // Add the format specifier that indicates we want the DateTimeKind to be // included in the output formulation -- UTC gets written out with a "Z", // and Local gets written out with e.g. "-08:00" for Pacific Standard Time. formatString.Append("K"); // We've finally got our format string built, we can create the string. return dateTime.ToString(formatString.ToString(), CultureInfo.GetCultureInfo("en-US") ); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.using the arguments. ///
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TreeViewBindingsEditorForm.cs
- dbdatarecord.cs
- ConsoleKeyInfo.cs
- DataGridViewTextBoxColumn.cs
- XsltArgumentList.cs
- Scripts.cs
- Helper.cs
- HwndHostAutomationPeer.cs
- ShaderRenderModeValidation.cs
- SocketInformation.cs
- BrowserCapabilitiesFactoryBase.cs
- QuaternionIndependentAnimationStorage.cs
- InvalidCommandTreeException.cs
- DataControlExtensions.cs
- CodeTypeDeclaration.cs
- ThicknessAnimationBase.cs
- SafeFileHandle.cs
- CompilerLocalReference.cs
- VisualStyleElement.cs
- XmlIlVisitor.cs
- PeerNodeTraceRecord.cs
- MenuItemBindingCollection.cs
- XmlChildEnumerator.cs
- DataGridViewSelectedColumnCollection.cs
- ManagementObjectSearcher.cs
- StringArrayConverter.cs
- CodeStatementCollection.cs
- Util.cs
- ProcessThreadCollection.cs
- XmlChildNodes.cs
- XmlValidatingReader.cs
- DataReaderContainer.cs
- CompiledIdentityConstraint.cs
- WmpBitmapDecoder.cs
- IncrementalCompileAnalyzer.cs
- DataDocumentXPathNavigator.cs
- SystemParameters.cs
- StrongNamePublicKeyBlob.cs
- SoapHeader.cs
- CodeConstructor.cs
- ApplicationFileParser.cs
- TraceData.cs
- Style.cs
- IPPacketInformation.cs
- DataListItemEventArgs.cs
- ConfigXmlSignificantWhitespace.cs
- DataViewManagerListItemTypeDescriptor.cs
- RelatedImageListAttribute.cs
- VectorAnimationUsingKeyFrames.cs
- TableLayoutStyle.cs
- QueryContinueDragEventArgs.cs
- TextServicesCompartment.cs
- GeneralTransform3D.cs
- AQNBuilder.cs
- UserNamePasswordServiceCredential.cs
- _AcceptOverlappedAsyncResult.cs
- StateInitialization.cs
- Cloud.cs
- SerialReceived.cs
- Rotation3DKeyFrameCollection.cs
- DesignerOptions.cs
- XmlQueryStaticData.cs
- CompilerGeneratedAttribute.cs
- NameObjectCollectionBase.cs
- EncodingInfo.cs
- MailMessageEventArgs.cs
- AssemblyAssociatedContentFileAttribute.cs
- UIElementIsland.cs
- ToolStripSystemRenderer.cs
- NumberSubstitution.cs
- tooltip.cs
- DataSourceGroupCollection.cs
- CompModHelpers.cs
- SpnegoTokenAuthenticator.cs
- OdbcCommand.cs
- XamlSerializerUtil.cs
- sqlnorm.cs
- ContainerControl.cs
- JsonWriter.cs
- X509CertificateCollection.cs
- ReadOnlyObservableCollection.cs
- WsdlBuildProvider.cs
- ComplexBindingPropertiesAttribute.cs
- RootBrowserWindowAutomationPeer.cs
- TextServicesDisplayAttributePropertyRanges.cs
- ExpressionPrefixAttribute.cs
- TdsParserStaticMethods.cs
- ParameterCollection.cs
- HostingEnvironmentException.cs
- PropertyCondition.cs
- FileDialogCustomPlaces.cs
- ScrollBar.cs
- SoapFault.cs
- TrackingMemoryStreamFactory.cs
- SpotLight.cs
- FrameworkObject.cs
- XmlSerializationGeneratedCode.cs
- UriScheme.cs
- XmlTextEncoder.cs
- PersonalizationProviderCollection.cs