2010-05-17 4 views

Répondre

33

Il n'y a pas d'en-tête "Accept-Timezone" (ou similaire) - le standard HTTP ne contient aucune fonction permettant au navigateur de dire automatiquement au serveur le fuseau horaire dont l'utilisateur se soucie.

L'approche de base consiste à utiliser une date et à lire les informations de TZ.

Comme il n'y a pas de norme (fuseau horaire multi-navigateur nommant les fonctions - vous devez recourir à quelque chose (beurk):

function getTimezoneName() { 
    tmSummer = new Date(Date.UTC(2005, 6, 30, 0, 0, 0, 0)); 
    so = -1 * tmSummer.getTimezoneOffset(); 
    tmWinter = new Date(Date.UTC(2005, 12, 30, 0, 0, 0, 0)); 
    wo = -1 * tmWinter.getTimezoneOffset(); 

    if (-660 == so && -660 == wo) return 'Pacific/Midway'; 
    if (-600 == so && -600 == wo) return 'Pacific/Tahiti'; 
    if (-570 == so && -570 == wo) return 'Pacific/Marquesas'; 
    if (-540 == so && -600 == wo) return 'America/Adak'; 
    if (-540 == so && -540 == wo) return 'Pacific/Gambier'; 
    if (-480 == so && -540 == wo) return 'US/Alaska'; 
    if (-480 == so && -480 == wo) return 'Pacific/Pitcairn'; 
    if (-420 == so && -480 == wo) return 'US/Pacific'; 
    if (-420 == so && -420 == wo) return 'US/Arizona'; 
    if (-360 == so && -420 == wo) return 'US/Mountain'; 
    if (-360 == so && -360 == wo) return 'America/Guatemala'; 
    if (-360 == so && -300 == wo) return 'Pacific/Easter'; 
    if (-300 == so && -360 == wo) return 'US/Central'; 
    if (-300 == so && -300 == wo) return 'America/Bogota'; 
    if (-240 == so && -300 == wo) return 'US/Eastern'; 
    if (-240 == so && -240 == wo) return 'America/Caracas'; 
    if (-240 == so && -180 == wo) return 'America/Santiago'; 
    if (-180 == so && -240 == wo) return 'Canada/Atlantic'; 
    if (-180 == so && -180 == wo) return 'America/Montevideo'; 
    if (-180 == so && -120 == wo) return 'America/Sao_Paulo'; 
    if (-150 == so && -210 == wo) return 'America/St_Johns'; 
    if (-120 == so && -180 == wo) return 'America/Godthab'; 
    if (-120 == so && -120 == wo) return 'America/Noronha'; 
    if (-60 == so && -60 == wo) return 'Atlantic/Cape_Verde'; 
    if (0 == so && -60 == wo) return 'Atlantic/Azores'; 
    if (0 == so && 0 == wo) return 'Africa/Casablanca'; 
    if (60 == so && 0 == wo) return 'Europe/London'; 
    if (60 == so && 60 == wo) return 'Africa/Algiers'; 
    if (60 == so && 120 == wo) return 'Africa/Windhoek'; 
    if (120 == so && 60 == wo) return 'Europe/Amsterdam'; 
    if (120 == so && 120 == wo) return 'Africa/Harare'; 
    if (180 == so && 120 == wo) return 'Europe/Athens'; 
    if (180 == so && 180 == wo) return 'Africa/Nairobi'; 
    if (240 == so && 180 == wo) return 'Europe/Moscow'; 
    if (240 == so && 240 == wo) return 'Asia/Dubai'; 
    if (270 == so && 210 == wo) return 'Asia/Tehran'; 
    if (270 == so && 270 == wo) return 'Asia/Kabul'; 
    if (300 == so && 240 == wo) return 'Asia/Baku'; 
    if (300 == so && 300 == wo) return 'Asia/Karachi'; 
    if (330 == so && 330 == wo) return 'Asia/Calcutta'; 
    if (345 == so && 345 == wo) return 'Asia/Katmandu'; 
    if (360 == so && 300 == wo) return 'Asia/Yekaterinburg'; 
    if (360 == so && 360 == wo) return 'Asia/Colombo'; 
    if (390 == so && 390 == wo) return 'Asia/Rangoon'; 
    if (420 == so && 360 == wo) return 'Asia/Almaty'; 
    if (420 == so && 420 == wo) return 'Asia/Bangkok'; 
    if (480 == so && 420 == wo) return 'Asia/Krasnoyarsk'; 
    if (480 == so && 480 == wo) return 'Australia/Perth'; 
    if (540 == so && 480 == wo) return 'Asia/Irkutsk'; 
    if (540 == so && 540 == wo) return 'Asia/Tokyo'; 
    if (570 == so && 570 == wo) return 'Australia/Darwin'; 
    if (570 == so && 630 == wo) return 'Australia/Adelaide'; 
    if (600 == so && 540 == wo) return 'Asia/Yakutsk'; 
    if (600 == so && 600 == wo) return 'Australia/Brisbane'; 
    if (600 == so && 660 == wo) return 'Australia/Sydney'; 
    if (630 == so && 660 == wo) return 'Australia/Lord_Howe'; 
    if (660 == so && 600 == wo) return 'Asia/Vladivostok'; 
    if (660 == so && 660 == wo) return 'Pacific/Guadalcanal'; 
    if (690 == so && 690 == wo) return 'Pacific/Norfolk'; 
    if (720 == so && 660 == wo) return 'Asia/Magadan'; 
    if (720 == so && 720 == wo) return 'Pacific/Fiji'; 
    if (720 == so && 780 == wo) return 'Pacific/Auckland'; 
    if (765 == so && 825 == wo) return 'Pacific/Chatham'; 
    if (780 == so && 780 == wo) return 'Pacific/Enderbury' 
    if (840 == so && 840 == wo) return 'Pacific/Kiritimati'; 
    return 'US/Pacific'; 
} 
+4

