StyleCop clean.
[dotnetoauth.git] / src / DotNetOpenAuth / OpenId / Extensions / ProviderAuthenticationPolicy / TimespanSecondsEncoder.cs
blobcc3f7cc41db87a5bd36dcc05b1f7298a635973a1
1 //-----------------------------------------------------------------------
2 // <copyright file="TimespanSecondsEncoder.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth.OpenId.Extensions.ProviderAuthenticationPolicy {
8 using System;
9 using System.Globalization;
10 using DotNetOpenAuth.Messaging.Reflection;
12 /// <summary>
13 /// Encodes and decodes the <see cref="TimeSpan"/> as an integer of total seconds.
14 /// </summary>
15 internal class TimespanSecondsEncoder : IMessagePartEncoder {
16 #region IMessagePartEncoder Members
18 /// <summary>
19 /// Encodes the specified value.
20 /// </summary>
21 /// <param name="value">The value. Guaranteed to never be null.</param>
22 /// <returns>
23 /// The <paramref name="value"/> in string form, ready for message transport.
24 /// </returns>
25 public string Encode(object value) {
26 TimeSpan? timeSpan = value as TimeSpan?;
27 if (timeSpan.HasValue) {
28 return timeSpan.Value.TotalSeconds.ToString(CultureInfo.InvariantCulture);
29 } else {
30 return null;
34 /// <summary>
35 /// Decodes the specified value.
36 /// </summary>
37 /// <param name="value">The string value carried by the transport. Guaranteed to never be null, although it may be empty.</param>
38 /// <returns>
39 /// The deserialized form of the given string.
40 /// </returns>
41 /// <exception cref="FormatException">Thrown when the string value given cannot be decoded into the required object type.</exception>
42 public object Decode(string value) {
43 return TimeSpan.FromSeconds(double.Parse(value, CultureInfo.InvariantCulture));
46 #endregion