Advertisement
Spectrewiz

The UTILS of my Ticket Plugin (just some code I pilfered)

Feb 26th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using Terraria;
  7. using TShockAPI;
  8.  
  9. namespace TicketPlugin
  10. {
  11.     public class Utils
  12.     {
  13.         public static int UnixTimestamp()
  14.         {
  15.             int unixTime = (int)(System.DateTime.UtcNow - new System.DateTime(1970, 1, 1)).TotalSeconds;
  16.             return unixTime;
  17.         }
  18.  
  19.         public static TimeSpan ParseTimeSpan(string s)
  20.         {
  21.             const string Quantity = "quantity";
  22.             const string Unit = "unit";
  23.  
  24.             const string Days = @"(d(ays?)?)";
  25.             const string Hours = @"(h((ours?)|(rs?))?)";
  26.             const string Minutes = @"(m((inutes?)|(ins?))?)";
  27.             const string Seconds = @"(s((econds?)|(ecs?))?)";
  28.  
  29.             Regex timeSpanRegex = new Regex(
  30.                 string.Format(@"\s*(?<{0}>\d+)\s*(?<{1}>({2}|{3}|{4}|{5}|\Z))",
  31.                               Quantity, Unit, Days, Hours, Minutes, Seconds),
  32.                               RegexOptions.IgnoreCase);
  33.             MatchCollection matches = timeSpanRegex.Matches(s);
  34.             int l;
  35.             TimeSpan ts = new TimeSpan();
  36.             if (!Int32.TryParse(s.Substring(0, 1), out l))
  37.             {
  38.                 return ts;
  39.             }
  40.             foreach (Match match in matches)
  41.             {
  42.                 if (Regex.IsMatch(match.Groups[Unit].Value, @"\A" + Days))
  43.                 {
  44.                     ts = ts.Add(TimeSpan.FromDays(double.Parse(match.Groups[Quantity].Value)));
  45.                 }
  46.                 else if (Regex.IsMatch(match.Groups[Unit].Value, Hours))
  47.                 {
  48.                     ts = ts.Add(TimeSpan.FromHours(double.Parse(match.Groups[Quantity].Value)));
  49.                 }
  50.                 else if (Regex.IsMatch(match.Groups[Unit].Value, Minutes))
  51.                 {
  52.                     ts = ts.Add(TimeSpan.FromMinutes(double.Parse(match.Groups[Quantity].Value)));
  53.                 }
  54.                 else if (Regex.IsMatch(match.Groups[Unit].Value, Seconds))
  55.                 {
  56.                     ts = ts.Add(TimeSpan.FromSeconds(double.Parse(match.Groups[Quantity].Value)));
  57.                 }
  58.                 else
  59.                 {
  60.                     ts = ts.Add(TimeSpan.FromMinutes(double.Parse(match.Groups[Quantity].Value)));
  61.                 }
  62.             }
  63.             return ts;
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement