70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Google.Protobuf.WellKnownTypes;
|
|
|
|
namespace bts.api
|
|
{
|
|
public static class TimestampExtensions
|
|
{
|
|
public static Timestamp add_nanos(this Timestamp lhs, int nanos)
|
|
{
|
|
var c = new Timestamp(lhs);
|
|
c.Nanos += nanos;
|
|
return c;
|
|
}
|
|
public static bool less(this Timestamp lhs, Timestamp rhs)
|
|
{
|
|
return lhs.compare(rhs) < 0;
|
|
}
|
|
public static bool less_or_equal(this Timestamp lhs, Timestamp rhs)
|
|
{
|
|
return lhs.compare(rhs) <= 0;
|
|
}
|
|
public static bool greater(this Timestamp lhs, Timestamp rhs)
|
|
{
|
|
return lhs.compare(rhs) > 0;
|
|
}
|
|
public static bool greater_or_equal(this Timestamp lhs, Timestamp rhs)
|
|
{
|
|
return lhs.compare(rhs) >= 0;
|
|
}
|
|
public static int compare(this Timestamp lhs, Timestamp rhs)
|
|
{
|
|
int secdiff = lhs.Seconds.CompareTo(rhs.Seconds);
|
|
return (secdiff != 0) ? secdiff : (lhs.Nanos.CompareTo(rhs.Nanos));
|
|
}
|
|
public static Comparer<Timestamp> timestamp_comparer => Comparer<Timestamp>.Create((x, y) => x.compare(y));
|
|
|
|
public static Timestamp parse_timestamp(string data)
|
|
{
|
|
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
try
|
|
{
|
|
if (data.Length <= 8)
|
|
{
|
|
dt = DateTime.ParseExact(data, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
|
|
}
|
|
else
|
|
{
|
|
int sep = 14;
|
|
var yyMMddhhmmss = data.Substring(0, sep);
|
|
dt = DateTime.ParseExact(yyMMddhhmmss, "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture);
|
|
var nnanos = data.Substring(sep, data.Length - sep);
|
|
long nano = long.Parse(nnanos);
|
|
dt = dt.AddTicks(nano / 100);
|
|
}
|
|
}
|
|
catch (Exception _)
|
|
{
|
|
System.Diagnostics.Debug.Assert(_ != null); //UNUSED(_)
|
|
}
|
|
dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
|
|
var ts = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(dt);
|
|
return ts;
|
|
}
|
|
public static string to_fix_string(this Timestamp ts)
|
|
{
|
|
return ts.ToDateTime().ToString("yyyyMMdd-HH:mm:ss.fff");
|
|
}
|
|
}
|
|
} |