private void RefreshElapsed() { if (goalDateInt != 0) { // Call GuardTime GuardTime guard = new GuardTime(); // Tell GuardTime to give us the DateTime of the last event and set that to our lastEvent dateTime. lastEvent = guard.EpochToDateTime(eventDateInt); // Create a new timer Timer timeSince = new Timer(); // Set the Interval timeSince.Interval = 100; // Set the event handler timeSince.Tick += new EventHandler(timeSince_Tick); // Declare a DateTime of now DateTime now = DateTime.Now; // Set the timespan equal to the time now, minus the time of the last event. TimeSpan ts = now - lastEvent; timeSinceLabel.Text = ts.ToString(""); timeSince.Start(); } }
// Refresh the goal date label. private void RefreshGoalDate() { GuardTime guard = new GuardTime(); // Tell GuardTime to set the config. guard.setConfig(); // Initialize SQLize class and pass it the command. SQLize calc = new SQLize("SELECT * FROM Dates ORDER BY datetime DESC LIMIT 1"); // Tell SQLize to return OneDate and set it equal to a string. string aDate = calc.Read_OneDate(); // If the date is null, we don't want to be doing operations on it. // This could happen when Guard starts for the first time. if (aDate != null) { // Parse the string into a 64 bit integer. Int64 n; Int64.TryParse(aDate, out n); // Assign our original date -> necessary for the counting UP process. eventDateInt = n; // Tell guard to calculate the release date and set the goalDate string. goalDate = guard.CalcReleaseDate(n); // set our int value goalDateInt = guard.CalcReleaseDateInt(n); // Display the goal date without GMT information nextGoalLabel.Text = guard.RemoveGMT(goalDate); } }
private void RefreshCountdown() { if (goalDateInt != 0) { // Call GuardTime GuardTime guard = new GuardTime(); // Tell GuardTime to convert the epoch time into our goal date time object declared at the start of the MainWindow. goal = guard.EpochToDateTime(goalDateInt); // Create a new timer Timer countDown = new Timer(); // Set the Interval countDown.Interval = 100; // Set the event handler countDown.Tick += new EventHandler(countDown_Tick); TimeSpan ts = goal.Subtract(DateTime.Now); timeLeftLabel.Text = ts.ToString(""); countDown.Start(); } }
// Refresh the table by updating the gregorian table in sqlite and then reading it into the table. private void RefreshTable() { // Initialize Guardtime GuardTime guard = new GuardTime(); // Populate the Gregorian Table SQLize GenerateTimes = new SQLize("SELECT datetime FROM (SELECT * FROM Dates ORDER BY date_id DESC) ORDER BY date_id"); // Call PopulateTimestamps to update the Gregorian table GenerateTimes.PopulateTimestamps(); // Create a connection to the DB. SQLiteConnection m_dbConnection; m_dbConnection = new SQLiteConnection("Data Source=GuardDB.sqlite;Version=3;"); m_dbConnection.Open(); // Create our adapter var adapter = new SQLiteDataAdapter("SELECT day_of_week, month, day, year, timestamp FROM Gregorian", m_dbConnection); // Fill the dataset object adapter.Fill(ds); // Close the connection to the DB m_dbConnection.Close(); // Show this in the Data Grid View guardDatesDGV.DataSource = ds.Tables[0].DefaultView; }
private void RefreshData() { // Clear the textboxes achTextBox.Clear(); datesTextBox.Clear(); // Initialize GuardTime. GuardTime guard = new GuardTime(); // Issue a new SELECT query for the Achievements table. SQLize refreshAch = new SQLize("SELECT * FROM (SELECT * FROM Achievements ORDER BY ach_id DESC LIMIT 17) ORDER BY ach_id"); // Call Read_Ach to read the data. refreshAch.Read_Ach(); // Set the output of SQLize's Get_Out method to the Achievements textbox.text achTextBox.Text = refreshAch.Get_Out(); // Issue a new SELECT query for the Dates table. SQLize refreshDates = new SQLize("SELECT datetime FROM (SELECT * FROM Dates ORDER BY date_id DESC LIMIT 17) ORDER BY date_id"); // Call Read_Dates to read the data. refreshDates.Read_Dates(); // Set the output of SQLize's Get_Out method to the Dates textbox.text datesTextBox.Text = refreshDates.Get_Out(); }
private void WeeklyAverage() { Int64 ttsearch = 0; Int64 result = 0; SQLiteConnection m_dbConnection; m_dbConnection = new SQLiteConnection("Data Source=GuardDB.sqlite;Version=3"); m_dbConnection.Open(); // Create UnixTimestamp GuardTime gt = new GuardTime(); var now = DateTime.Today; ttsearch = gt.DateToEpoch(now); ttsearch = ttsearch - 2592000; SQLiteCommand findInstances = new SQLiteCommand("SELECT COUNT(datetime) FROM dates WHERE datetime > " + ttsearch, m_dbConnection); SQLiteDataReader getInstances = findInstances.ExecuteReader(); while (getInstances.Read()) { result = getInstances.GetInt64(0); } result = result / 5; fiveWeekAvgLabel.Text = result.ToString(); m_dbConnection.Close(); }
// PopulateTimeStamps // // This reads dates from the dates table into the 'aList' List. Effectively copies the Dates Table! public void PopulateTimestamps() { // Create a counter variable int i = 1; // Start GuardTime GuardTime guard = new GuardTime(); // Create a connection to the DB. SQLiteConnection m_dbConnection; m_dbConnection = new SQLiteConnection("Data Source=GuardDB.sqlite;Version=3;"); m_dbConnection.Open(); // Create our command SQLiteCommand command = new SQLiteCommand(commandString, m_dbConnection); // Start the reader SQLiteDataReader reader = command.ExecuteReader(); // While the reader has data to read, add the value to the list. while (reader.Read()) { // Make some holder values; string month; string day; string day_of_week; string year; string time; // Test value for reader.read Int64 test = reader.GetInt64(0); // Initialize a DTO DateTimeOffset dto = new DateTimeOffset(); // Get Guard to give us the DTO of the current line dto = guard.EpochToDTO(reader.GetInt64(0)); // Pull the information // month = guard.MonthToString(dto.Month); month = dto.ToString("MMMM"); day = dto.Day.ToString(); day_of_week = dto.DayOfWeek.ToString(); year = dto.Year.ToString(); time = dto.TimeOfDay.ToString(); // Create a SQLite Command to insert stuff SQLiteCommand populate = new SQLiteCommand("INSERT OR IGNORE INTO Gregorian (greg_id, month, day, day_of_week, year, timestamp)" + "VALUES (" + i + "," + "'" + month + "'" + "," + "'" + day + "'" + "," + "'" + day_of_week + "'" + "," + "'" + year + "'" + "," + "'" + time + "'" + ")", m_dbConnection); // Run that command! populate.ExecuteNonQuery(); // Increment the counter i++; } }
// CUSTOM BUTTON // // When the custom button is clicked, Guard updates a text file which contains configuration // information. This information pertains to the settings used to calculate the "Next Release" // time which can also be thought of as the next time a user is allowed to indulge in a craving. // // Custom settings take user defined values, se we need to check boxes and sanitize input before // passing this information to the CreateCFG method. private void btnUseCustom_Click(object sender, EventArgs e) { // First lets get some holder values. string c_years = txtBoxC_Years.Text; string c_months = txtBoxC_Months.Text; string c_days = txtBoxC_Days.Text; string c_hours = txtBoxC_Hours.Text; string c_minutes = txtBoxC_Minutes.Text; // Now lets fix any empty boxes. c_years = LetsCheck(c_years); c_months = LetsCheck(c_months); c_days = LetsCheck(c_days); c_hours = LetsCheck(c_hours); c_minutes = LetsCheck(c_minutes); // Now lets run a sanity check on that information and double check that the user hasn't input // any "dirty" data (eg: non-numerical data) c_years = SanityCheck(c_years, "Years"); c_months = SanityCheck(c_months, "Months"); c_days = SanityCheck(c_days, "Days"); c_hours = SanityCheck(c_hours, "Hours"); c_minutes = SanityCheck(c_minutes, "Minutes"); // Once the data is cleaned, we need to change everything over to seconds so we can use these // values in our calculations since we're using EpochTime now. This means switching these to // Int64 values. // // GuardTime has a method to do this, so initialze GuardTime GuardTime guard = new GuardTime(); // Now convert the values // The Gregorian Input function expects you to tell it whether we are converting years, months // days, hours, or minutes as the second argument. string ec_years = guard.GregorianInputToEpoch(c_years, "years"); string ec_months = guard.GregorianInputToEpoch(c_months, "months"); string ec_days = guard.GregorianInputToEpoch(c_days, "days"); string ec_hours = guard.GregorianInputToEpoch(c_hours, "hours"); string ec_minutes = guard.GregorianInputToEpoch(c_minutes, "minutes"); // Once the data is cleaned, update the config file! CreateCFG(ec_years, ec_months, ec_days, ec_hours, ec_minutes, "custom"); // Inform the user of the changes MessageBox.Show("Config file updated with the following settings:" + "\n" + c_years + " Years" + "\n" + c_months + " Months" + "\n" + c_days + " Days" + "\n" + c_hours + " Hours" + "\n" + c_minutes + " Minutes" , "Switched To Custom Config"); }
// READ_ACH() // This method reads the achievements from the Achievements table an formats it for use // in a textbox control. public void Read_Ach() { // Start GuardTime GuardTime guard = new GuardTime(); // Create a connection to the DB. SQLiteConnection m_dbConnection; m_dbConnection = new SQLiteConnection("Data Source=GuardDB.sqlite;Version=3;"); m_dbConnection.Open(); // Create our command SQLiteCommand command = new SQLiteCommand(commandString, m_dbConnection); // Start the reader SQLiteDataReader reader = command.ExecuteReader(); // While the reader has data to read, Feed it while (reader.Read()) { // Get our values. Int64 bestTime = reader.GetInt64(1); // If the record is 0 (ie has no timestamp data) don't do any of this because a best time doesn't exist. if (bestTime != 0) { // Grab our calculation // ***DEPRECATED*** The resultTime IS bestTime. // // Int64 resultTime = guard.CalcUnixTimeSpan(startTime, endTime); // Throw this into guards converter string s = guard.EpochLenToString(bestTime); // Concatenate our return string with nicely formatted text using Guards // pretty print option. aReturn += guard.PrettyPrintAch(s) + "\r\n"; // Concatenate our return string. //old method aReturn += guard.EpochLenToString(resultTime) + "\r\n"; } } // Close the connection to the DB m_dbConnection.Close(); }
// READ_DATES() // This method reads dates from the dates table into the 'aReturn' string and // includes formatting for new lines so that the text can be displayed in // a textbox control. public void Read_Dates() { // Start GuardTime GuardTime guard = new GuardTime(); // Create a connection to the DB. SQLiteConnection m_dbConnection; m_dbConnection = new SQLiteConnection("Data Source=GuardDB.sqlite;Version=3;"); m_dbConnection.Open(); // Create our command SQLiteCommand command = new SQLiteCommand(commandString, m_dbConnection); // Start the reader SQLiteDataReader reader = command.ExecuteReader(); // While the reader has data to read, append the text to the 'aReturn' string while (reader.Read()) { // Declare holder string string holder = ""; // Grab the value and feed it to GuardTime which will convert it to a string that's human readable // and set our holder string to this. holder = guard.EpochToDateString(reader.GetInt64(0)); // Remove the GMT info from the output (it's not really necessary for the user to see and takes up space). holder = guard.RemoveGMT(holder); // Append the values for every date we encounter. aReturn += holder + "\r\n"; } // Close the connection to the DB m_dbConnection.Close(); }
// ADD NOW BUTTON // // When a user clicks the 'Add Now' button, the current date and // time are entered into the Dates table as a unix timestamp. private void addNowButton_Click(object sender, EventArgs e) { // Delcare some variables we need Int64 unixTimeThen = 0; Int64 compare = 0; Int64 highest = 0; // When button is clicked Int64 unixTimeNow = DateTimeOffset.Now.ToUnixTimeSeconds(); // Create a new SQLize instance to get the last date SQLize retrieveDate = new SQLize("SELECT * FROM Dates ORDER BY datetime DESC LIMIT 1"); // Store this data briefly with the Read_OneDateInt function of SQLizer unixTimeThen = retrieveDate.Read_OneDateInt(); // Check the difference between unixTimeNow and unixTimeThen with GuardTime // But ONLY if the value of unixTimeThen is not 0 which would indicate // that there are no records. We need at least one date entry in the dates // table to perform calculations! if (unixTimeThen != 0) { GuardTime guard = new GuardTime(); compare = guard.CalcUnixTimeSpan(unixTimeThen, unixTimeNow); // Create one more SQLize instance to grab the last time span from the achievements table SQLize retrieveBest = new SQLize("SELECT * FROM Achievements ORDER BY best_time DESC LIMIT 1"); highest = retrieveBest.Read_OneDateInt(); // If the the highest time is 0, then there are no records which means we should add one! if (highest == 0) { // Create a new SQLize instance SQLize add = new SQLize("INSERT INTO Achievements (best_time) VALUES(" + compare + ")"); // Run that command add.Run_Command(); } // If the highest time is not 0, then determine if we should insert a new best time. if (compare > highest && highest != 0) { // Create a new SQLize instance SQLize add = new SQLize("INSERT INTO Achievements (best_time) VALUES(" + compare + ")"); // Run that command add.Run_Command(); } } // Issue an Insert statement to the Dates table. SQLize insert = new SQLize("INSERT INTO Dates (datetime) VALUES (" + unixTimeNow + ")"); // Tell SQLize to run the command. insert.Run_Command(); // Call Refresh Data RefreshData(); // Refresh our timers RefreshGoalDate(); RefreshCountdown(); RefreshElapsed(); }
// ADD BUTTON // // When a user clicks this button a new record containing a custom date and time // is inserted into the database and the database is refreshed. private void addButton_Click(object sender, EventArgs e) { // Declare some variables we need Int64 unixTimeThen = 0; Int64 compare = 0; Int64 highest = 0; // Begin by creating a new SQLize instance to get the last date SQLize retrieveDate = new SQLize("SELECT * FROM Dates ORDER BY datetime DESC LIMIT 1"); // Store this data briefly with the Read_OneDateInt function of SQLizer unixTimeThen = retrieveDate.Read_OneDateInt(); // Grab our values from the text box // Day string day = dayComboBox.Text; // Month string month = monthComboBox.Text; // Year string year = yearPicker.Text; // Time string time = timePicker.Text; // Parse these values into Guards date format. string dateStringRaw = day + " " + month + " " + year + " " + time; DateTime inputDate = DateTime.Parse(dateStringRaw); // Get ourselves a datetime offset. DateTimeOffset dto = new DateTimeOffset(inputDate); // Turn this into a unix timestamp! Int64 unixTimeNow = dto.ToUnixTimeSeconds(); // Check the difference between unixTimeNow and unixTimeThen with GuardTime // But ONLY if the value of unixTimeThen is not 0 which would indicate // that there are no records. We need at least one date entry in the dates // table to perform calculations! if (unixTimeThen != 0) { // Check the difference between unixTimeNow and unixTimeThen with GuardTime GuardTime guard = new GuardTime(); compare = guard.CalcUnixTimeSpan(unixTimeThen, unixTimeNow); // Create one more SQLize instance to grab the last time span from the achievements table SQLize retrieveBest = new SQLize("SELECT * FROM Achievements ORDER BY best_time DESC LIMIT 1"); highest = retrieveBest.Read_OneDateInt(); // If the highest time is 0, then there are no records whcih means we should add one! if (highest == 0) { // Create a new SQLize instance SQLize add = new SQLize("INSERT INTO Achievements (best_time) VALUES(" + compare + ")"); // Run that command add.Run_Command(); } // Determine if we should insert a new best time // If the highest time is not 0, then determine if we should insert a new best time! if (compare > highest && highest != 0) { // Create a new SQLize instance SQLize add = new SQLize("INSERT INTO Achievements (best_time) VALUES(" + compare + ")"); // Run that command add.Run_Command(); } } // Create our date //string dateStringNew = "\'" + unixTimeNow.ToString() + "\'"; // Issue an insert statement to the Dates table SQLize insert = new SQLize("INSERT INTO Dates (datetime) VALUES (" + unixTimeNow + ")"); // Tell SQLize to run the command. insert.Run_Command(); }