Android Question Need help with unusual countdown timer please (SOLVED)

Discussion in 'Android Questions' started by Reproretro, Jul 17, 2015.

  1. Reproretro

    Reproretro Member Licensed User

    I have been playing around with DateUtils, but can't quite work out how to do this one.

    The local trains leave every 15 minutes during the day.
    E.g. the train to one town (Fremantle) leaves on the hour, then 15, 30 and 45 minutes past.
    The train to another town leaves at 3 mins past, then 18, 33, 48
    and so one.

    So taking Fremantle as an example, at any time I want to simply display the number of minutes and seconds to the next train departure.
    Any advice gratefully accepted.
     
  2. Roycefer

    Roycefer Well-Known Member Licensed User

    Code:
    Sub secondsLeftUntilQuarterHour(when As Long) As Long
        
    Return (15*60*1000-(when mod (15*60*1000)))/1000
    End Sub
    This sub will give you the number of seconds left until the next quarter hour interval. From there, you can parse that into minutes and seconds using simple arithmetic.
     
    Reproretro and DonManfred like this.
  3. Reproretro

    Reproretro Member Licensed User

    Thanks Roycefer
    I'll give it a go
     
  4. Reproretro

    Reproretro Member Licensed User

    It worked perfectly, thank you.
    Here is the resultant code with two trains. I just have to pass the time (in minutes) of the first train in the hour.

    Code:
    Sub Process_Globals
    Dim Mins As Int
    Dim secs As Long
    Dim Rsecs As String
    End Sub

    Sub Globals
       
    Private lblClockFaceL As Label
       
    Private lblClockFaceP As Label
       
    Dim NowDay As Long     
       
    Dim CurrentTime As Long
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       
    Activity.LoadLayout("Main")

       NowDay = 
    DateTime.DateParse(DateTime.Date(DateTime.Now))
       CurrentTime = 
    DateTime.Now - NowDay
    '
       lblClockFaceP.Text = TimeLeft(CurrentTime,0'train from Perth to City West - first train leaves on the hour
       lblClockFaceL.Text = TimeLeft(CurrentTime,6'train from City West to Perth - first train leaves at 6 minutes past the hour
      
    End Sub
    Sub TimeLeft(when As Long, FirstTrain As Int) As String
       
    If FirstTrain > 0 Then FirstTrain = FirstTrain * 60000
       secs = (
    15*60*1000-((when-FirstTrain) mod (15*60*1000)))/1000
       Mins = secs/
    60 : Rsecs = "0" & (secs - Mins*60)
       
    Return Mins & ":" & Rsecs.SubString(Rsecs.Length - 2)
    End Sub

    Sub Activity_Resume
     
Loading...