Share My Creation Julian Date/Time Calculator Based On DateTim.c By David Cass Tyler

Hi

I use Julian Date Time UTC as doubles in my real-time systems. Of course its pretty hard to work out the date and time by looking at the number:

2459008.73432 = 2020/06/08 05:37:25

Calculated using:
On-Line Converter - Julian Date

I found an algorithm written by D. C. Tyler in C which may be found among other web sites on:

DateTime.c

The beauty of this algorithm is that it includes time. It is based on an algorithm developed by Robert Tanzin which can be found on:

Algorithm 199: conversions between calendar date and Julian day number

I originally used the algorithm with just a few minor mods in a embedded C program and later converted it into Visual Basic for use in MS Access.

Its tricky because the data types in basic are different from c and so truncation and rounding must be carried out to get the correct result.

I have also converted it for use in B4X and have attached the module for others to use. I cannot guarantee it, but it hasn't failed so far over the small time I have used it.

If you wish to use it over a wide range of dates i.e centuries then I'd give it a good test first. It's currently programmed as a single instance code module.

Perhaps someone might like to convert it to a class and post this.

It's probably not very efficiently coded for B4X as I am not good at Object Oriented programming, but it does work.

Best regards

Rob
 

Attachments

  • dateTimeConversions.bas
    5.4 KB · Views: 418

rgarnett1955

Active Member
Licensed User
Longtime User
Mucho embarrassment,

Converting from calender date time to Julian date time didn't work

I had tested it in VBA, but sadly not in B4X.

Two problems the constant 146097l should have been 146097

The last digit wasn't a digit it was a lower case L which looks like a 1 in Keil because of the font they use.

In addition VBA uses Int(var) to pull the integer part out of a number whereas in B4X you use Floor(var)

Bad MDY2GD That's Month Day Year to Pope Gregory the 13th's Calendar:
Public Sub MDY2GD (nMonth As Int, nDay As Int, nYear As Int ) As Long

       Dim nYears As Long

       Dim nDate  As Long


       nYears = nYear - nBias(nMonth)

 

       nDate     = Floor(( Floor(nYears / 100) * 146097l ) / 4) _
'                                                      ^ Naughty digit (Lower case L interpreted by me as 1)
               + Floor((( nYears Mod 100 ) * 1461  / 4)) _

            - nDays(nMonth) _

            + nDay

       Return nDate

End Sub  ' long MDY2GD ( int nMonth, int nDay, int nYear )

The correct constant is 146097 and order of magnitude smaller than the incorrect one I entered.

I have attached the revised code to this post. Maybe Erel or someone could dispatch the old version to the round file as we don't want any poor soles using it.

Best regards

Rob
 

Attachments

  • dateTimeConversions.bas
    5.4 KB · Views: 339

WimS

Member
Licensed User
This function works correct over whole the year!

Convert Julian serial date to calendar date string:
Sub JulianSerialToDate(JulianSerial As Double) As String
    
    
    Try
        
        Dim Z As Int = JulianSerial+0.5
        Dim F As Double = JulianSerial+0.5-Z


        Dim A As Double
        Dim alpha As Double
        If (Z < 2299161) Then A = Z
        If (Z >= 2299161) Then
            alpha = Floor((Z-1867216.25)/36524.25)
            A = Z + 1 + alpha - Floor(alpha/4)
        End If

        
        Dim B As Double = A + 1524
        Dim C As Double = Floor((B-122.1)/365.25)
        Dim D As Double = Floor( 365.25*C )
        Dim E As Double = Floor((B-D)/30.6001)


        Dim day As Int = Floor(B - D - Floor(30.6001*E))
        Dim month As Int
        Dim year As Int
        
        If (E < 13.5) Then month = E - 1
        If (E > 13.5) Then month = E - 13
        If (month > 2.5) Then year = C - 4716
        If month < 2.5 Then year = C - 4715
        
        
        Dim Str As String = day & "-" & month & "-" & year
        DateTime.DateFormat = "dd-MM-yyyy"
        Dim l As Long = DateTime.DateParse(Str)
        DateTime.DateFormat = "dd-MM-yyyy"
        Str = DateTime.Date(l)
        Return Str
    
    Catch
        Log(LastException)
    End Try

End Sub
 
Top