summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core/Messaging/TimestampEncoder.cs
blob: 1b3379f213978945cb0013077ff071bd99aa96ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//-----------------------------------------------------------------------
// <copyright file="TimestampEncoder.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. 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);
		}
	}
}