1

実際には文書化されていない C++ API のラッパーを使用しています。公開されたメソッドの中には、uint 型のフィールド (from および to) を必要とするものがあります。フィールドは実際には datefrom と dateto ですが、型はそうではありません。datetime を DOS unsigned int 表現に変換するなど、さまざまなアプローチを試しました

 public  ushort ToDosDateTime( DateTime dateTime)
    {
        uint day = (uint)dateTime.Day;              // Between 1 and 31
        uint month = (uint)dateTime.Month;          // Between 1 and 12
        uint years = (uint)(dateTime.Year - 1980);  // From 1980

        if (years > 127)
            throw new ArgumentOutOfRangeException("Cannot represent the year.");

        uint dosDateTime = 0;
        dosDateTime |= day << (16 - 16);
        dosDateTime |= month << (21 - 16);
        dosDateTime |= years << (25 - 16);

        return unchecked((ushort)dosDateTime);
    }

、それでもAPI関数呼び出しはエラーでなければ何も返しませんでした。、私は : 20160101 のような単純な表現も試しましたが、これは理にかなっていますが成功しませんでした。日付と時刻を符号なし整数として表す既知の方法はありますか?

4

2 に答える 2

2

APIからuintとして取得した日付でテストしたこの関数を作成しました。

    public DateTime FromCtmToDateTime(uint dateTime)
    {
        DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return startTime.AddSeconds(Convert.ToDouble( dateTime));
    }

@ChrisF:これを試してみましたが、うまくいきました。これは確かに C 時間です。つまり、開始日は 1970 年午前 0 時 - 1 -1 です。uint は、その日付からの秒数を表します。

作業関数から取得した出力日付を処理することで意味のある日付を正常に取得し、次を使用して逆に変換しました。

   public  UInt32 ToDosDateTime( DateTime dateTime)
    {
        DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        TimeSpan currTime = dateTime - startTime;
        UInt32 time_t = Convert.ToUInt32(Math.Abs(currTime.TotalSeconds));
        return time_t;
    }
于 2016-02-10T12:30:35.580 に答える
1

.NET 自体はDateTime、0001 年 1 月 1 日からのティックを表す unsigned long として格納されます。参照元から:

// The data is stored as an unsigned 64-bit integeter
//   Bits 01-62: The value of 100-nanosecond ticks where 0 represents 1/1/0001 12:00am, up until the value
//               12/31/9999 23:59:59.9999999
//   Bits 63-64: A four-state value that describes the DateTimeKind value of the date time, with a 2nd
//               value for the rare case where the date time is local, but is in an overlapped daylight
//               savings time hour and it is in daylight savings time. This allows distinction of these
//               otherwise ambiguous local times and prevents data loss when round tripping from Local to
//               UTC time.
private UInt64 dateData;

また、UNIX では時刻の保存方法が少し異なります。ウィキペディアによると:

1970 年 1 月 1 日木曜日の協定世界時 (UTC) 00:00:00 から経過した秒数として定義されます。

したがって、符号なし整数で簡単に表現できます。DateTime コードをあまり使わずに変換できます。

于 2016-02-10T11:51:22.957 に答える