2

アプリケーションで時間の変換を処理する必要があります。できるだけ標準ライブラリ関数を使うことにこだわりたいです。現在、システム タイム ベースとして time_t 構造体を使用しています。ただし、一部のデバイスは時刻を自分のデバイスに同期できますが、その時刻は UTC である場合とそうでない場合があります。また、私のデバイスは時刻を別のデバイスと同期し、その時刻は常に UTC になります。

とにかく、デバイスに同期されている時間のタイムゾーンと、DST を使用しているかどうかをユーザーに尋ねることができます。デバイスが時刻同期を取得したら、mktime を使用してタイム スタンプを直接生成できます (デバイスのシステム時刻は、タイム スタンプの目的で同期している時刻と一致する必要があります。それ以外の場合は、常に変換を行う必要があります)。時刻同期が UTC 以外のソースからのものであることがわかっている場合は、gmtime() を使用して UTC 時刻を取得します。問題は、デフォルトで localtime() と gmtime() が同じ値を返すことです。これは、ライブラリがデフォルトでストレート UTC 時間であり、DST またはタイムゾーン オフセットがないと見なすためです。

そこで、ライブラリの __getzone 関数を実装して上書きすることが、これに対処する方法だと思います。

EW430_CompilerReference.pdfの106ページより

__time32、__time64、および日付関数を機能させるには、関数 clock、__time32、__time64、および __getzone を実装する必要があります。__time32 または __time64 のどちらを使用するかは、time_t に使用するインターフェイスによって異なります。time.h、304 ページを参照してください。

...

__getzone のデフォルトの実装では、タイム ゾーンとして UTC (協定世界時) が指定されます。

Q1: 私が望むことを行う最善の方法は、この __getzone 関数を実装することであるという私の推論は正しいですか?

私が躊躇している理由は、 __getzone によって返される値が、次のようなフォーマットの奇妙な文字列であるためです。

:[XXX[:YYY[:NNN[:DST[:DST ...]]]]]

ここで、XXX は標準のタイムゾーン名 (EST の GMT-5 など)、YYY は夏時間のタイムゾーン名 (EST の GMT-4 など)、NNN は HHMM 形式の UTC からの数値オフセットです (- 記号を含む場合があります)。 )、そして DST は、独自の煩わしい書式設定を持つ夏時間ルールのオプションの文字列を指定します。

とにかく、私は同じ DST ルールを持つカナダと米国だけを心配しているので、これは今の私にとってはかなり簡単です。

Q2: その文字列を形成するためのサンプル コードを持っている人はいますか?

4

4 に答える 4

2

これが __getzone() の私の実装です。したがって、システムのタイムベースは UTC になります。ユーザーがシステムを構成するときに、タイム ソースが UTC を提供していない場合、ローカル タイムを尋ねます。次に、彼らが私のシステムに時刻同期を提供すると、彼らが提供する時刻は MKTIME への呼び出しによって UTC に変換されます (DST ルールが考慮されます)。次に、時間がユーザーにレンダリングされると、 localtime() の呼び出しによって行われます。

これを実装するプロセスで学んだもう 1 つのことは、IAR の MKTIME() の実装は __getzone() を呼び出すが、tm_isdst を「-1」に設定しない限り、DST 規則は考慮されないということです。-1 は、ルールに基づいて DST を適用するかどうかを判断する MKTIME() への呼び出しを行います。

