Results 1 to 12 of 12

Thread: GMT Offset

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    GMT Offset

    Hi.

    im trying to convert this code into C# but not sure where to begin. maybe because ive been too sucked into VB6!

    Code:
    Private Function GMT_OFFSET() As Integer
        '****DECLARE VARIABLES
        Dim dtNow As Date, dtEngland As Date
        Dim dtGMT As SYSTEMTIME
        '****GET SYSTEM TIME INFORMATION
        Call GetSystemTime(dtGMT)
        dtNow = Now
        dtEngland = dtGMT.wYear & "-" & dtGMT.wMonth & "-" & dtGMT.wDay & " " & dtGMT.wHour & ":" & dtGMT.wMinute & ":" & dtGMT.wSecond
        '****RETURN QUARTER HOURS
        GMT_OFFSET = DateDiff("n", dtEngland, dtNow) \ 15
    End Function
    I know GetSystemTime is a Win32 API but I am sure we dont need to do PInvoke in C# but instead just use the DateTime/TimeSpan objects and the globalization is needs be.

    if the above can be translated into VB.NET then thats fine, I can easily translate to C# (but be sure it is using pure .NET)

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: GMT Offset

    You could just use TimeZone.GetUtcOffset
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: GMT Offset

    try this:

    Code:
    TimeSpan ts = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
    String tsString = ts.ToString();
    MessageBox.Show(tsString);

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: GMT Offset

    Thanks. so how would I then convert the VB6 code into VB.NET or C#?

    This is what I have in C# and it seems to work but fear that it maybe incorrect

    Code:
     DateTime dtNow = DateTime.Now;
                DateTime dtEngland = DateTime.UtcNow;
                int gmtOffset = Convert.ToInt32((dtNow - dtEngland).TotalMinutes) / 15;
    VB.NET:

    Code:
    Dim dtNow as DateTime = DateTime.Now
    Dim dtEngland = DateTime.UtcNow
    Dim gmtOffset = Convert.ToInt32((dtNow - dtEngland).TotalMinutes) / 15

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: GMT Offset

    Here are a couple of ways of getting the GMT Offset.

    Code:
                int gmtOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Hours;
                int gmtOffset2 = (DateTime.Now - DateTime.UtcNow).Hours;
    Not sure if daylight savings is an issue but here's how to find out if it is daylight savings time

    Code:
    var isDaylightSavings = TimeZoneInfo.Local.IsDaylightSavingTime(DateTime.Now);
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: GMT Offset

    thanks, yes I thought as much. but im trying to convert the actual code to VB.NET/C#. I guess "n" is minutes which would equate to TotalMinutes.
    I dont think DLS is an issue here. But the difference between UTC time (england) and the current date/time of the system (wherever the system is in the world) and then divide by 15 to return quarter hours

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  7. #7
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: GMT Offset

    Quote Originally Posted by Techno View Post
    I guess "n" is minutes which would equate to TotalMinutes.
    The 1st parameter of DateDiff is interval with "n" being minutes. Documentation: DateDiff Function.

    To get an exact translation of this in C# you need to reference Microsoft.VisualBasic. Seems like a lot of work to me to get the TotalMinutes / 15.

    Code:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            [DllImport("kernel32.dll")]
            static extern void GetSystemTime(out SYSTEMTIME lpSystemTime);
    
            private void Form1_Load(object sender, EventArgs e)
            {
                int gmtOffset = GMT_OFFSET();
            }
    
            private int GMT_OFFSET()
            {
                DateTime dtNow = DateTime.Now;
                SYSTEMTIME dtGMT;
                GetSystemTime(out dtGMT);
                DateTime dtEngland = new DateTime(dtGMT.wYear, dtGMT.wMonth, dtGMT.wDay, dtGMT.wHour, dtGMT.wMinute, dtGMT.wSecond);
                return (int)(Microsoft.VisualBasic.DateAndTime.DateDiff("n", dtEngland, dtNow) / (long)15);
            }
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEMTIME
        {
            public short wYear;
            public short wMonth;
            public short wDayOfWeek;
            public short wDay;
            public short wHour;
            public short wMinute;
            public short wSecond;
            public short wMilliseconds;
        }
    Edit: seems like there's an off by 1 error in there somewhere as I'm getting -23 where I'd expect -24. (-6 * 4). Not too concerned as I'd just multiply the hours returned by methods provided above by 4 to get the quarter hours rather than this.
    Last edited by MattP; Aug 2nd, 2012 at 04:41 PM. Reason: Long division the correct way.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: GMT Offset

    thanks. yeh, dont wanna do PInvoke! or even reference the VB library in C#

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  9. #9
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: GMT Offset

    Quote Originally Posted by Techno View Post
    thanks. yeh, dont wanna do PInvoke! or even reference the VB library in C#
    But I spent minutes writing up that example showing how efficient it was to not use current .NET methods to do things!
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: GMT Offset

    whilst I do appreciate it, it certainly is not best practice. There is a reason why you shouldnt still use "old style code" when using .NET.
    you will find later on that the VB reference library will be obsolete meaning your app in future when upgrading will be wortheless without having to go through another cycle of maintainence, which costs both time and money

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  11. #11
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: GMT Offset

    Quote Originally Posted by Techno View Post
    whilst I do appreciate it, it certainly is not best practice. There is a reason why you shouldnt still use "old style code" when using .NET.
    you will find later on that the VB reference library will be obsolete meaning your app in future when upgrading will be wortheless without having to go through another cycle of maintainence, which costs both time and money
    Um...yeah. I should really finish up statements like that with </sarcasm> just to make sure.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: GMT Offset

    ah!

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width