diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2012-01-12 08:40:50 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2012-01-12 08:42:14 -0800 |
commit | af21cdaf77ca72f54e04f22268b740ce262582fa (patch) | |
tree | 9b158e3bff1f56264bccb9e45c8b807816beece6 /src/DotNetOpenAuth.Core/Messaging/TimestampEncoder.cs | |
parent | a73f2a4830aaa2afcb3f13da2206d9b011dad7fb (diff) | |
download | DotNetOpenAuth-af21cdaf77ca72f54e04f22268b740ce262582fa.zip DotNetOpenAuth-af21cdaf77ca72f54e04f22268b740ce262582fa.tar.gz DotNetOpenAuth-af21cdaf77ca72f54e04f22268b740ce262582fa.tar.bz2 |
Renamed assembly DotNetOpenAuth.Messaging(.UI) to DotNetOpenAuth.Core(.UI)
Diffstat (limited to 'src/DotNetOpenAuth.Core/Messaging/TimestampEncoder.cs')
-rw-r--r-- | src/DotNetOpenAuth.Core/Messaging/TimestampEncoder.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Core/Messaging/TimestampEncoder.cs b/src/DotNetOpenAuth.Core/Messaging/TimestampEncoder.cs new file mode 100644 index 0000000..b83a426 --- /dev/null +++ b/src/DotNetOpenAuth.Core/Messaging/TimestampEncoder.cs @@ -0,0 +1,61 @@ +//----------------------------------------------------------------------- +// <copyright file="TimestampEncoder.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Messaging { + using System; + using System.Globalization; + using DotNetOpenAuth.Messaging.Reflection; + + /// <summary> + /// Translates between a <see cref="DateTime"/> and the number of seconds between it and 1/1/1970 12 AM + /// </summary> + internal class TimestampEncoder : IMessagePartEncoder { + /// <summary> + /// The reference date and time for calculating time stamps. + /// </summary> + internal static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + /// <summary> + /// Initializes a new instance of the <see cref="TimestampEncoder"/> class. + /// </summary> + public TimestampEncoder() { + } + + /// <summary> + /// Encodes the specified value. + /// </summary> + /// <param name="value">The value. Guaranteed to never be null.</param> + /// <returns> + /// The <paramref name="value"/> in string form, ready for message transport. + /// </returns> + public string Encode(object value) { + if (value == null) { + return null; + } + + var timestamp = (DateTime)value; + TimeSpan secondsSinceEpoch = timestamp - Epoch; + return ((int)secondsSinceEpoch.TotalSeconds).ToString(CultureInfo.InvariantCulture); + } + + /// <summary> + /// Decodes the specified value. + /// </summary> + /// <param name="value">The string value carried by the transport. Guaranteed to never be null, although it may be empty.</param> + /// <returns> + /// The deserialized form of the given string. + /// </returns> + /// <exception cref="FormatException">Thrown when the string value given cannot be decoded into the required object type.</exception> + public object Decode(string value) { + if (value == null) { + return null; + } + + var secondsSinceEpoch = int.Parse(value, CultureInfo.InvariantCulture); + return Epoch.AddSeconds(secondsSinceEpoch); + } + } +} |