/*!
* \brief Overrides default library function __getzone to support different time
* zones and DST rules.
* \returns Pointer to a const string containing the timezone + dst rules
*
* This function supports all time zones and DST rules for the U.S. and Canada.
*
* \par IAR Notes
* The return value should be a string on the following form:
* \code
* :[XXX[:YYY[:NNN[:DST[:DST ...]]]]]
* \endcode
* \par
* Where \b XXX is the standard time-zone name, \b YYY is the daylight
* savings time-zone name, \b NNN is the time zone offset, and the DSTs
* are the daylight savings time rules. Daylight savings time will add
* one hour to the normal time.
* \par
* The time zone offset \b NNN is specified as a number relative to UTC,
* possibly negative (east is positive), on the format HHMM, where HH
* is hours and MM is minutes.
* \par
* The DSTs specifes a set of rules for how daylight savings time is
* applied. The rules must be sorted in increasing date order starting
* from the earliest date. The first rule for a specific year will
* enable DST, the next will disable it, and so on. Each rule is on
* the following form:
* \code
*   [(YYYY)]MMDD[HH][-W|+W]
* \endcode
*
* * \b (YYYY) is the first year the daylight savings rule was applied.
*      It is optional. If not specified it will default to the same
*      year as the previous rule or zero if no previous rule.
* * \b MM is the month number (1-12).
* * \b DD is the day of the month (1-31).
* * \b HH is the hour number in a 24-hour day (optional, defaults to 0).
* * \b +/-W specifies the day of the week the rule takes effect (where
*      Sunday = 0, Monday = 1, etc). +W means that the rule applies
*      to the first such day on or after the specified date and -W
*      strictly before the date. If this is not specified, the rule
*      will take effect on the exact date, regardless of the day of
*      the week.
*
* \par Example
* U.S. Eastern Standard time is UTC -5. Eastern Daylight time is UTC -4.
* Daylight time goes into affect on the second sunday of March at 2:00AM local
* time. Daylight time ends on the first sunday of November at 2:00AM local
* time. The law that defines this went into affect in 2007.
* Therefore here is how the DST string is constructed:
* | \| | STD Time | \| | DST Time | \| | UTC Offset | \| | DST Rule Year | Month DST Starts | Day DST Starts | Hour DST Starts | Day of Week | \| | Month DST Ends | Day DST Ends | Hour DST Ends | Day of Week |
* |----|----------|----|----------|----|------------|----|---------------|------------------|----------------|-----------------|-------------|----|----------------|--------------|---------------|-------------|
* | :  |  XXX     | :  | YYY      | :  | NNN        | :  | (YYYY)        | MM               | DD*            | HH              | +/-W**      | :  | MM             | DD           | HH            | +/-W        |
* | :  |  GMT-5   | :  | GMT-4    | :  | -0500      | :  | (2007)        | 03               | 08             | 02              | +0          | :  | 11             | 01           | 02            | +0          |
* - * An 8 for the day means that DST will start around the 8th day of the
*   month. Or that the +/-W parameter is relative to the 8th day of the month.
* - ** A + here means that the DST rule will start \b on or \b after the
*   previously specified day (the 8th). 0 means that it should happen on a
*   sunday. Therefore if the 8th is a sunday (and the 8th cannot be the first
*   sunday of the month) then the rule will take affect on that day - or it
*   will happen on the very next sunday.
* \par
* Result:
* \code
* :GMT-5:GMT-4:-0500:(2007)030802+0:110102+0
* \endcode
*
* \sa
* - time_zones - Supported time zones
*/
const char8_t * __getzone(void)
{
    const char8_t *current_zone = NULL;
    static const char8_t dst_time_zones[][50] =
    {
        // UTC time
        ":GMT+0:GMT+0:0000:0",
        // Newfoundland Standard Time UTC – 3:30
        ":GMT-3:GMT-2:-0330:(2007)030802+0:110102+0",
        // Atlantic Standard Time, UTC – 4
        ":GMT-4:GMT-3:-0400:(2007)030802+0:110102+0",
        // Eastern Standard Time, UTC – 5
        ":GMT-5:GMT-4:-0500:(2007)030802+0:110102+0",
        // Central Standard Time, UTC – 6
        ":GMT-6:GMT-5:-0600:(2007)030802+0:110102+0",
        // Mountain Standard Time, UTC – 7
        ":GMT-7:GMT-6:-0700:(2007)030802+0:110102+0",
        // Pacific Standard Time, UTC – 8
        ":GMT-8:GMT-7:-0800:(2007)030802+0:110102+0",
        // Alaska Standard Time, UTC – 9
        ":GMT-9:GMT-8:-0900:(2007)030802+0:110102+0",
        // Hawaii-Aleutian Standard Time, UTC – 10
        ":GMT-10:GMT-9:-1000:(2007)030802+0:110102+0"
    };

    static const char8_t std_time_zones[][20] =
    {
        // UTC time
        ":GMT+0:GMT+0:0000",
        // Newfoundland Standard Time UTC – 3:30
        ":GMT-3:GMT-2:-0330",
        // Atlantic Standard Time, UTC – 4
        ":GMT-4:GMT-3:-0400",
        // Eastern Standard Time, UTC – 5
        ":GMT-5:GMT-4:-0500",
        // Central Standard Time, UTC – 6
        ":GMT-6:GMT-5:-0600",
        // Mountain Standard Time, UTC – 7
        ":GMT-7:GMT-6:-0700",
        // Pacific Standard Time, UTC – 8
        ":GMT-8:GMT-7:-0800",
        // Alaska Standard Time, UTC – 9
        ":GMT-9:GMT-8:-0900",
        // Hawaii-Aleutian Standard Time, UTC – 10
        ":GMT-10:GMT-9:-1000"
    };

    switch(get_config()->time_zone)
    {
        case NST:
        {
            if(get_config()->b_dst)
            {
                current_zone = dst_time_zones[NST];
            }
            else
            {
                current_zone = std_time_zones[NST];
            }
        }
        break;

        case AST:
        {
            if(get_config()->b_dst)
            {
                current_zone = dst_time_zones[AST];
            }
            else
            {
                current_zone = std_time_zones[AST];
            }
        }
        break;

        case EST:
        {
            if(get_config()->b_dst)
            {
                current_zone = dst_time_zones[EST];
            }
            else
            {
                current_zone = std_time_zones[EST];
            }
        }
        break;

        case CST:
        {
            if(get_config()->b_dst)
            {
                current_zone = dst_time_zones[CST];
            }
            else
            {
                current_zone = std_time_zones[CST];
            }
        }
        break;

        case MST:
        {
            if(get_config()->b_dst)
            {
                current_zone = dst_time_zones[MST];
            }
            else
            {
                current_zone = std_time_zones[MST];
            }
        }
        break;

        case PST:
        {
            if(get_config()->b_dst)
            {
                current_zone = dst_time_zones[PST];
            }
            else
            {
                current_zone = std_time_zones[PST];
            }
        }
        break;

        case AKST:
        {
            if(get_config()->b_dst)
            {
                current_zone = dst_time_zones[AKST];
            }
            else
            {
                current_zone = std_time_zones[AKST];
            }
        }
        break;

        case HAST:
        {
            if(get_config()->b_dst)
            {
                current_zone = dst_time_zones[HAST];
            }
            else
            {
                current_zone = std_time_zones[HAST];
            }
        }
        break;

        case UTC:
        default:
            current_zone = std_time_zones[UTC];
        break;
    }

    return current_zone;
}
于 2014-07-08T20:02:55.483 に答える
2

上記のウクライナの規則は、31 日になる日曜日には適用されないため、完全には正しくありません。getzone.c のルールを読むと、 -W は厳密にこの日付の前の日曜日を意味します。

+/-W specifies the day of the week the rule takes effect (where
    Sunday = 0, Monday = 1, etc). +W means that the rule applies
    to the first such day on or after the specified date and -W
    strictly before the date. If this is not specified, the rule
    will take effect on the exact date, regardless of the day of
    the week.

正しいルールは次のようになります

char const * __getzone()
{
    return ":GMT+2:GMT+3:0200:(1996)032503+0:102504+0";
  // Ukraine; rule: (last Sun (March | October) )
}
于 2015-08-21T15:25:12.697 に答える
1
char const * __getzone()
{
        return ":GMT+2:GMT+3:0200:(1996)033103-0:103104-0";



    // Ukraine; rule: (last Sun (March | October) )
}
于 2014-06-12T14:09:46.000 に答える