Announcement

Collapse
No announcement yet.

Day Of Week from Julian Date - Mystery "feature"

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Day Of Week from Julian Date - Mystery "feature"

    OK Now that I understand MOD I still do not understand why there is a bug in this code.

    This App produces an ascii file of julian dates and days of the week from 97 - 04 that is perfect except for one day - Feb 24 2000!

    I cant figure it out.

    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    'Description : Day Of week From Julian Date Bug
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    #COMPILE EXE "DateTest.exe"

    GLOBAL Jstart AS LONG, Date AS LONG, DayStr AS STRING, CalDateStr AS STRING
    GLOBAL Day AS LONG, Jul AS LONG, Julerror AS LONG, CalDate AS LONG, hFile2 AS LONG
    GLOBAL DestFile AS STRING, i AS LONG
    '************************************************************************
    ' Converts TradeStation Date (Y)YYMMDD (LONG) to Julian Date (LONG)
    '************************************************************************
    FUNCTION DateToJulian( BYVAL TSDate AS LONG ) AS LONG

    LOCAL Year AS DWORD, Day AS DWORD, Month AS DWORD, Gregorian AS DWORD
    LOCAL JulYear AS LONG, JulMonth AS LONG, Adjust AS LONG, JulDate AS LONG
    Gregorian = 588829
    Year = 1900 + FIX(TSDate/10000)
    Month = FIX((TSDate - FIX(TSDate/10000)*10000)/100)
    Day = TSDate - FIX(TSDate/100)*100

    IF ( Month > 2 ) THEN
    JulYear = Year
    JulMonth = Month + 1
    ELSE
    JulYear = Year - 1
    JulMonth = Month + 13
    END IF

    JulDate = ( INT( 365.25 * JulYear ) + INT( 30.6001 * JulMonth ) + Day + 1720995 )
    IF ( Day + ( 31 * ( Month + ( 12 * Year ) ) ) ) >= Gregorian THEN
    Adjust = INT( 0.01 * JulYear )
    JulDate = ( JulDate + 2 - Adjust + INT( 0.25 * Adjust ) )
    END IF
    DateToJulian = JulDate
    END FUNCTION
    '************************************************************************
    ' Converts Julian Date (LONG) to TradeStation Date (Y)YYMMDD (LONG)
    '************************************************************************
    FUNCTION JulianToDate( BYVAL JulDate AS LONG ) AS LONG

    LOCAL Year AS DWORD, Day AS DWORD, Month AS DWORD, Gregorian AS DWORD
    LOCAL JulA AS LONG, Jul_Alpha AS LONG, JulB AS LONG, JulC AS LONG', TSDate AS LONG
    LOCAL JulD AS LONG, JulE AS LONG, CalDate AS DWORD
    Gregorian = 2299161
    IF ( JulDate >= Gregorian ) THEN
    Jul_Alpha = INT( ( ( JulDate - 1867216 ) - 0.25 ) / 36524.25 )
    JulA = ( JulDate + 1 + Jul_Alpha - INT( 0.25 * Jul_Alpha ) )
    ELSE
    JulA = JulDate
    END IF

    JulB = JulA + 1524
    JulC = INT( 6680! + ( ( JulB - 2439870 ) - 122.1 ) / 365.25 )
    JulD = INT( 0.25 * JulC ) + ( 365 * JulC )
    JulE = INT( ( JulB - JulD ) / 30.6001 )

    Day = JulB - JulD - INT( 30.6001 * JulE )
    Month = JulE - 1
    IF ( Month > 12 ) THEN Month = Month - 12
    Year = JulC - 4715
    IF ( Month > 2 ) THEN Year = Year - 1

    JulianToDate = CLNG( (Year-1900)*10000 + Month*100 + Day )
    END FUNCTION
    '************************************************************************
    ' JulianToDate - Converts TradeStation Date (LONG) to Day of Week (LONG)
    '************************************************************************
    FUNCTION DayOfWeek( BYVAL TDate AS LONG ) AS LONG
    LOCAL TSDateJul AS LONG
    TSDateJul = DateToJulian(TDate)
    DayOfWeek = TSDateJul MOD 7
    END FUNCTION
    '************************************************************************
    ' DateToJulian - Converts TSdate (Y)YYMMDD to Calendar Date (LONG)
    '************************************************************************
    FUNCTION TStoCalDate( BYVAL TSDate AS LONG ) AS LONG
    TStoCalDate = 1900 + (TSDate/10000 - INT(TSDate/10000))*100000000 + INT(TSDate/10000)
    END FUNCTION
    '************************************************************************
    FUNCTION PBMAIN
    DestFile = "Dates.txt
    hFile2 = FREEFILE
    OPEN DestFile FOR OUTPUT AS hFile2 ' Create/Overwrite output file

    Jstart = DateToJulian(970101)
    FOR i = Jstart TO Jstart + 2900
    Date = JulianToDate(i)
    CalDate = TStoCalDate(Date)
    CalDateStr = TRIM$(STR$(CalDate))
    IF LEN(CalDateStr) < 8 THEN CalDateStr = "0"+CalDateStr
    CalDateStr = LEFT$(CalDateStr,2)+"/"+LEFT$(RIGHT$(CalDateStr,6),2)+"/"+RIGHT$(CalDateStr,4)
    Day = DayOfWeek(i)
    Jul = DateToJulian(Date)

    SELECT CASE Day
    CASE 0 : DayStr = "Sunday -----"
    CASE 1 : DayStr = "Mon"
    CASE 2 : DayStr = "Tues"
    CASE 3 : DayStr = "Wed"
    CASE 4 : DayStr = "Thur"
    CASE 5 : DayStr = "Fri"
    CASE 6 : DayStr = "Sat"
    END SELECT
    JulError = i - Jul
    IF JulError > 0 THEN MSGBOX("ERROR !!!!")
    PRINT# hFile2, "Julian "+STR$(i)+" Date "+STR$(Date)+" CalDate "+_
    CalDateStr+" Day "+DayStr ' Write a line

    NEXT
    CLOSE #hFile2 ' Close Output file
    MSGBOX("Finished")
    END FUNCTION
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤'



    ------------------
    Kind Regards
    Mike

  • #2
    Perhaps this will help:

    Code:
    Function DayOfTheWeek() Export As String
         '// get date format in "dddd, MMMM dd, yyyy"
         '// e.g. Monday, June 26, 2000
         Local uTime As SYSTEMTIME
         Local lNum As Long
         Local saDay As Asciiz * 64
         Local saMonth As Asciiz * 64
    
         GetLocalTime uTime
    
         If uTime.wDayOfWeek = 0 Then
             ' this is Sunday, work out separately
             lNum = &H30
         Else
             lNum = &H2A + (uTime.wDayOfWeek) -1
         End If
    
         GetLocaleInfo %LOCALE_USER_DEFAULT, lNum, saDay, SizeOf(saDay)
         lNum = &H38 + (uTime.wMonth - 1)
         GetLocaleInfo %LOCALE_USER_DEFAULT, lNum, saMonth, SizeOf(saMonth)
         Function = Trim$(saDay & ", " & saMonth & " " & Trim$(Str$(uTime.wDay)) & ", " & Trim$(Str$(uTime.wYear)))
    
    End Function
    ------------------
    Scott
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

    Comment


    • #3
      You might try converting :

      TradeStation date -> Systemtime value -> Filetime value

      <perform math on Filetime value>

      Filetime value -> Systemtime value -> TradeStation date

      Code:
      #INCLUDE "WIN32API.INC"
      
      
      %OneDay      =   864000000000&&
      
      
      UNION MathTime
         Q AS QUAD
         F AS FILETIME
      END UNION
      
      
      DIM ST AS SYSTEMTIME
      DIM MT AS MathTime
      DIM DaysOfWeek$(6)
      
      DaysOfWeek$(0) = "Sun" : DaysOfWeek$(1) = "Mon" 'etc....
      
      ST.wYear = Year
      ST.wMonth = Month
      ST.wDay = Day
      
      
      SystemTimeToFileTime ST, MT.F
      
      
      MT.Q = MT.Q + 30 * %OneDay
      
      
      FileTimeToSystemTime MT.F, ST
      
      Day$ = DaysOfWeek$( ST.wDayOfWeek)
      
      Year = ST.wYear
      Month = ST.wMonth
      Day = ST.wDay
      ------------------
      Bernard Ertl

      [This message has been edited by Bern Ertl (edited March 11, 2001).]
      Bernard Ertl
      InterPlan Systems

      Comment


      • #4
        Mike --

        > This App produces an ascii file of julian
        > dates and days of the week from 97 - 04
        > that is perfect except for one day - Feb 24 2000!

        Sorry, it isn't perfect. It skips days of the week on...

        05/30/1997
        12/16/1997
        03/26/1998
        10/12/1998

        ...and so on.

        Your Julian Date functions appear to be working correctly. All you have to do is take the Julian Date MOD 7 to get the day of the week, offset by 1. In other words...

        Code:
        (Julian+1) MOD 7 = Day of Week from 0 (Sunday) to 6 (Saturday).
        -- Eric


        ------------------
        Perfect Sync Development Tools
        Perfect Sync Web Site
        Contact Us: mailto:[email protected][email protected]</A>
        "Not my circus, not my monkeys."

        Comment


        • #5
          A complete set of Julian functions can be found at www.basicguru.com/zijlema/ .
          It is for DOS, but it's rather easy to 'translate' it to PB for Windows.

          ------------------
          mailto:[email protected][email protected]</A>
          www.basicguru.com/zijlema/

          [This message has been edited by Egbert Zijlema (edited March 11, 2001).]

          Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
          http://zijlema.basicguru.eu
          *** Opinions expressed here are not necessarily untrue ***

          Comment


          • #6
            Bern, Scott and Egbert, Thx nut I dont want to go thru yet more conversions if I can possible help it. I am so close.

            Eric
            Youre right it skips other dates too. I really dont get this ..

            I tried substituting
            FUNCTION DayOfWeek( BYVAL TDate AS LONG ) AS LONG
            LOCAL TSDateJul AS LONG
            TSDateJul = DateToJulian(TDate)
            DayOfWeek = (TSDateJul+1) MOD 7
            END FUNCTION
            All this does is shift the days of the week relative to the Julian date. The fundamental problem still persists.

            I have checked and rechecked my Julian<->Date conversions. They are clean. As you can see the julian Numbers are in perfect sequence and the dates are correct.

            Yet the day of the week has these wierd hiccups every once in while!?

            ------------------
            Kind Regards
            Mike

            Comment


            • #7
              Mike --

              I think you're mistaken. I changed your program like this:

              Code:
              'Day = DayOfWeek(i)  'remove this
              Jul = DateToJulian(Date)
              Day = (Jul+1) MOD 7  'add this
              ...and it produces the correct day of the week for every date. No hiccups, coughs, sneezes...

              -- Eric


              ------------------
              Perfect Sync Development Tools
              Perfect Sync Web Site
              Contact Us: mailto:[email protected][email protected]</A>



              [This message has been edited by Eric Pearson (edited March 11, 2001).]
              "Not my circus, not my monkeys."

              Comment


              • #8
                Mike,

                In

                Day = DayOfWeek(i)

                i was converted to Julian before the loop

                JStart = DateToJulian(970101)

                the DayOfWeek looks like it is trying to convert i to Julian
                again. Did you mean to pass it Date? Or you need to comment out
                the DateToJulian in the DayOfWeek function and try TDate Mod 7.

                Mike



                ------------------

                Comment


                • #9
                  Mike,

                  Here's a trick I used in PB for Dos. It seems to work fine in
                  windows as well The thing you have to do is save the current
                  date BEFORE starting you loop. Then restore it when you've
                  finished. The only problem you could encounter is if you start
                  your loop before midnight and end it after. In that case you
                  would set the date for yesterday. If that is a concern then
                  check the time before and after the loop and if time < old_time
                  then reset the date then incr by one.

                  SUB Incr_date
                  LOCAL last_date$, temp$, m$, d$, y$, x&
                  last_date$ = DATE$
                  temp$ = DATE$
                  d$ = MID$(temp$, 4, 2)
                  x = VAL(d$)
                  INCR x
                  d$ = FORMAT$(x, "00")
                  MID$(temp$, 4,2) = d$
                  DATE$ = temp$
                  IF DATE$ <> last_date$ THEN EXIT SUB

                  ' new month
                  MID$(temp$, 4, 2) = "01" ' 1st day of month
                  m$ = LEFT$(temp$, 2)
                  x = VAL(m$)
                  INCR x
                  m$ = FORMAT$(x, "00")
                  MID$(temp$, 1) = m$
                  DATE$ = temp$
                  IF DATE$ <> last_date$ THEN EXIT SUB

                  ' new year
                  MID$(temp$, 1, 2) = "01" 'Jan
                  y$ = RIGHT$(temp$, 4)
                  x = VAL(y$)
                  INCR x
                  y$ = FORMAT$(x, "0000")
                  MID$(temp$, 7) = y$
                  DATE$ = temp$
                  END SUB

                  Hope that helps

                  ------------------
                  "There are two novels that can change a bookish fourteen-year old's life: The Lord of the Rings and Atlas Shrugged. One is a childish fantasy that often engenders a lifelong obsession with its unbelievable heroes, leading to an emotionally stunted, socially crippled adulthood, unable to deal with the real world. The other, of course, involves orcs." - John Rogers

                  Comment


                  • #10
                    I like MSDN sample:
                    Code:
                       Function AstroDay(year As Long, month As Long, day As Long) As Long
                          Dim y As Double
                          y = year + (month - 2.85) / 12
                          AstroDay = Int(Int(Int(367 * y) - 1.75 * Int(y) + day) -0.75 * Int(0.01 * y)) + 1721116
                       End Function

                    Sample of usage
                    Code:
                       Function PbMain
                          Select Case (AstroDay(2001, 1, 17) Mod 7) ' Day of week
                             Case 0: Print "Sunday"
                             Case 1: Print "Monday"
                             Case 2: Print "Tuesday"
                             Case 3: Print "Wednesday"
                             Case 4: Print "Thursday"
                             Case 5: Print "Friday"
                             Case 6: Print "Saturday"
                          End Select
                          Print Astroday(2001, 1, 17) - Astroday(2000, 12, 31) ' No. of days
                          WaitKey$
                       End Function
                    ------------------
                    E-MAIL: [email protected]

                    Comment


                    • #11
                      Astroday
                      Post number 10 correction (there is a reason for both dates)
                      I just happened to be comparing some Julian date functions I found in the forums and noticed a difference ... the values 1721116 and 1721119. Which is correct? Outside the forum I've found both used. Function AstroDay(TheDate$) As Long 'MM-DD-YYYY Local y As Double, Year, Month, Day As Long Year =


                      Can also be changed in Roca includes. afxtime.inc and cafxtime.inc
                      Code:
                      Function AstroDay(year As Long, month As Long, day As Long) As Long
                        Dim y As Double
                        y = year + (month - 2.85) / 12
                        AstroDay = Int(Int(Int(367 * y) - 1.75 * Int(y) + day) -0.75 * Int(0.01 * y)) + 1721119  '1721116
                      End Function​
                      The world is full of apathy, but who cares?

                      Comment

                      Working...
                      X