Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using Terraria;
- using TShockAPI;
- namespace TicketPlugin
- {
- public class Utils
- {
- public static int UnixTimestamp()
- {
- int unixTime = (int)(System.DateTime.UtcNow - new System.DateTime(1970, 1, 1)).TotalSeconds;
- return unixTime;
- }
- public static TimeSpan ParseTimeSpan(string s)
- {
- const string Quantity = "quantity";
- const string Unit = "unit";
- const string Days = @"(d(ays?)?)";
- const string Hours = @"(h((ours?)|(rs?))?)";
- const string Minutes = @"(m((inutes?)|(ins?))?)";
- const string Seconds = @"(s((econds?)|(ecs?))?)";
- Regex timeSpanRegex = new Regex(
- string.Format(@"\s*(?<{0}>\d+)\s*(?<{1}>({2}|{3}|{4}|{5}|\Z))",
- Quantity, Unit, Days, Hours, Minutes, Seconds),
- RegexOptions.IgnoreCase);
- MatchCollection matches = timeSpanRegex.Matches(s);
- int l;
- TimeSpan ts = new TimeSpan();
- if (!Int32.TryParse(s.Substring(0, 1), out l))
- {
- return ts;
- }
- foreach (Match match in matches)
- {
- if (Regex.IsMatch(match.Groups[Unit].Value, @"\A" + Days))
- {
- ts = ts.Add(TimeSpan.FromDays(double.Parse(match.Groups[Quantity].Value)));
- }
- else if (Regex.IsMatch(match.Groups[Unit].Value, Hours))
- {
- ts = ts.Add(TimeSpan.FromHours(double.Parse(match.Groups[Quantity].Value)));
- }
- else if (Regex.IsMatch(match.Groups[Unit].Value, Minutes))
- {
- ts = ts.Add(TimeSpan.FromMinutes(double.Parse(match.Groups[Quantity].Value)));
- }
- else if (Regex.IsMatch(match.Groups[Unit].Value, Seconds))
- {
- ts = ts.Add(TimeSpan.FromSeconds(double.Parse(match.Groups[Quantity].Value)));
- }
- else
- {
- ts = ts.Add(TimeSpan.FromMinutes(double.Parse(match.Groups[Quantity].Value)));
- }
- }
- return ts;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement