Shop OBEX P1 Docs P2 Docs Learn Events
Calculate from GPS date if BST or GMT timezone? need day of week — Parallax Forums

Calculate from GPS date if BST or GMT timezone? need day of week

Dwayne DibbleyDwayne Dibbley Posts: 63
edited 2012-07-10 11:07 in Propeller 1
is it possible from a GPS date, to calculate if the current timezone is BST ot GMT?

as BST and GMT change at the following times "the period beginning at one o'clock, Greenwich mean time, in the morning of the last Sunday in March and ending at one o'clock, Greenwich mean time, in the morning of the last Sunday in October."

in vbscript it is possible like below, but i would need to calculate the last Sunday's somehow in spin from only the ddmmyyyy supplied from the GPS
Public Function BSTstarts(ByVal TheYear As Integer) As String
     BSTstarts = "March " _
     & 31 - (Weekday("#March 31, " & TheYear & "#", 2) Mod 7) _
     & ", " & TheYear & " 01:00:00" 
End Function

Public Function GMTstarts(ByVal TheYear As Integer) As String
     GMTstarts = "October " _
     & 31 - (Weekday("#October 31, " & TheYear & "#", 2) Mod 7) _
     & ", " & TheYear & " 01:00:00" 
End Function

Comments

  • Heater.Heater. Posts: 21,230
    edited 2012-07-08 02:26
    How about Tomohiko Sakamoto's algorithm which you can find in wikipedia http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week

    and looks like this in C:
    int dow(int y, int m, int d)
    {
        static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
        y -= m < 3;
        return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
    }
    

    Easy enough to rewrite in Spin
  • Heater.Heater. Posts: 21,230
    edited 2012-07-08 02:50
    Something like this:
    ' Day of week.
    ' Algorithm by Tomohiko Sakamoto, accurate for any Gregorian date.
    ' Returns Sunday as 0, Monday as 1 etc
    PUB dayOfWeek(y, m, d)
        y -= ((m < 3) & 1)
        return (y + y/4 - y/100 + y/400 + dowTable[m-1] + d) // 7
    
    DAT
    dowTable   long 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4
    

    Spin version untested as I am Propless at the moment.
  • Dwayne DibbleyDwayne Dibbley Posts: 63
    edited 2012-07-08 04:05
    Thanks Heater, that works fine for identifying the day of the week.

    Would the best way to find the last Sunday in the month, just feed the PUB with dayOfWeek(2012,03,31) and if return value not 0 ( Sunday ) then subtract 1 from day and try again. keep extracting 1 until return value = 0 ? or is that a long way round to do it?
  • Heater.Heater. Posts: 21,230
    edited 2012-07-08 04:25
    Hmmm...A simple subtraction should do it.

    The last day of March 2012 is dayOfWeek(2012, 3, 31) which returns 6 for Saturday.
    Subtract that 6 from the day of the last day of month, 31 - 6, gives us 25 which happens to be the required date of the last Sunday in March.

    We can check, dayOfWeek(2012, 3, 25) returns 0 for Sunday.

    So:

    lastSundayMarch := 31 - dayOfWeek(2012, 3, 31)
    lastSundayOctober := 31 - dayOfWeek(2012, 10, 31)
  • Dwayne DibbleyDwayne Dibbley Posts: 63
    edited 2012-07-08 05:02
    Excellent, i seem to always think of the hardest way to do things :)

    Many Thanks
  • Dwayne DibbleyDwayne Dibbley Posts: 63
    edited 2012-07-09 04:38
    one more question?. What would be the best way to identify if the current date falls between the last Sunday in march and the last Sunday in October? would converting the dates to Julian format then just doing a greater than or less than? or is there a more simpler approach?

    Thanks
  • Heater.Heater. Posts: 21,230
    edited 2012-07-09 15:50
    Something like this:
    ' Return TRUE if BST
    pub isBST (month, dayOfMonth)
        if (month == 3) and (dayOfMonth >= lastSundayMarch)        'In the tail end of March ?
            return true
        if (month == 10) and (dayOfMonth < lastSundayOctober)      'In the beginning of October?
            return true 
        if (month > 3) and (month < 10)                            'Between March and October?
            return true
        return false                                               'No, must be GMT
    

    I'm tired so you had better check those comparisons. It could be simplified further but these tests should make it clear.
  • Dwayne DibbleyDwayne Dibbley Posts: 63
    edited 2012-07-10 10:54
    Thanks just needed to change the operator in
    if (month == 3) and (dayOfMonth >= lastSundayMarch)        'In the tail end of March ?
    

    to
    if (month == 3) and (dayOfMonth => lastSundayMarch)        'In the tail end of March ?
    
  • Heater.Heater. Posts: 21,230
    edited 2012-07-10 11:07
    Oops, sorry.
Sign In or Register to comment.