Information > DNN Blog
 
DotNetNuke Blog

Current Articles | | Search |

Correcting the initially Selected Date

In version 4.0.1, the Events module uses Now.Date to set the initially Selected Date. This is unfortunately based on the server date. In order to make this more relevant for users in different timezones to their servers (which is common), I have tried to correct the time (and therefore the associated date). This is not 100% accurate, but will be near enough for most use cases.:

Therefore....

Getting the browser data is nigh on impossible, so I have gone for a second best. The module should now set the current date =

  • Current date (Date.Now)
  • Corrected for server timezone to GMT, corrected to DST by server DST
  • For logged on user
    • Corrected by users timezone from their profile
  • For anonymous user
    • Corrected by module timezone as set in Events Settings

This does not attempt to correct for DST in the module timezone since this information is not available within .Net V2 or DNN 4.x. Therefore this time (and therefore the date portion which is what is used) is now accurate to with +-1 hour (probably). This has no effect on the data being displayed, only the initially selected date.

I have added a function to EventsBase, which is based on code from the Forums module (thanks Chris Paterra). I feed this Date.Now, since Now.Date only gets Date, and we need DateTime.

 Public Function ConvertTimeZone(ByVal dateIn As DateTime) As DateTime
    Dim dateOut As DateTime = dateIn

    Try
        'We need to know the servers timezone, we will use this information to convert the datetime to a GMT time & date
        Dim tz As System.TimeZone = System.TimeZone.CurrentTimeZone
        Dim LocalTimeZone As Double = tz.GetUtcOffset(DateTime.Parse("00:00")).TotalMinutes

        'we also want to take into account Daylight savings time, since this will affect the calculation as well. we will calculate daylight savings time based on the date passede and
        'not based on what the current date and time is.
        If tz.IsDaylightSavingTime(dateIn) Then
            LocalTimeZone -= tz.GetDaylightChanges(dateIn.Year).Delta.TotalMinutes
        End If

        'we need to invert the timezone, when the timezone is GMT-8 for example we need to add 8 hours
        'to get GMT and if the timezone is GMT+2 for Example we need to deduct 2 hours to get GMT
        Dim InvertedTimeZone As Double = 0 - LocalTimeZone
        'Calculate the GMT time of the input date, all calculations will be based on this
        Dim GMTTime As DateTime = dateIn.AddMinutes(InvertedTimeZone)

        'the module time zone will be a our fallback timezone, this will allow you to use the modules configured timezone if all else fails
        Dim ModuleTimeZone As Integer = CType(Settings("TimeZone"), Integer)
        Dim ModuleTime As DateTime = GMTTime.AddMinutes(ModuleTimeZone)
        dateOut = ModuleTime

        If HttpContext.Current.Request.IsAuthenticated Then
            Dim objUser As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo
            Dim UserTimeZone As Double = objUser.Profile.TimeZone
            Dim UserTime As DateTime = GMTTime.AddMinutes(UserTimeZone)
            dateOut = UserTime
        End If
    Catch
        ' The added or subtracted value results in an un-representable DateTime. Possibly a problem with TimeZone values.
        dateOut = dateIn
    End Try

    Return dateOut

End Function

posted @ 16 July 2008 20:42 by Roger Selwyn

Previous Page | Next Page

COMMENTS

Name (required)

Email (required)

Website

Enter the code shown above in the box below