Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created March 30, 2017 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/505b4202878cb15850418a2feb13c7de to your computer and use it in GitHub Desktop.
Save atifaziz/505b4202878cb15850418a2feb13c7de to your computer and use it in GitHub Desktop.
Generic C# structure to hold a value together with a timestamp
#region The MIT License (MIT)
//
// Copyright (C) 2017 Atif Aziz. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#endregion
using System;
static partial class Timestamped
{
public static Timestamped<T> Create<T>(T value, DateTimeOffset timestamp) =>
new Timestamped<T>(value, timestamp);
}
[Serializable]
partial struct Timestamped<T> : IEquatable<Timestamped<T>>
{
readonly (DateTimeOffset Timestamp, T Value) _data;
public Timestamped(T value, DateTimeOffset timestamp) => _data = (timestamp, value);
public DateTimeOffset Timestamp => _data.Timestamp;
public T Value => _data.Value;
public bool Equals(Timestamped<T> other) => _data.Equals(other._data);
public override bool Equals(object obj) => obj is Timestamped<T> ts && Equals(ts);
public override int GetHashCode() => _data.GetHashCode();
public override string ToString() => _data.Value + " @ " + _data.Timestamp;
public static bool operator ==(Timestamped<T> a, Timestamped<T> b) => a.Equals(b);
public static bool operator !=(Timestamped<T> a, Timestamped<T> b) => !a.Equals(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment