From 370716b9171fffe3bc58e64cd0ad780dc4a28002 Mon Sep 17 00:00:00 2001 From: ceee Date: Tue, 13 Aug 2013 22:26:34 +0200 Subject: [PATCH] add common utilities --- UptimeSharp/Utilities.cs | 78 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 UptimeSharp/Utilities.cs diff --git a/UptimeSharp/Utilities.cs b/UptimeSharp/Utilities.cs new file mode 100644 index 0000000..dd368b0 --- /dev/null +++ b/UptimeSharp/Utilities.cs @@ -0,0 +1,78 @@ +using RestSharp; +using System; +using System.Collections.Generic; + +namespace UptimeSharp +{ + /// + /// General utilities + /// + internal class Utilities + { + /// + /// converts DateTime to an UNIX timestamp + /// + /// The date. + /// UNIX timestamp + public static int? GetUnixTimestamp(DateTime? dateTime) + { + if (dateTime == null) + { + return null; + } + + return (int)((DateTime)dateTime - new DateTime(1970, 1, 1)).TotalSeconds; + } + + + /// + /// Creates a Parameter object. + /// + /// The name. + /// The value. + /// The type. + /// + public static Parameter CreateParam(string name, object value, ParameterType type = ParameterType.GetOrPost) + { + return new Parameter() { Name = name, Value = value, Type = type }; + } + + + /// + /// Creates a Parameter object within a list. + /// + /// The name. + /// The value. + /// The type. + /// + public static List CreateParamInList(string name, object value, ParameterType type = ParameterType.GetOrPost) + { + return new List() { CreateParam(name, value, type) }; + } + + + /// + /// Convert a dictionary to a list + /// + /// + /// The items. + /// + public static List DictionaryToList(Dictionary items) where T : new() + { + if (items == null) + { + return null; + } + + var itemEnumerator = items.GetEnumerator(); + List list = new List(); + + while (itemEnumerator.MoveNext()) + { + list.Add(itemEnumerator.Current.Value); + } + + return list; + } + } +}