TimeZone convertion - InfoPath Dev
in

InfoPath Dev

TimeZone convertion

Last post 10-05-2010 02:16 AM by denfer06. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 10-04-2010 08:57 AM

    TimeZone convertion

    Hello

    I have a field where the user enter a date and time and another where he specify the TimeZone (WindowsId)

    I need to store this date to an SQL table in UTC

    How can I convert this date from the specify timezone to UTC ? 

    I have to deal with the Daylight saving and this is the big problem

    .Net 3.5 provide the TimeZoneInfo Class for that but the TimeZone class from the .net 2.0 that comes with VSTA  is quite limited from convertion to local to UTC

     Do you have any Idea ?

    I'm using Infopath 2007

    thanks in advance

  • 10-05-2010 02:16 AM In reply to

    I finally find a solution that is discribe below.

    in order to have available the TimeZoneInfo Class you have to add the System.Core reference to your C# project (make sure that you have .net 3.5 installed on your computer)

    then you can use the below sample code to convert the date from the specify timezone to UTC ? 

    using Microsoft.Office.InfoPath;
    using System;
    using System.Windows.Forms;
    using System.Xml;
    using System.Xml.XPath;
    using mshtml;

    namespace Default_RFQ_101004
    {
        public partial class FormCode
        {      
            // NOTE: The following procedure is required by Microsoft Office InfoPath.
            // It can be modified using Microsoft Office InfoPath.
            public void InternalStartup()
            {
                EventManager.XmlEvents["/my:Job/my:Definition/@my:TempRequestDate"].Changed += new XmlChangedEventHandler(TempRequestDate_attr_Changed);
                EventManager.XmlEvents["/my:Job/my:Definition/@my:TempDeliveryDate"].Changed += new XmlChangedEventHandler(TempDeliveryDate_attr_Changed);
            }

            public void TempRequestDate_attr_Changed(object sender, XmlEventArgs e)
            {
                XPathNavigator xdoc = this.MainDataSource.CreateNavigator();
                DateTime TempRequestDate = Iso8601ToDate(xdoc.SelectSingleNode("/my:Job/my:Definition/@my:TempRequestDate",this.NamespaceManager).Value.ToString());
                TimeZoneInfo SelectedTimeZoneId = TimeZoneInfo.FindSystemTimeZoneById(xdoc.SelectSingleNode("/my:Job/my:Definition/@my:HandoffTimeZoneID", this.NamespaceManager).Value.ToString());
                DateTime RequestDate = TimeZoneInfo.ConvertTimeToUtc(TempRequestDate, SelectedTimeZoneId);
                xdoc.SelectSingleNode("/my:Job/my:Definition/@my:RequestDate", this.NamespaceManager).SetValue(DateToIso8601(RequestDate));
            }

            public void TempDeliveryDate_attr_Changed(object sender, XmlEventArgs e)
            {
                XPathNavigator xdoc = this.MainDataSource.CreateNavigator();
                DateTime TempDeliveryDate = Iso8601ToDate(xdoc.SelectSingleNode("/my:Job/my:Definition/@my:TempDeliveryDate", this.NamespaceManager).Value.ToString());
                TimeZoneInfo SelectedTimeZoneId = TimeZoneInfo.FindSystemTimeZoneById(xdoc.SelectSingleNode("/my:Job/my:Definition/@my:DeliveryTimeZoneID", this.NamespaceManager).Value.ToString());
                DateTime DeliveryDate = TimeZoneInfo.ConvertTimeToUtc(TempDeliveryDate, SelectedTimeZoneId);
                xdoc.SelectSingleNode("/my:Job/my:Definition/@my:DeliveryDate", this.NamespaceManager).SetValue(DateToIso8601(DeliveryDate));
            }

            private static DateTime Iso8601ToDate(string iso8601Date)
            {
                if (iso8601Date == null)
                    throw new ArgumentNullException("iso8601Date");

                return DateTime.ParseExact(iso8601Date, "yyyy-MM-ddTHH:mm:ss", null);
            }

            private static string DateToIso8601(DateTime dateTime)
            {
                return dateTime.ToString("yyyy-MM-ddTHH:mm:ss");
            }       
        }
    }

Page 1 of 1 (2 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.