Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 11th, 2007, 03:00 PM
Friend of Wrox
 
Join Date: Aug 2006
Location: , , .
Posts: 131
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to kherrerab Send a message via Yahoo to kherrerab
Default adjust zone time

I have my site (www.clubvwnica.com) in a UK server. The forums shows the time of UK and would like to adjust it to central america zone time.

How can i do this?

  #2 (permalink)  
Old June 11th, 2007, 04:11 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

kherrerab,

look up some of the DateTime.UtcNow and DateTime.ToLocalTime functions on google (i suggest this as i don't know offhand exactly what you need to do, tho' i did do something on this a while back but cant remeber exactly what it was :)...

if i stumble across it, i'll paste the example here.


jimi

http://www.jamestollan.com
  #3 (permalink)  
Old June 11th, 2007, 07:13 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

This may be of some use to you:
http://www.codeproject.com/useritems..._Time_Zone.asp

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
  #4 (permalink)  
Old June 12th, 2007, 09:33 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

kherrerab,

following on from dparsons post above, i used the javascript section in the article and created a few helper methods. here's what i have in my app.

Template.Master (the javascript portion):

<script type="text/javascript">
    /* <![CDATA[ */
        function AdjustColumnsHeight()
        {
            // get a reference to the three DIVS that make up the columns
            var centerCol = window.document.getElementById('centercol');
            var leftCol = window.document.getElementById('leftcol');
            var rightCol = window.document.getElementById('rightcol');
            // calculate the max height
            var hCenterCol = centerCol.offsetHeight;
            var hLeftCol = leftCol.offsetHeight;
            var hRightCol = rightCol.offsetHeight;
            var maxHeight = Math.max(hCenterCol, Math.max(hLeftCol, hRightCol));
            // set the height of all 3 DIVS to the max height
            centerCol.style.height = maxHeight + 'px';
            leftCol.style.height = maxHeight + 'px';
            rightCol.style.height = maxHeight + 'px';
            // Show the footer
            window.document.getElementById('footer').style.vis ibility = 'inherit';
            // always get the offset with each pageload
            GetLocalTimeOffset();
        }
        function GetLocalTimeOffset()
        {
            var rightNow = new Date();
            var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
            var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
            var temp = date1.toGMTString();
            var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
            var temp = date2.toGMTString();
            var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
            var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
            var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
            if (hoursDiffDaylightTime == hoursDiffStdTime)
            {
                //Daylight Saving Time is NOT observed here.
                createCookie("daylightSavingTime", "false", 0);
            }
            else
            {
                //Daylight Saving Time is observed here.
                createCookie("daylightSavingTime", "true", 0);
            }
            createCookie("ClientDateTime", rightNow.toUTCString(), 0);
        }
        function createCookie(name,value,days)
        {
            if (days && days > 0)
            {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else {var expires = "";}
            document.cookie = name+"="+value+expires+"; path=/";
        }
        window.onload = function() { AdjustColumnsHeight(); }
    /* ]]> */
    </script>

I've then got these two little methods in my Helpers.cs class:

        public static DateTime GetUserDateTime(HttpRequest Request, ProfileCommon Profile)
        {
            if (Request.Cookies["ClientDateTime"] != null && Request.Cookies["daylightSavingTime"] != null)
            {
                try
                {
                    // try to add to Profile as only logged in user will be able to use this
                    // if we want anonymous users to be able to use, just remove the two Profile lines
                    // and use two variables to capture the Cookie values
                    Profile.ClientDateTime = (string)Request.Cookies["ClientDateTime"].Value.Replace("UTC", "");
                    Profile.daylightSavingTime = bool.Parse(Request.Cookies["daylightSavingTime"].Value);
                    DateTime clientTime = DateTime.Parse(Profile.ClientDateTime);
                    return clientTime.AddHours(Profile.daylightSavingTime ? 1 : 0);
                }
                catch { return DateTime.Now; }
            }
            return DateTime.Now;
        }

        public static DateTime AdjustDateTimeToLocal(DateTime datetimeTobeAdjusted, DateTime userDateTime)
        {
            //Move to universal time (GMT) with Zero offset
            DateTime utcDateTime = DateTime.Now.ToLocalTime();
            //Add client side offset
            TimeSpan timeDiff = userDateTime.Subtract(utcDateTime);
            // clocks should be within 20 mins of each other but just in case!!
            // we're accouting here for tehran, bombay adelaide etc that is on a 30 min offset
            datetimeTobeAdjusted = datetimeTobeAdjusted.AddMinutes((Math.Abs(timeDiff .Minutes) > 20) ? timeDiff.Minutes : 0);
            datetimeTobeAdjusted = datetimeTobeAdjusted.AddHours(timeDiff.Hours);
            return datetimeTobeAdjusted;
        }

This allows me to either 'adjust' a passed in datetime (i.e. a post time for example) or simply query the user's current datetime (should he/she request info at a particular time of day etc..). for example, here's the portion of code from my ShowThread.aspx page that displays the date:

<asp:Literal ID="lblAddedDate" runat="server" Text='<%# string.Format("{0:g}", Helpers.AdjustDateTimeToLocal(Convert.ToDateTime(E val("AddedDate")), Convert.ToDateTime(Helpers.GetUserDateTime(Request , Profile)))) %>' />

obviously, the functions can be run from 'normal' cs files to derive various offsets as well.

hope this helps

[edit] - oops, just realised that i'd forgotten to add the section that's required for the Profile stuff in the web.config file. anyway, here it is. Under the <profile> section (under <properties>) add the following (doesn't matter where, i did it after ShoppingCart)

        <add name="ClientDateTime" type="String"/>
        <add name="daylightSavingTime" defaultValue="false" type="bool"/>

[end edit]

jimi

http://www.jamestollan.com
  #5 (permalink)  
Old July 1st, 2007, 09:13 PM
Friend of Wrox
 
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts
Default

And If you change your hoster to another country???
You'll have two datetimes in same table!

I think it's better to save yours local time Country in all database inserts...
That time never change... and then you can use this script using yours(webMaster) localTime as base!

I had to change my hostCompany one time and I was glad to think about this earlier...

  #6 (permalink)  
Old July 2nd, 2007, 03:22 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

Maxxim - good point. i think however, if you take that approach, then it's better to save to UTCTime **ALL THE TIME**. that way, you have a 'zero based' benchmark from which to 'serve' all other 'localisations'. also, should you change your country of residence (as the site admin), then you don't have to remember which offset you were basing your time calculations on (especially if you're on the run etc :D)

jimi

http://www.jamestollan.com
  #7 (permalink)  
Old July 2nd, 2007, 08:53 AM
Friend of Wrox
 
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes jimibt!

That way is more professional

But in my case, i use direct database querys to see some information...
I had no time yet to make pages to show that data!
So, when i retrieve values from db i see my country datetime... And this way is better for me!

But if my site was made for sell I had needed to make your solution, because i don't know where my client will use the webapplication!


 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
date and time in EST time zone anboss XSLT 1 May 21st, 2008 01:42 PM
GMT(Time Zone) Help Mukesh Dubey ASP.NET 2.0 Professional 1 May 7th, 2008 09:18 PM
time zone & day light time rajn ASP.NET 1.0 and 1.1 Professional 0 August 7th, 2007 05:02 PM
Zone Time kherrerab BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 October 20th, 2006 12:30 PM
getting time zone nerssi Javascript 2 December 22nd, 2004 10:56 AM



All times are GMT -4. The time now is 10:31 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.