I was wondering how I would convert a string of four to six digits to a date in C#?
111110 would be 11 11 10
111 10 would be 11 1 10
1 1 10 would be 1 1 10
The pattern being mmddyy mmd yy m d yy
and the spaces are either ' ' or '\0' (the input I am given is not very clean)
This what I have so far and it works for all above cases, its just not very pretty:
Is there a more efficient solution to the above cases?
//Converts the given input string into a valid Date
private DateTime convertToDateFromString(string dateString)
{
int length = dateString.Length;
int month = 1;
int day = 1;
int year = 1;
bool gotMonth = false;
bool gotDay = false;
bool gotYear = false;
char c = ' ';
char peek = ' ';
string buffer = "";
DateTime bufferDate;
int count = 0;
try
{
//loop character by character through the string
for (int i = 0; i < dateString.Length; i++)
{
c = dateString[i];
if ((i + 1) < dateString.Length)
peek = dateString[i + 1];
else
peek = '\0';
if (c != ' ' && c != '\0')
{
buffer += c;
count++;
//Check if the month is done
if ((peek == ' ' || peek == '\0' || count == 2) && gotMonth == false)
{
count = 0;
gotMonth = true;
month = int.Parse(buffer);
buffer = null;
}
//Check if the day is done
else if ((peek == ' ' || peek == '\0' || count == 2) && gotDay == false && gotMonth == true)
{
count = 0;
gotDay = true;
day = int.Parse(buffer);
buffer = null;
}
//Check if the year is done
else if ((peek == ' ' || peek == '\0' || count == 2) && gotYear == false && gotMonth == true && gotDay == true)
{
count = 0;
gotYear = true;
year = int.Parse(buffer);
buffer = null;
if (year >= 80 && year <= 99)
year += 1900;
else if (year >= 0 && year <= 79)
year += 2000;
}
}
}
bufferDate = new DateTime(year, month, day);
}
catch (System.Exception ex)
{
bufferDate = new DateTime(1, 1, 1);
}
return bufferDate;
}
You discriminator here is the number of spaces. First get that:
string source = "....";
int spaceCount = source.Count(c => c == ' ');
Then create formatstrings for the expected range 0..2 . You can use the strings from the question except that you have to use M for the months:
var formats = new string[] { "MMddyy", "MMd yy", "M d yy" };
and then you can get your date :
DateTime r = DateTime.ParseExact(source, formats[spaceCount], null);
Add validations as required.
Try using
DateTime.TryParseExact(dateString, new string[] { "MMddyy", "MMd yy", "M d yy" }, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out result);
if you need to you can
dateString = dateString.Replace('\0', ' ');
before you begin
You should use one of the many Parse methods defined on DateTime.
These will take the string, an optional format string (describing the format of the datetime string) and an optional culture.
Take a look at Parse, ParseExact, TryParse and TryParseExact, some of which will take a string[] of the formats to try.
Additionally, here available string formats - standard and custom date and time format strings.
Related
I want to start by saying I am very new (1 week) into learning C#, so I sincerely apologize if this question is obvious. I do understand the reason for the exception, diverse.Length has to be 0 or greater. However, I am required to have the formula in my code so as to get the numbered-positions of the last 2 characters of an ever changing string (diverse).
Below, are 3 sets of code....
Firstly my working method.
static void Main(string[] args)
{
try
{
// Sample data - inputs 3 ints.
Console.WriteLine(Solution1(6, 1, 1));
Console.WriteLine(Solution1(1, 3, 1));
Console.WriteLine(Solution1(0, 1, 8));
Console.WriteLine(Solution1(5, 2, 4));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static string Solution1(int A, int B, int C)
{
string a = "a"; // a/b/c are added to string diverse when needed.
string b = "b";
string c = "c";
string diverse = "";
int totalLength = A + B + C; // Length of all 3 arrays
for(int i = 1; i <= totalLength; i++)
{
if (A >= B && A >= C && A > 0) { diverse = diverse + a; A = A - 1; }
if (B >= A && B >= C && B > 0) { diverse = diverse + b; B = B - 1; }
if (C >= A && C >= B && C > 0) { diverse = diverse + c; C = C - 1; }
}
return diverse;
}
What I am trying to do is add an additional check to my code. This check will take the printed letters and check to see if 2 of the same letter has previously been printed. If so, it will not print a 3rd. To do this I made a solution that would find the last 2 characters of the string (as I mentioned above) and compare it to the conditional check in the if statement.
Below is the code with this additional check, that I need to get working...
static string Solution1(int A, int B, int C)
{
string a = "a"; // a/b/c are added to string diverse when needed.
string b = "b";
string c = "c";
string diverse = "";
int totalLength = A + B + C; // Length of all 3 arrays
for (int i = 1; i <= totalLength; i++)
{
// Finds the last 2 characters in the diverse string.
int LastTwoChars = diverse.Length - 2;
string twoCharCheck = diverse.Substring(LastTwoChars, 2);
if (A > 0 && B < 2 && C < 2 && twoCharCheck != "aa")
{
diverse = diverse + a; A = A - 1;
}
if (B > 0 && A < 2 && C < 2 && twoCharCheck != "bb")
{
diverse = diverse + b; B = B - 1;
}
if (C > 0 && B < 2 && A < 2 && twoCharCheck != "cc")
{
diverse = diverse + c; C = C - 1;
}
}
return diverse;
}
You can have the string check only after you have at least 2 characters:
int LastTwoChars = diverse.Length - 2;
string twoCharCheck = LastTwoChars >= 0 ? diverse.Substring(LastTwoChars, 2) : string.Empty; // identical with if (LastTwoChars >= 0) twoCharCheck = diverse.Substring(LastTwoChars, 2); else twoCharCheck = string.Empty
if (A > 0 && B < 2 && C < 2 && (LastTwoChars < 0 || twoCharCheck != "aa")) // check the end only if you have at least 2 chars;
{
...
}
....
As you can see the code gets ugly very fast, so I would propose using a helper method:
// returns the last two chars in a string format, or a default user string
private static string LastTwoCharsOrDefault(string input, string default)
{
var lastTwoCharsIdx = input.Length - 2;
if (lastTwoCharsIdx > 0 )
{
return input.Substring(lastTwoCharsIdx, 2);
}
// we don't have at least 2 chars, so lets just return the default
return default;
}
Then you can change your code like this:
if (A > 0 && B < 2 && C < 2 && string.Equals("aa", LastTwoCharsOrDefault(diverse, "aa"), StringComparison.OrdinalIgnoreCase))
...
You can find more about string comparison here.
If I read your code right, you initialize Diverse as: string diverse = "";
and then execute string twoCharCheck = diverse.Substring(LastTwoChars, 2);
LastTwoChars is going to be -2 the first time through the loop.
This is a complete un compressed IP address 2001:0008:0000:CD30:0000:0000:0000:0101
I need to compress it like this
2001:8:0:CD30::101
But i was only able to compress the zeroes in blocks like this
2001:8:0:CD30:0:0:0:101
using this code
string output = "";
string a = textBox1.Text;
if (a.Length != 39 )
MessageBox.Show("Invalid IP please enter the IPv6 IP in this format 6cd9:a87a:ad46:0005:ad40:0000:5698:8ab8");
else
{
for (int i = 0; i < a.Length; i++)
{
if ((a[i] >= '1' && a[i] <= '9') || (Char.ToLower(a[i]) >= 'a' && Char.ToLower(a[i]) <= 'f') || ((i + 1) % 5 == 0 && a[i] == ':'))
{
output = output + a[i];
}
else if ((a[i]=='0' && a[i-1]==':') || (a[i]=='0' && a[i-1]=='0' && a[i-2]==':') || (a[i]=='0' && a[i-1]=='0' && a[i-2]=='0' && a[i-3]==':'))
{
}
else if (a[i] == '0')
{
output = output + a[i];
}
else
{
MessageBox.Show("Invalid IP please enter the IPv6 IP in this format 6cd9:a87a:ad46:0005:ad40:0000:5698:8ab8");
}
}
textBox2.Text = output;
}
Im using c# but i only need the programming logic about how can whole blocks of zeroes be deleted the problem is there could be more then 1 group of blocks containing all zeros in an ip but only one should be abbreviated.
Was far more tricky than I expected, but here you got the way to do it with regular expressions:
private static string Compress(string ip)
{
var removedExtraZeros = ip.Replace("0000","*");
//2001:0008:*:CD30:*:*:*:0101
var blocks = ip.Split(':');
var regex = new Regex(":0+");
removedExtraZeros = regex.Replace(removedExtraZeros, ":");
//2001:8:*:CD30:*:*:*:101
var regex2 = new Regex(":\\*:\\*(:\\*)+:");
removedExtraZeros = regex2.Replace(removedExtraZeros, "::");
//2001:8:*:CD30::101
return removedExtraZeros.Replace("*", "0");
}
If you would like to achieve the same result without using Regex:
public string Compress(string value)
{
var values = value.Split(",");
var ints = values.Select(i => int.Parse(i, System.Globalization.NumberStyles.HexNumber));
var result = ints.Select(Conversion.Hex);
return string.Join(":", result);
}
Didn't spend time on micro-optimizations (stackalloc, spans, etc.) but this gives an idea. For reference on optimization, you can look at the implementation of the IPAddress.Parse in the .net core. Do mind that result of the IPAddress.Parse will give a different result than the example above:
Compress -> 2001:8:0:CD30:0:0:0:101
IPAddress.Parse -> 2001:8:0:cd30::101
This could be "cleaned" up by moving into some object and other ideas.
Edit:
After chatting with one of my colleagues, I spent some time writing an "optimized" version of this. I haven't spent time cleaning up the code, so maybe one of the future edits will be even cleaner.
public string Compress(string value)
{
Span<char> chars = stackalloc char[value.Length];
const char zero = '0';
const char colon = ':';
int index = 0;
int positionInSegment = 0;
bool startsWithZero;
while (index < _originalValue.Length)
{
startsWithZero = value[index] == zero && positionInSegment == 0;
positionInSegment++;
if (startsWithZero)
{
if (index == value.Length - 1)
{
chars[index] = zero;
break;
}
if (value[index + 1] == colon)
{
chars[index] = zero;
positionInSegment = 0;
index++;
continue;
}
positionInSegment = 0;
index++;
continue;
}
if (value[index] == colon)
{
positionInSegment = 0;
chars[index] = colon;
index++;
continue;
}
chars[index] = value[index];
index++;
}
return chars.ToString();
}
I have also created a public gist for future references:
https://gist.github.com/DeanMilojevic/7b4f1d060ce8cfa191592694b11234d7
I want to calculate the count of total quarters (of a year) in the given time span.
for example:
start date = 1-june -2009
end date = 18-july-2011
count should be = 10.
one more
start date = 4-Jan-2009
end date = 27-oct -2010
count =8.
I have not been able to get the correct result.
Your example is wrong: there are only 7 quarters between 4-Jan-2009 and 27-oct -2010
You could simply add a reference to the Microsoft.VisualBasic.dll to your project and use DateDiff:
VB:
Public Shared Function getQuartersBetween(ByVal d1 As Date, ByVal d2 As Date) As Int32
Return DateDiff(DateInterval.Quarter, d1, d2)
End Function
C#:
public static int getQuartersBetween(System.DateTime d1, System.DateTime d2)
{
return Microsoft.VisualBasic.DateAndTime.DateDiff(DateInterval.Quarter, d1, d2);
}
or you could write your own implementation:
public class Quarter
{
public static long GetQuarters(DateTime dt1, DateTime dt2)
{
double d1Quarter = GetQuarter(dt1.Month);
double d2Quarter = GetQuarter(dt2.Month);
double d1 = d2Quarter - d1Quarter;
double d2 = (4 * (dt2.Year - dt1.Year));
return Round(d1 + d2);
}
private static int GetQuarter(int nMonth)
{
if (nMonth <= 3)
return 1;
if (nMonth <= 6)
return 2;
if (nMonth <= 9)
return 3;
return 4;
}
private static long Round(double dVal)
{
if (dVal >= 0)
return (long)Math.Floor(dVal);
return (long)Math.Ceiling(dVal);
}
}
or in VB.NET:
Public Class Quarter
Public Shared Function GetQuarters(ByVal dt1 As DateTime, ByVal dt2 As DateTime) As Long
Dim d1Quarter As Double = GetQuarter(dt1.Month)
Dim d2Quarter As Double = GetQuarter(dt2.Month)
Dim d1 As Double = d2Quarter - d1Quarter
Dim d2 As Double = (4 * (dt2.Year - dt1.Year))
Return Round(d1 + d2)
End Function
Private Shared Function GetQuarter(ByVal nMonth As Integer) As Integer
If nMonth <= 3 Then
Return 1
End If
If nMonth <= 6 Then
Return 2
End If
If nMonth <= 9 Then
Return 3
End If
Return 4
End Function
Private Shared Function Round(ByVal dVal As Double) As Long
If dVal >= 0 Then
Return CLng(Math.Floor(dVal))
End If
Return CLng(Math.Ceiling(dVal))
End Function
End Class
Code for you : Try below code
public static void Main()
{
//Application.Run(new XmlTreeDisplay());
int monthdiuff = monthDifference(Convert.ToDateTime("01/04/09"), Convert.ToDateTime("10/27/10"));
Console.WriteLine(monthdiuff);
int totalQuater = (monthdiuff / 3) + (monthdiuff%3);
Console.WriteLine(totalQuater);
Console.ReadLine();
}
private static int monthDifference(DateTime startDate, DateTime endDate)
{
int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
return Math.Abs(monthsApart);
}
Without some code to look over I can't help you find your exact problem.
If it were me I would probably find the difference between the dates in days, then divide by number of days in a quarter (91 or so). I'm sure that C# has some kind of date parsing module that can read in the dates as a string, giving you two objects that you could then subtract to find the difference in days.
This is one crude form of calculating the quarters based on your assumptions, you can choose to modify as it is it works good enough
DateTime dt1 = new DateTime(2009, 1, 1);// new DateTime(2009, 6, 1);
DateTime dt2 = new DateTime(2010, 10, 27);// new DateTime(2011, 7, 18);
if (dt1.Month < 4)
dt1 = new DateTime(dt1.Year,1,1);
else if (dt1.Month < 7)
dt1 = new DateTime(dt1.Year,4,1);
else if (dt1.Month < 10)
dt1 = new DateTime(dt1.Year,7,1);
else
dt1 = new DateTime(dt1.Year,10,1);
if (dt2.Month < 4)
dt2 = new DateTime(dt2.Year, 3, DateTime.DaysInMonth(dt2.Year, 3));
else if (dt2.Month < 7)
dt2 = new DateTime(dt2.Year, 6, DateTime.DaysInMonth(dt2.Year, 6));
else if (dt2.Month < 10)
dt2 = new DateTime(dt2.Year, 9, DateTime.DaysInMonth(dt2.Year, 9));
else
dt2 = new DateTime(dt2.Year, 12, DateTime.DaysInMonth(dt2.Year, 12));
TimeSpan ts = dt2 - dt1;
int quarters = (int) ts.TotalDays/90;
Console.WriteLine(quarters);
I am baselining the dates to the start and end of the quarters as you want and then assuming for 90 day quarter transforming the diff as int. Works for your mentioned examples,see if it suits you well enough
If the definition of a quarter is a 90-day difference, it's easy of course:
internal static int GetNumberOfQuarters(DateTime p_DtStart, DateTime p_DtEnd)
{
TimeSpan span = p_DtEnd.Subtract(p_DtStart);
return (int)span.TotalDays % 90;
}
But that's not what you're looking for. What about this (not tested but you'll get the idea)
internal static class DateTimeTools
{
internal static int GetNumberOfQuartersBetweenDates(DateTime startDate, DateTime endDate)
{
int iYearStart, iYearEnd, iMonthStart, iMonthEnd, iDayStart, iDayEnd;
iYearStart = startDate.Year;
iYearEnd = endDate.Year;
iMonthStart = startDate.Month;
iMonthEnd = endDate.Month;
iDayStart = startDate.Day;
iDayEnd = endDate.Day;
int iYearDiff, iQuarterDiff, iDayDiff;
iYearDiff = iYearEnd - iYearStart;
iQuarterDiff = iMonthEnd % 3 - iMonthStart % 3;
iDayDiff = iDayEnd - iDayStart;
int iNumOfQuarters = 0;
// at least a year difference?
if ((iYearDiff > 0 && iQuarterDiff > 0) || iYearDiff > 0 && iQuarterDiff == 0 && iDayDiff >= 0)
{
iNumOfQuarters = iYearDiff * 4 + iQuarterDiff;
}
// at least a quarter difference?
// within different years
if ((iYearDiff > 0 && iQuarterDiff <= 0)) // eg, dec 2010 - feb 2011 iYearDiff 1 iQuarterDiff -3
{
if ((iQuarterDiff == -3 && iDayDiff >= 0) || iQuarterDiff > -3)
{
iNumOfQuarters = iQuarterDiff + 4;
}
}
// within the same year
if (iYearDiff == 0 && iQuarterDiff > 0)
{
if ((iQuarterDiff == 1 && iDayDiff >= 0) || iQuarterDiff > 1)
{
iNumOfQuarters = iQuarterDiff;
}
}
return iNumOfQuarters;
}
}
Regards,
Nico
public static string GetQuarter(this DateTime date)
{
var quarterList = new List<string>();
if (date.Month >= 1 && date.Month <= 3)
return "Q1";
else if (date.Month >= 4 && date.Month <= 6)
return "Q1,Q2";
else if (date.Month >= 7 && date.Month <= 9)
return "Q1,Q2,Q3";
else
return "Q1,Q2,Q3,Q4";
}
This too can be used as an extension method if you are expecting to get a list of Quarters, You can later on use GetQuarter().Split(new[] { ',' }).Count() to get the count.
Easy formula to get quarters difference:
{
int firstQuarter = getQuarter(first);
int secondQuarter = getQuarter(second);
return 1 + Math.Abs(firstQuarter - secondQuarter);
}
private static int getQuarter(DateTime date)
{
return (date.Year * 4) + ((date.Month - 1) / 3);
}
I'm personally tired of reimplementing "a ticket" mechanism for my web projects for some one-time operations like account activation or password reset. I know it's simple but it requires me to keep (and persist!) two pieces of data: a ticket itself and an expiration date.
So I have this idea, to generate a unique string with embedded datetime (expiration date) in it, which we can check upon receiving as a part of url-request.
I've started with this:
var clearTicket = Convert.ToInt64(expiration.ToString("yyyyMMddhhmm")).ToString("x12") + key.ToString("N");
But I want it to be more compact. Something BaseXX-ish I suppose.
Any ideas how to implement this encoding/decoding efficiently (considering url-safe charset)?
It took me some time, so I hope it helps:
First of all, you should substract 200000000000 from "yyyyMMddhhmm", because you actually don't need the first two digits for the next 88 years.
Here is the implementation for encoding and decoding Base64, using only URL-safe characters.
If you have any questions, feel free to ask.
public string Base64Encode (Int64 Number)
{
string HelpString = "";
if (Number >= 64)
{
HelpString = Base64Encode(Number / 64);
}
return (HelpString += Base64EncodeHelper(Number % 64));
}
public string Base64EncodeHelper(Int64 Number)
{
string HelpString = "";
Number += 65;
if ((Number >= 65 && Number <= 90) || (Number >= 97 && Number <= 122)) // 0 - 25 and 32 - 57
{
HelpString = Convert.ToString((char)Number);
}
else if (Number >= 91 && Number <= 96) // 26 - 31
{
HelpString = Convert.ToString((char)(Number - 43));
}
else if (Number >= 123 && Number <= 126) // 58 - 61
{
HelpString = Convert.ToString((char)(Number - 69));
}
else if (Number == 127) // 62
{
HelpString = "-";
}
else // 63
{
HelpString = "_";
}
return (HelpString);
}
public Int64 Base64Decode(string Encoded)
{
Int64 Result = 0, HelpInt = 0;
int i = Encoded.Length - 1;
foreach (char Character in Encoded)
{
int CharInInt = (int)Character;
if (Character == '_')
{
HelpInt = 63;
}
else if (Character == '-')
{
HelpInt = 62;
}
else if (((CharInInt + 69) >= 123) && ((CharInInt + 69) <= 126))
{
HelpInt = CharInInt + 4;
}
else if (((CharInInt + 43) >= 91) && ((CharInInt + 43) <= 96))
{
HelpInt = CharInInt - 22;
}
else
{
HelpInt = CharInInt - 65;
}
Result += Convert.ToInt64((Math.Pow(64, Convert.ToDouble(i))) * HelpInt);
i--;
}
return Result;
}
You can extend string like this:
public static string ToURLParameter(this string text)
{
if (String.IsNullOrEmpty(text)) return "";
// to lowercase, trim extra spaces
text = text.Trim();
var len = text.Length;
var sb = new StringBuilder(len);
bool prevdash = false;
char c;
//
text = text.Replace('Å', 'A');
text = text.Replace('å', 'a');
text = text.Replace('Ä', 'A');
text = text.Replace('ä', 'a');
text = text.Replace('Ö', 'O');
text = text.Replace('ö', 'o');
for (int i = 0; i < text.Length; i++)
{
c = text[i];
if (c == ' ' || c == ',' || c == '.' || c == '/' || c == '\\' || c == '-')
{
if (!prevdash)
{
sb.Append('-');
prevdash = true;
}
}
else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z'))
{
sb.Append(c);
prevdash = false;
}
if (i == 80) break;
}
text = sb.ToString();
// remove trailing dash, if there is one
if (text.EndsWith("-"))
text = text.Substring(0, text.Length - 1);
return text;
}
and then you can use it on any variable that is string like:
string something = "asdfljasdklf";
<%= something.ToUrlParameter() %>
I've finally created a solution using techniques described in these answers:
Compressing big number (or string) to small value
How do you convert Byte Array to Hexadecimal String, and vice versa?
Works great!
Thanks you all for your help!
I have an application written in .NETMF that requires that I be able to parse an RFC822-Datetime.
Normally, this would be easy, but NETMF does not have a DateTime.parse() method, nor does it have some sort of a pattern matching implementation, so I'm pretty much stuck.
Any ideas?
EDIT: "Intelligent" solutions are probably needed. Part of the reason this is difficult is that the datetime in question has a tendency to have extra spaces in it (but only sometimes). A simple substring solution might work one day, but fail the next when the datetime has an extra space somewhere between the parts. I do not have control over the datetime, it is from the NOAA.
Good ol' string manipulation:
Sun, 06 Jun 2010 20:07:44 +0000
1 2 3
0123456789012345678901234567890
string x = Sanitize(" Sun, 06 \t Jun 2010 \r\n 20:07:44 +0000 ");
int day = int.Parse(x.Substring(5, 2));
int month = Array.IndexOf(months, x.Substring(8, 3)) + 1;
int year = int.Parse(x.Substring(12, 4));
int hour = int.Parse(x.Substring(17, 2));
int minute = int.Parse(x.Substring(20, 2));
int second = int.Parse(x.Substring(23, 2));
int offsetSgn = (x[26] == "-") ? -1 : 1;
int offsetHour = int.Parse(x.Substring(27, 2));
int offsetMinute = int.Parse(x.Substring(29, 2));
DateTime result = new DateTime(year, month, day, hour, minute, second, 0);
TimeSpan offset = new TimeSpan(offsetHour, offsetMinute, 0);
// TODO: add offset...
with
string[] months = new string[12];
months[0] = "Jan";
months[1] = "Feb";
months[2] = "Mar";
months[3] = "Apr";
months[4] = "May";
months[5] = "Jun";
months[6] = "Jul";
months[7] = "Aug";
months[8] = "Sep";
months[9] = "Oct";
months[10] = "Nov";
months[11] = "Dec";
and
string Sanitize(string s)
{
if (s == null)
{
return null;
}
char[] buffer = new char[s.Length];
int pos = 0;
bool inSpace = true;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n')
{
if (!inSpace)
{
buffer[pos] = ' ';
pos++;
inSpace = true;
}
}
else
{
buffer[pos] = s[i];
pos++;
inSpace = false;
}
}
return new string(buffer, 0, pos);
}