Il y a beaucoup de façons que cela peut être inexact. Mais c'est bon pour une première estimation. – bobince

0

Vous ne pouvez pas récupérer le fuseau horaire sans quelque chose en cours d'exécution sur le client (par exemple JavaScript) comme ASP.NET est du côté serveur.

+2

Ouais j'espérais que quelque chose soit passé dans les en-têtes et que je puisse le déterminer sur le serveur et rendre la page appropriée. – Matt

+0

Supposons que j'utilise JS alors, comment puis-je obtenir le fuseau horaire via JS sur l'événement C# page_Load? –

+0

Vous ne pouvez pas, vous devez l'envoyer avec une requête AJAX, puis le stocker par ex. dans la session. – ThiefMaster

6

Vous ne pouvez pas obtenir obtenir de manière fiable les paramètres de fuseau horaire du client à partir du navigateur! au client ou côté serveur.

Vous pouvez, à partir de JavaScript, obtenir le décalage du fuseau horaire actuel de l'UTC, en utilisant Date#getTimezoneOffset. Cela ne permet pas d'identifier de manière unique un fuseau horaire, mais, comme il y a beaucoup de zones qui peuvent utiliser le même décalage UTC, plus de nombreuses régions qui changent de temps zones sur DST.

Vous pouvez parfois obtenir une chaîne représentant le nom abrégé du fuseau horaire en appelant Date#toLocaleString. Il n'y a aucune garantie qu'il y aura un nom de fuseau horaire utilisable, et les noms de fuseau horaire ne sont pas globalement uniques, et parfois le navigateur se trouve (en particulier à propos de l'heure d'été). Mais il y a souvent quelque chose, et si vous pouvez lier cette abréviation avec le décalage que vous avez déjà lu, et comparez cela avec une liste restreinte de règles de fuseau horaire connues, cela vous donne une bonne idée. Vous pouvez également utiliser le ciblage IP géographique et l'utilisation côté serveur de l'en-tête Accept-Language pour vous aider à mieux deviner. S'il est important que les utilisateurs voient les heures locales appropriées, vous devrez leur donner un mécanisme manuel pour choisir un paramètre régional de fuseau horaire (par exemple, en tant que paramètre par compte), car rien de tout cela n'est fiable.

+0

Comment obtenir le fuseau horaire via JS sur l'événement C# page_Load? –

9

Vous devez compter sur javascript. Voici une bibliothèque pour le faire - detect_timezone.js

+0

J'ai un problème avec cette lib: https://bitbucket.org/pellepim/jstimezonedetect/issue/106/america-toronto – ses