Skip to content

Instantly share code, notes, and snippets.

@sassembla
Last active August 29, 2015 14:02
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 sassembla/6653b6335f4fa44d6945 to your computer and use it in GitHub Desktop.
Save sassembla/6653b6335f4fa44d6945 to your computer and use it in GitHub Desktop.
TimeAssert for Unity
/*
example
"2014/06/23 3:05:34".TimeAssert(30, "time over!");
*/
using System;
using System.Globalization;
using UnityEngine;
static class Assertions {
/**
assert which extends string of date
*/
public static void TimeAssert (this string writtenDate, int limitSec, string reason) {
DateTime parsedDate;
var fullhead_time_result = DateTime.TryParseExact(writtenDate, "yyyy/MM/dd hh:mm:ss", null, DateTimeStyles.None, out parsedDate);
if (!fullhead_time_result) {
var no_head_time_result = DateTime.TryParseExact(writtenDate, "yyyy/MM/dd h:mm:ss", null, DateTimeStyles.None, out parsedDate);
if (!no_head_time_result) {
return;
}
}
var now = DateTime.Now;
var diff = now - parsedDate;
var diffSec = Math.Floor(diff.TotalSeconds);
if (diffSec < limitSec) {
return;
}
OutputStackThenDown(reason + " passed:" + (limitSec - diffSec) + "sec");
}
private static void OutputStackThenDown (string reason) {
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(true);
// at least 2 stack exists in st. 0 is "System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(true);", 1 is where the assertion faild.
var assertFaildPointDescription = st.GetFrame(2).ToString();
// get specific data from stacktrace.
var descriptions = assertFaildPointDescription.Split(':');
var fileName = descriptions[2].Split(' ')[1];
var line = descriptions[3];
Debug.LogError("A:" + fileName + ":" + line + ":" + reason);
// broke up
Debug.Break();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment