[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Help!! DateTimePicker

Posted on 2004-10-28
2
Medium Priority
?
1,848 Views
Last Modified: 2012-08-13
HI experts,

I have tried this for 3 hours and couldn't get it to work. :((
Scenerio:


1)I have a comboBox which I could choose the time interval in an hour of 15, 20, 30 or 60 Mins.

2)I have a dateTimePicker(shows only time) for schedule items.
The increment or decrement of the DateTimePicker minute is according to the interval chosen.
if interval of 20 is choosen, when I click up sign, 10:20 PM becomes 10:40PM, down button 10:20PM becomes 10:00PM.

3)  Schedule items (FYI) are scheduled according to the interval chosen. if interval of 30 is chosen, the schedule can only be the time interval 30. Eg 10.30pm, 11.30pm, 3.30AM.
But some schedule items are schedule before the interval change. Therefore my schedule item time would not have a interval time of the chosen interval. EG. 10.30PM but the interval chosen is 20 minutes.

What I want to do is when I click the up or down button of the date time picker it will go to the nearest time of the current chosen interval. For example.

--------------------------------------------------------
Example 1)
Interval Chosen := 30;
Original Time := 10.20PM; <- the time in datetimepicker

When I click up                  when i click down
once > 10:30 pm               once > 10:00 PM
twice > 11.00pm                Twice > 9:30 PM

and so on
--------------------------------------------------
Example 2)
Interval Chosen := 20 minutes;
Original Time := 10.45PM;  <- the time in datetimepicker

When I click up                  when i click down
once > 11:00 pm               once > 10:40 PM
twice > 11.20 pm               Twice > 10:20 PM

and so on

--------------------------------------------------
Example 2)
Interval Chosen := 20 minutes;
Original Time := 10.15PM;  <- the time in datetimepicker

When I click up                  when i click down
once > 10:20 pm               once > 10:00 PM
twice > 10.40 pm               Twice > 9:40 PM

and so on

-------------------------------------------------------------------
I'm a student just started learning delphi this year, so please bear with me :)
Below are some of my codes, which are not very good. Just ignore my codes and see if there are any good idea.
The alogrithms of my codes is wrong. It works for some scenerio but not all. And some parts of the codes are not in there.

Thank you very much in advance. I really need it to work ASAP. Thanks.





//************************************************************
procedure TDefaultPlannerItemEditForm.DateTimePickerChange(
  Sender: TObject);
var
  TempMin, TempHour, DTHour, DTMin, Ohour, Omin, tempsec, tempmsec : Word;
  tInterval, tempTime, ainterval : TTime;
  Minus, Unequal : Boolean;
  a, b, c, CurrentInterval : integer;
begin
  CurrentInterval := FormSchedule.SLInterval;
  TempMin :=  CurrentInterval mod MintuesPerHour;
  TempHour := CurrentInterval div MintuesPerHour;

  tInterval := EncodeTime(TempHour, TempMin, 0, 0);

 if sender = StartTimeDateTimePicker then
    tempTime := OriginalStartTime
  else
    tempTime := OriginalEndTime;

  DecodeTime((sender as TDateTimePicker).Time, DTHour, DTMin, tempsec, tempmsec);
  DecodeTime(tempTime, Ohour, Omin, tempsec, tempmsec);

  if (Omin <> 0) and
    (Max(CurrentInterval, Omin) mod
       Min(CurrentInterval, Omin) <> 0 ) then
  begin
    a := Omin - CurrentInterval;
    b := CurrentInterval - Omin;
    c := CurrentInterval - a;
  end;

  //let temptime and datetimepicker date be the same before comparing the time
  (sender as TDateTimePicker).Date := tempTime;

  Minus := tempTime > (sender as TDateTimePicker).time;
  if DTHour  = Ohour then
  begin
    Unequal := False;
    if (Omin <> 0) and (Max(CurrentInterval, Omin) mod
         Min(CurrentInterval, Omin) <> 0 ) then
    begin
      a := Omin - CurrentInterval;
      b := CurrentInterval - Omin;
      c := Max(CurrentInterval, a) - Min(CurrentInterval, a);
      Unequal := True;
    end;



    if Minus then
    begin
      if Unequal then
      begin
        if CurrentInterval > Omin then
          (sender as TDateTimePicker).Time := tempTime - EncodeTime(0,b*2,0,0)
        else if CurrentInterval < Omin then
          (sender as TDateTimePicker).Time := tempTime - EncodeTime(0,a,0,0);
      end
      else
        (sender as TDateTimePicker).Time := tempTime - tInterval;
    end
    else
    begin
      if Unequal then
      begin
        if CurrentInterval > Omin then
          (sender as TDateTimePicker).Time := tempTime + EncodeTime(0,b,0,0)
        else if CurrentInterval < Omin then
          (sender as TDateTimePicker).Time := tempTime + EncodeTime(0,c,0,0);
      end
      else
        (sender as TDateTimePicker).Time :=
          tempTime + ifthen (DTMin <> 59, tInterval, -tInterval );
    end;
  end;

  tempTime :=  (sender as TDateTimePicker).Time;

  if sender = StartTimeDateTimePicker then
    OriginalStartTime := tempTime
  else
    OriginalEndTime := tempTime;
