using System;
using System.Linq;
using System.Text.RegularExpressions;
class Test
{
static void Main()
{
string[] ArrayElement = new string[6];
ArrayElement[0]= "1:05:00 PM 06/25/2013 On True";
ArrayElement[1]= "1:10:00 PM 06/25/2013 Off True";
ArrayElement[2]= "1:15:00 PM 06/25/2013 On True";
ArrayElement[3]= "1:20:00 PM 06/25/2013 Off True";
ArrayElement[4]= "1:25:00 PM 06/25/2013 On True";
ArrayElement[5]= "1:30:00 PM 06/25/2013 Off True";
Console.WriteLine(Validate(ArrayElement)); // True
ArrayElement[2]= "1:15:00 PM 05/25/2013 On True";
Console.WriteLine(Validate(ArrayElement)); // False
Console.ReadKey();
}
static bool Validate(string[] state)
{
string f = "h:mm:ss tt MM/dd/yyyy";
// extract time stamps and make sure they're in sequence
DateTime[] stamps = state
.Select(s => DateTime.ParseExact(s.Substring(0, s.LastIndexOf(" ")), f, null)).ToArray();
for(int i = 1; i < stamps.Length; i++)
{
if(stamps[i] < stamps[i-1]) return false;
}
// extract On/Off's and validate them
state = state.Select(s => s.Substring(s.IndexOf("O"))).ToArray();
string text = String.Join("", state);
string regExp = "^(OnOff)+$";
return Regex.IsMatch(text, regExp);
}
}
David SmithPosted Aug 22, 2013, 5:11 PM
9/25/2013 Incorrect
09/25/2013 correct
I am enduring the format is using the "MM/dd/yyyy" by doing this below.
sb.Append(c.ColorTimeTag.ToString("MM/dd/yyyy HH:mm:ss tt"));
Thanks vuples.
VulpesPosted Aug 22, 2013, 11:20 AM
So, if either of these are possible, I'd stick with this.
David SmithPosted Aug 22, 2013, 10:33 AM
David SmithPosted Aug 22, 2013, 10:33 AM
David SmithPosted Aug 22, 2013, 10:23 AM
However this work "M/d/yyyy" I am very confuse why this work and this did not work "MM/dd/yyyy".
VulpesPosted Aug 22, 2013, 3:52 AM
With 'M/d/yyyy' you use one digit if it's a single digit month or day or two digits otherwise. So today in this format would be 22/8/2013.
Personally, I prefer to use the 'MM/dd/yyyy' format because this always has the same length. However, if the data is being supplied externally, you may not have a choice.
David SmithPosted Aug 21, 2013, 9:38 PM
private const string TIMESTAMP_FORMAT = "M/d/yyyy HH:mm:ss tt";
what is the difference between "M/d/yyyy" and "MM/ds/yyyy"
VulpesPosted Aug 21, 2013, 6:39 PM
MM/dd/yyyy
However, I've noticed one or two dates in your posts which only had one digit in the day.
Maybe we should be using M/d/yyyy instead?
David SmithPosted Aug 21, 2013, 6:17 PM
VulpesPosted Aug 21, 2013, 5:27 PM
if (!ArrayElement.SequenceEqual(orderedState))
{
return false;
}
David SmithPosted Aug 21, 2013, 4:23 PM
string [] ArrayElement = state.Select(s => s.Substring(0, s.IndexOf("O") - 1)).ToArray();
// extract time stamps and make sure they're in sequence
var orderedState = ArrayElement.OrderBy(s => DateTime.ParseExact(s, TIMESTAMP_FORMAT, null));
if (!history.SequenceEqual(orderedState))
{
return false;
}
David SmithPosted Aug 21, 2013, 4:11 PM
string f = "MM/dd/yyyy h:mm:ss tt";
"8/21/2013 10:05:09 AM On True"
However, I dont think the substring is working correctly embedded in the ParseDate function below. I the error is complaining about the entire string "8/21/2013 10:05:09 AM On True". When you think "ON" and "TRUE" is an invalid datetime format if that make sense.
var orderedState = state.OrderBy(s => DateTime.ParseExact(s.Substring(0, s.IndexOf("O") - 1), TIMESTAMP_FORMAT, null)); I get an error on this line.
if (!history.SequenceEqual(orderedState))
{
return false;
}
VulpesPosted Aug 21, 2013, 3:59 PM
The strings need to be in the exact format specified for this to work.
VulpesPosted Aug 21, 2013, 3:56 PM
David SmithPosted Aug 21, 2013, 3:50 PM
This is my exact code below.
Also this is my exact string below when I look in the debugger.
"8/21/2013 10:05:09 On True", I notice that I dont have the AM/PM in the string. That could be the problem also
private const string TIMESTAMP_FORMAT = "h:mm:ss tt MM/dd/yyyy";
private static bool Validate(string[] state)
{
var orderedState = state.OrderBy(s => DateTime.ParseExact(s.Substring(0, s.IndexOf("O") - 1), TIMESTAMP_FORMAT, null)); I get an error on this line.
if (!history.SequenceEqual(orderedState))
{
return false;
}
else
{
return true;
}
}
VulpesPosted Aug 21, 2013, 3:46 PM
I suppose ArgumentException is the closest as there's something wrong with the array that's passed in:
throw new ArgumentException("Array elements are not in order");
David SmithPosted Aug 21, 2013, 3:35 PM
VulpesPosted Aug 21, 2013, 3:21 PM
The AM/PM desigmator is catered for in the format string you provided yourself:
string f = "h:mm:ss tt MM/dd/yyyy";
I suppose you could replace this part of the code:
with this:
David SmithPosted Aug 21, 2013, 3:18 PM
This logic works below, which is for testing purposes.
string f = "h:mm:ss tt MM/dd/yyyy";
DateTime dt = DateTime.ParseExact(test, f, null);
Console.WriteLine(test);
This logic is not working at all below, I am getting string is not in a valid datetime format.
DateTime[] timestamps = ArrayElement.Select(s => DateTime.ParseExact(s.Substring(0, s.IndexOf("O") - 1), f, null)).ToArray();
David SmithPosted Aug 21, 2013, 2:55 PM
David SmithPosted Aug 21, 2013, 2:54 PM
David SmithPosted Aug 21, 2013, 2:50 PM
VulpesPosted Aug 21, 2013, 1:56 PM
The output should be:
True
False