Android Question day and night duration

harinder

Active Member
Licensed User
Longtime User
Hello All,

I have been stuck with this for a while now and don't seem to be arriving at a logical solution.
I have a 7 hour work to do starting anytime from 0000h to 2400 h. Within the span of this work,I need to find out night time(if any) and day time(if any) depending on sunrise and sunset times for the calendar day.For eg if sunrise is 0500 and I start work at 0200,then night time will be 3h and day time 4h OR if I start work at 1500 and sunset is 1730 then day time is 2h30m and 4h30m night time OR if I start work at 2330 the work progresses to next calendar day and if sunrise is 0500 then night time will be 05h30m and day time 01h30m.
I can find out sunrise sunset time for 1st and 2nd calendar days using the astro lib of b4a, but I am not able to arrive at a logical code for calculating the day and night components.
Any help will be greatly appreciated..Thank you
 

harinder

Active Member
Licensed User
Longtime User
I have tried my hand at it and have progressed this far:

dtlbl1 and dtlbl2 are dates for starting and ending of task. Lat Long of departure and destination places is picked up from database.
It is presently giving satisfactory day and night durations. Any inputs from the Masters for improvement is highly appreciated..


B4X:
 Dim d1,d2,m1,m2,y1,y2 As Int
    Dim daymthyr1() As String =Regex.Split("-",Dtlbl1.text )
    d1=daymthyr1(0)
    m1=daymthyr1(1)
    y1=daymthyr1(2)
   
    Dim daymthyr2() As String =Regex.Split("-",Dtlbl2.text )
    d2=daymthyr2(0)
    m2=daymthyr1(1)
    y2=daymthyr2(2)
   
  Dim ast1,ast2 As Astro
    ast1.Zenith = ast1.CIVIL '96 deg
    ast2.Zenith = ast2.CIVIL
   
    Dim riseset1(),riseset2() As String   'riseset(3)
    If spndep.SelectedIndex <> spndep.IndexOf ("Select DEP") Then
    Dim MyQuery As String
    MyQuery="SELECT LAT ||','|| LONG FROM " & Main.DBTableplaces &" WHERE DEST='" & spndep.selecteditem &"'"  'fetch place,lat,long from db using regex
    Dim latlong As String=Main.sql1.ExecQuerySingleResult(MyQuery)
    Dim LL() As String =Regex.Split(",",latlong )
        riseset1=ast1.sun(d1,m1,y1,LL(0),LL(1),0)
    Else
        Msgbox("PLEASE INPUT DEP", "A T T E N T I O N")
        Return
    End If
   
    If spndest.SelectedIndex <> spndest.IndexOf ("Select DEST") Then
    Dim MyQuery1 As String
    MyQuery1="SELECT LAT ||','|| LONG FROM " & Main.DBTableplaces &" WHERE DEST='" & spndest.selecteditem &"'"  'fetch place,lat,long from db using regex
    Dim latlong1 As String=Main.sql1.ExecQuerySingleResult(MyQuery1)
    Dim LL1() As String =Regex.Split(",",latlong1 )
       
        riseset2=ast2.Sun(d2,m2,y2,LL1(0),LL1(1),0)
    Else
        Msgbox("PLEASE INPUT DEST", "A T T E N T I O N")
        Return
    End If
   
    Dim sunriseticks1,sunsetticks1, sunriseticks2,sunsetticks2 As Long
   
    sunriseticks1  =  DateTime.datetimeparse (Dtlbl1.text,riseset1(0))
    sunsetticks1  = DateTime.datetimeparse (Dtlbl1.text,riseset1(1))
   
    If sunriseticks1 > sunsetticks1 Then
        sunriseticks1 =  DateTime.Add(sunriseticks1,0,0,-1)
    End If
   
    sunriseticks2  = DateTime.datetimeparse (Dtlbl2.text,riseset2(0))
    sunsetticks2 = DateTime.datetimeparse (Dtlbl2.text,riseset2(1))
    If sunriseticks2 > sunsetticks2 Then
        sunriseticks2 =  DateTime.Add(sunriseticks2,0,0,-1)
    End If
    METday.Text="00:00"
    METnite.Text="00:00"
   
    chksoffticks = DateTime.datetimeparse(Dtlbl1.Text,lblchksoff.Text)
    chksonticks =  DateTime.datetimeparse(Dtlbl2.Text,lblchkson.Text)
   
    durationticks=chksonticks-chksoffticks
    durationstring  = TicksToTimeString(durationticks)
   
    Dim zerohr As Long=DateTime.datetimeparse(Dtlbl1.Text,"00:01")
    Dim lasthr As Long =DateTime.datetimeparse(Dtlbl1.Text,"23:59")
    Dim zerohr1 As Long=DateTime.datetimeparse(Dtlbl2.Text,"00:01")
    Dim lasthr1 As Long =DateTime.datetimeparse(Dtlbl2.Text,"23:59")
   
    If chksoffticks>=zerohr And chksoffticks<=sunriseticks1 And chksonticks>=zerohr1 _
     And chksonticks<=sunriseticks2 Then
        Log(3)
        METnite.Text=durationstring
    End If
    If chksoffticks>=zerohr And chksoffticks<=sunriseticks1 And chksonticks>sunriseticks2 _
     And chksonticks<=sunsetticks2 Then
        Log(4)
        Dim t As String=TicksToTimeString(sunriseticks1-chksoffticks)
        METnite.Text=t
        METday.text =TicksToTimeString(durationticks - (sunriseticks1-chksoffticks))
    End If
    
If chksoffticks>sunriseticks1 And chksoffticks<=sunsetticks1 And chksonticks>=sunriseticks2 And chksonticks<=sunsetticks2  Then
        Log(5)
        METday.Text =  durationstring
    End If
    If chksoffticks>sunriseticks1 And chksoffticks<=sunsetticks1 And chksonticks>sunsetticks2 And chksonticks<=lasthr1  Then
        Log(6)
        Dim s As String=TicksToTimeString(sunsetticks1-chksoffticks)
        METday.text =s
        METnite.Text = TicksToTimeString(durationticks - (sunsetticks1-chksoffticks))
       
    End If
    If chksoffticks>sunsetticks1 And chksoffticks<=lasthr And chksonticks>=sunsetticks2 And chksonticks<=lasthr1  Then
        Log(7)
       
        METnite.Text = durationstring
 
Upvote 0
Top