0
Comment
Question by:AgeOfWars
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
LRHGuy earned 2000 total points
ID: 12440194
I know this sounds like your homework, but it's interesting to me, so I make a few comments.

Here's one way to do it. Once you have the time broken down into HOUR and MINUTE, call this routine to do the adjustment. It can only adjust for an interval of 0 to 60 minutes.

Your specifications seem confusing. If the interval is 30, do you want 10:30, 11:00, 11:30?  or only 10:30, 11:30?

This routine performs "multiples" if the interval is less than 31, and forces the interval past the hour otherwise.
If you don't want the multiples, you can take it out, or add a parameter that specifies when to do it.


procedure AdjustToInterval(aUp:boolean; aIntMins:integer; var aHour,aMin:integer);
var
  Diff:integer;
begin
  { Adjust hour/min to the next interval }
  { Intervals of 0 minutes or less and 61 or more are treated as 60 minutes }
  { Intervals 30 minutes or less are forced to a multiple of aIntMins }
  { Intervals 31 or more, are forced to aIntMins past the hour }

  if (aIntMins<=0) or (60<aIntMins) then
    aIntMins:=60;
  if aIntMins>30 then begin
    aIntMins:=aIntMins mod 60;
    if aMin>=aIntMins then begin
      {Force to next/prev hour}
      aMin:=aIntMins;
      if aUp then
        inc(aHour)
      else
        dec(aHour);
    end
    else begin
      {Force to current/prev hour}
      aMin:=aIntMins;
      if not aUp then
        dec(aHour);
    end;
  end
  else begin
    {Interval less than 31 minutes}
    { How far from interval are the minutes? }
    Diff:=aMin mod aIntMins;
    if Diff=0 then begin
      Diff:=aIntMins; {we go a complete interval}
    end
    else begin
      { Going to "next" interval }
      if aUp then
        Diff:=aIntMins-Diff; {subtract for up}
    end;

    if aUp then begin
      aMin:=aMin+Diff;
    end
    else {down} begin
      aMin:=aMin-Diff;
    end;
  end;

  { Sanity checks }
  while aMin>=60 do begin
    dec(aMin,60);
    inc(aHour);
    if aHour>=24 then
      aHour:=aHour-24;
  end;
  while aMin<0 do begin
    inc(aMin,60);
    dec(aHour);
    if aHour<0 then
      aHour:=aHour+24;
  end;
end;
0
 

Author Comment

by:AgeOfWars
ID: 12448575
Hey LRHGuy,

It works! You are a genius to think of the algorithm.

I just added a line to dec the Hour when (aUp is False) and dec interval is changes the hour.

Thanks a lot !!!!
Regards,
Ian


------------------------------Con't from
    if aUp then begin
      aMin:=aMin+Diff;
    end
    else {down} begin
      if aMin = 59 then                 <----------------------
        dec(ahour);
      aMin:=aMin-Diff;
    end;
  end;

  { Sanity checks }
  while aMin>=60 do begin
    dec(aMin,60);
    inc(aHour);
    if aHour>=24 then
      aHour:=aHour-24;

-------------------------------------------
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question