|
-
Sep 24th, 2012, 08:56 AM
#1
Thread Starter
Hyperactive Member
Iterate through 2 dates
I'm stuck in here
My goal is to iterate through date
and compare it to string from a line read from a txt file
Here is my code
Code:
private List<DateTime> GetDateRange(DateTime StartingDate, DateTime EndingDate)
{
if (StartingDate > EndingDate)
{
return null;
}
List<DateTime> rv = new List<DateTime>();
DateTime tmpDate = StartingDate;
do
{
rv.Add(tmpDate);
tmpDate = tmpDate.AddDays(1);
} while (tmpDate <= EndingDate);
return rv;
}
private void button3_Click(object sender, EventArgs e)
{
StreamReader strReader = new StreamReader(Application.StartupPath + "\\Logs\\Sample Logs.txt");
string [] strArray = new string[3];
string sLine = "";
foreach (DateTime date in GetDateRange(dtpStart .Value,dtpEnd.Value))
{
//lb.Items.Add(string.Format ("{0:yyyy/MM/dd}",date.Date));
while (strReader.EndOfStream == false)
{
sLine = strReader.ReadLine();
strArray = sLine.Split(',');
if (strArray[1] == string.Format("{0:yyyy/MM/dd}", date.Date))
{
lb.Items.Add(strArray[1] + "*" + strArray[2]);
}
}
}
}
It seems that my loop to iterate days on dates is stuck at first day only.
It just execute then read a file, after a file, going to next day is stop.
Here is the content of my txtfile
Code:
1,2011/12/1,7:31,0
2,2011/12/1,7:32,0
3,2011/12/1,7:33,0
4,2011/12/1,7:34,0
1,2011/12/1,11:50,0
2,2011/12/1,11:51,0
3,2011/12/1,11:52,0
4,2011/12/1,11:53,0
1,2011/12/1,13:01,0
2,2011/12/1,13:02,0
3,2011/12/1,13:03,0
4,2011/12/1,13:04,0
1,2011/12/1,17:31,0
2,2011/12/1,17:32,0
3,2011/12/1,17:33,0
4,2011/12/1,17:34,0
1,2011/12/2,7:31,0
2,2011/12/2,7:32,0
3,2011/12/2,7:33,0
4,2011/12/2,7:34,0
1,2011/12/2,11:50,0
2,2011/12/2,11:51,0
3,2011/12/2,11:52,0
4,2011/12/2,11:53,0
1,2011/12/2,13:01,0
2,2011/12/2,13:02,0
3,2011/12/2,13:03,0
4,2011/12/2,13:04,0
1,2011/12/2,17:31,0
2,2011/12/2,17:32,0
3,2011/12/2,17:33,0
4,2011/12/2,17:34,0
1,2011/12/3,7:31,0
2,2011/12/3,7:32,0
3,2011/12/3,7:33,0
4,2011/12/3,7:34,0
1,2011/12/3,11:50,0
2,2011/12/3,11:51,0
3,2011/12/3,11:52,0
4,2011/12/3,11:53,0
1,2011/12/3,13:01,0
2,2011/12/3,13:02,0
3,2011/12/3,13:03,0
4,2011/12/3,13:04,0
1,2011/12/3,17:31,0
2,2011/12/3,17:32,0
3,2011/12/3,17:33,0
4,2011/12/3,17:34,0
1,2011/12/4,7:31,0
2,2011/12/4,7:32,0
3,2011/12/4,7:33,0
4,2011/12/4,7:34,0
1,2011/12/4,11:50,0
2,2011/12/4,11:51,0
3,2011/12/4,11:52,0
4,2011/12/4,11:53,0
1,2011/12/4,13:01,0
2,2011/12/4,13:02,0
3,2011/12/4,13:03,0
4,2011/12/4,13:04,0
1,2011/12/4,17:31,0
2,2011/12/4,17:32,0
3,2011/12/4,17:33,0
4,2011/12/4,17:34,0
1,2011/12/5,7:31,0
2,2011/12/5,7:32,0
3,2011/12/5,7:33,0
4,2011/12/5,7:34,0
1,2011/12/5,11:50,0
2,2011/12/5,11:51,0
3,2011/12/5,11:52,0
4,2011/12/5,11:53,0
1,2011/12/5,13:01,0
2,2011/12/5,13:02,0
3,2011/12/5,13:03,0
4,2011/12/5,13:04,0
1,2011/12/5,17:31,0
2,2011/12/5,17:32,0
3,2011/12/5,17:33,0
4,2011/12/5,17:34,0
1,2011/12/6,7:31,0
2,2011/12/6,7:32,0
3,2011/12/6,7:33,0
4,2011/12/6,7:34,0
1,2011/12/6,11:50,0
2,2011/12/6,11:51,0
3,2011/12/6,11:52,0
4,2011/12/6,11:53,0
1,2011/12/6,13:01,0
2,2011/12/6,13:02,0
3,2011/12/6,13:03,0
4,2011/12/6,13:04,0
1,2011/12/6,17:31,0
2,2011/12/6,17:32,0
3,2011/12/6,17:33,0
4,2011/12/6,17:34,0
1,2011/12/7,7:31,0
2,2011/12/7,7:32,0
3,2011/12/7,7:33,0
4,2011/12/7,7:34,0
1,2011/12/7,11:50,0
2,2011/12/7,11:51,0
3,2011/12/7,11:52,0
4,2011/12/7,11:53,0
1,2011/12/7,13:01,0
2,2011/12/7,13:02,0
3,2011/12/7,13:03,0
4,2011/12/7,13:04,0
1,2011/12/7,17:31,0
2,2011/12/7,17:32,0
3,2011/12/7,17:33,0
4,2011/12/7,17:34,0
1,2011/12/8,7:31,0
2,2011/12/8,7:32,0
3,2011/12/8,7:33,0
4,2011/12/8,7:34,0
1,2011/12/8,11:50,0
2,2011/12/8,11:51,0
3,2011/12/8,11:52,0
4,2011/12/8,11:53,0
1,2011/12/8,13:01,0
2,2011/12/8,13:02,0
3,2011/12/8,13:03,0
4,2011/12/8,13:04,0
1,2011/12/8,17:31,0
2,2011/12/8,17:32,0
3,2011/12/8,17:33,0
4,2011/12/8,17:34,0
1,2011/12/9,7:31,0
2,2011/12/9,7:32,0
3,2011/12/9,7:33,0
4,2011/12/9,7:34,0
1,2011/12/9,11:50,0
2,2011/12/9,11:51,0
3,2011/12/9,11:52,0
4,2011/12/9,11:53,0
1,2011/12/9,13:01,0
2,2011/12/9,13:02,0
3,2011/12/9,13:03,0
4,2011/12/9,13:04,0
1,2011/12/9,17:31,0
2,2011/12/9,17:32,0
3,2011/12/9,17:33,0
4,2011/12/9,17:34,0
1,2011/12/10,7:31,0
2,2011/12/10,7:32,0
3,2011/12/10,7:33,0
4,2011/12/10,7:34,0
1,2011/12/10,11:50,0
2,2011/12/10,11:51,0
3,2011/12/10,11:52,0
4,2011/12/10,11:53,0
1,2011/12/10,13:01,0
2,2011/12/10,13:02,0
3,2011/12/10,13:03,0
4,2011/12/10,13:04,0
1,2011/12/10,17:31,0
2,2011/12/10,17:32,0
3,2011/12/10,17:33,0
4,2011/12/10,17:34,0
1,2011/12/11,7:31,0
2,2011/12/11,7:32,0
3,2011/12/11,7:33,0
4,2011/12/11,7:34,0
1,2011/12/11,11:50,0
2,2011/12/11,11:51,0
3,2011/12/11,11:52,0
4,2011/12/11,11:53,0
1,2011/12/11,13:01,0
2,2011/12/11,13:02,0
3,2011/12/11,13:03,0
4,2011/12/11,13:04,0
1,2011/12/11,17:31,0
2,2011/12/11,17:32,0
3,2011/12/11,17:33,0
4,2011/12/11,17:34,0
1,2011/12/12,7:31,0
2,2011/12/12,7:32,0
3,2011/12/12,7:33,0
4,2011/12/12,7:34,0
1,2011/12/12,11:50,0
2,2011/12/12,11:51,0
3,2011/12/12,11:52,0
4,2011/12/12,11:53,0
1,2011/12/12,13:01,0
2,2011/12/12,13:02,0
3,2011/12/12,13:03,0
4,2011/12/12,13:04,0
1,2011/12/12,17:31,0
2,2011/12/12,17:32,0
3,2011/12/12,17:33,0
4,2011/12/12,17:34,0
1,2011/12/13,7:31,0
2,2011/12/13,7:32,0
3,2011/12/13,7:33,0
4,2011/12/13,7:34,0
1,2011/12/13,11:50,0
2,2011/12/13,11:51,0
3,2011/12/13,11:52,0
4,2011/12/13,11:53,0
1,2011/12/13,13:01,0
2,2011/12/13,13:02,0
3,2011/12/13,13:03,0
4,2011/12/13,13:04,0
1,2011/12/13,17:31,0
2,2011/12/13,17:32,0
3,2011/12/13,17:33,0
4,2011/12/13,17:34,0
1,2011/12/14,7:31,0
2,2011/12/14,7:32,0
3,2011/12/14,7:33,0
4,2011/12/14,7:34,0
1,2011/12/14,11:50,0
2,2011/12/14,11:51,0
3,2011/12/14,11:52,0
4,2011/12/14,11:53,0
1,2011/12/14,13:01,0
2,2011/12/14,13:02,0
3,2011/12/14,13:03,0
4,2011/12/14,13:04,0
1,2011/12/14,17:31,0
2,2011/12/14,17:32,0
3,2011/12/14,17:33,0
4,2011/12/14,17:34,0
1,2011/12/15,7:31,0
2,2011/12/15,7:32,0
3,2011/12/15,7:33,0
4,2011/12/15,7:34,0
1,2011/12/15,11:50,0
2,2011/12/15,11:51,0
3,2011/12/15,11:52,0
4,2011/12/15,11:53,0
1,2011/12/15,13:01,0
2,2011/12/15,13:02,0
3,2011/12/15,13:03,0
4,2011/12/15,13:04,0
1,2011/12/15,17:31,0
2,2011/12/15,17:32,0
3,2011/12/15,17:33,0
4,2011/12/15,17:34,0
That is an attendace records.
My plan is to save that data to database before I manage it.
I need to search and record all in and out for a given date.
Thanks
VB 6.0 = "Self-Study" Then
vb.NET = "Self-Study" Then
C# = 'on going study.....
-
Sep 24th, 2012, 10:59 AM
#2
Re: Iterate through 2 dates
I'm not entirely sure, that I understand completely, what you need. But here is my guess at something you might be able to use:
The function getemptyattendance below will create an empty attendance-list (SortedList<DateTime, Int32>) in the specified date-interval. This attendance-list can subsequently be used to update attendances for specific dates. The button_click event will create an empty attendance-list in the date-interval specified by the DateTimePickers dtpStart and dtpEnd, and read all the lines of the attendance-file. The attendance is updated by one (didn't really know if attendance was the first or last element or possibly neither) for each line in the file, that has a date occuring in the attendance-list. The complete attendance list is finally written out to the console-window.
CSharp Code:
private void button3_Click(object sender, EventArgs e)
{
// This line reads all lines in the file at one time. There is nothing wrong with using a streamreader and reading
// one line at the time, but unless the file is really large, the code below is a bit more readable.
string[] datelines = File.ReadAllLines(Application.StartupPath + "\\Logs\\Sample Logs.txt");
string[] lineelements;
DateTime curdate;
SortedList<DateTime, Int32> attendancelist = getemptyattendancelist(dtpStart.Value, dtpEnd.Value);
foreach (string line in datelines)
{
lineelements = line.Split(',');
if (lineelements.Length == 4)
{
if (DateTime.TryParse(lineelements[1] + " " + lineelements[2], out curdate))
{
if (attendancelist.ContainsKey(curdate.Date))
{
// The current line in the file is in the required format, and the date in within the range
// of the attendancelist. The line is stored in the variable [line], and the 4 parts of the
// line, seperated by kommas, are stored in [lineelements].
//
// The first and last elements can be found by using Int32.TryParse(lineelements[0], out ivar)
// and Int32.TryParse(lineelements[3], out ivar), where ivar is declared as an int32 variable.
// Or you can use Convert.ToInt32(lineelements[0 or 3 respectively]), if you are sure that no
// errors can occur.
//
// The current attendance entry can be updated with the following line:
attendancelist[curdate.Date] += 1;
}
}
else
{
// The second and third elements combined dosen't constitute a correctly formatted datetime.
}
}
else
{
// The line dosen't have the correct number of parts seperated by komma.
}
}
Console.WriteLine("The file provided the following attendance:");
Console.WriteLine("-------------------------------------------");
foreach (DateTime loopdate in attendancelist.Keys)
Console.WriteLine(string.Format("Date: {0}, attendance: {1}", loopdate.ToShortDateString(), attendancelist[loopdate]));
}
private SortedList<DateTime, Int32> getemptyattendancelist(DateTime startdate, DateTime enddate)
{
if (startdate.Date > enddate.Date) return null;
int days = enddate.Subtract(startdate).Days;
SortedList<DateTime, Int32> rv = new SortedList<DateTime,int>();
foreach (DateTime loopdate in Enumerable.Range(0, days + 1).Select(i => startdate.Date.AddDays(i)))
rv.Add(loopdate, 0);
return rv;
}
Regards Tom
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 24th, 2012, 10:37 PM
#3
Thread Starter
Hyperactive Member
Re: Iterate through 2 dates
THanks TOm.
Or is there any best way to manage logs of attendance?
The way that all log with same date will be save as single line.
Something like this
1,2011/12/1,7:31,0
2,2011/12/1,7:32,0
3,2011/12/1,7:33,0
4,2011/12/1,7:34,0
1,2011/12/1,11:50,0
2,2011/12/1,11:51,0
3,2011/12/1,11:52,0
4,2011/12/1,11:53,0
1,2011/12/1,13:01,0
2,2011/12/1,13:02,0
3,2011/12/1,13:03,0
4,2011/12/1,13:04,0
1,2011/12/1,17:31,0
2,2011/12/1,17:32,0
3,2011/12/1,17:33,0
4,2011/12/1,17:34,0
I want to save that on my database as
My table format.
ID------Date--------AMin--------AMout-------PMin-------PMout
1----2011/12/1----7:31--------11:50--------13:01------17:31
2----2011/12/1----7:32--------11:51--------13:02------17:32
so on....
Last edited by dr_aybyd; Sep 24th, 2012 at 11:07 PM.
VB 6.0 = "Self-Study" Then
vb.NET = "Self-Study" Then
C# = 'on going study.....
-
Sep 25th, 2012, 10:18 AM
#4
Re: Iterate through 2 dates
In my opinion, you should go ahead and create a database. Making example code based on a text-file is all good and well, but pretty much all of that code will be useless once you get a database to store the data in. You will most likely rely on a strongly typed dataset and use SQL to retrieve:
* Logins for a specified person (possibly over a given period in time).
* Logins for a specified period.
* Missing logins for a specified person (possibly over a given period).
* etc.
eliminating the need to store text-files and/or declare classes or variables to maintain the data.
I implemented a similar system for a youth-centre a couple of years ago. The data basically consisted of a table of persons, each uniquely defined by an id, and a table of logins using inner join on the person-table on the id and keeping a login-datetime (in your case also a logout-datetime or possibly login-duration). Each young person had a magnetic card, and when they scanned it in a given club, the information was sent to a central server storing the information for all 21 clubs.
The system allowed for a wide range of statistics to be drawn, cards being banned etc., but basically it is similar in nature to your project.
Start out with a very simple database consisting of just 2 tables with as few columns as possible - for example:
Person_Table:
* Id (integer, primary key)
* Name (string)
Attendance_Table:
(* Possible autonumber for primary key or no key at all - depending on whether you plan on deleting/editing rows or permanently keep them as is).
* Person_Id (integer - refers to the person-table)
* Login (DateTime - the date and time when the student arrives).
* Additional columns can be added as needed, but no more columns are required to make a working example.
Once you have the database created with the 2 tables, you will find that using databases is pretty well-supported and not that difficult in the .Net framework. The most difficult task (IMO) will be creating SQL-sentences to extract the required information - ie. attendance in professor X's classes in May, number of days Bryan was absent in any class in 2012 etc. etc.
Regards Tom
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 25th, 2012, 08:10 PM
#5
Thread Starter
Hyperactive Member
Re: Iterate through 2 dates
I'll take your advice sir Thom.
Maybe I need to recode the previous system(made by other coder) that generate a logs from biometric device.
I will make save directly to database instead of making it in text file format.
I'll update you later about the progress.
VB 6.0 = "Self-Study" Then
vb.NET = "Self-Study" Then
C# = 'on going study.....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|