Beispiel #1
0
        public SimpleTime toUTC(int year, int month, int day, int h, int m)
        {
            DateTime dt = new DateTime(year, month, day, h, m, 0, DateTimeKind.Utc);
            long     s  = toSecsSinceEpoch(year, month, day, h, m);
            TZType   t  = getTZ(s);

            dt = dt.AddSeconds(-t.offset);
            SimpleTime ret = new SimpleTime();

            ret.year   = dt.Year;
            ret.month  = dt.Month;
            ret.day    = dt.Day;
            ret.hour   = dt.Hour;
            ret.minute = dt.Minute;
            return(ret);
        }
Beispiel #2
0
        /*protected void loadFile(string filename) {
         * }*/
        protected void loadFile(BinaryReader ins)
        {
            //QDataStream::ByteOrder origByteOrder = in->byteOrder();
            //in->setByteOrder(QDataStream::BigEndian);
            ins.BaseStream.Seek(28, SeekOrigin.Current); //->skipRawData(28);
            int leapcnt;


            int charcnt;

            leapcnt = swapBytes32(ins.ReadInt32());
            timecnt = swapBytes32(ins.ReadInt32());
            typecnt = swapBytes32(ins.ReadInt32());

            charcnt = swapBytes32(ins.ReadInt32());

            //qint32* transTimes;	// transition times
            //qint8* transTypes;	// timezone description for each transition

            transTimes = new int[timecnt]; //](qint32*)malloc(sizeof(qint32) * timecnt);
            for (int i = 0; i < timecnt; i++)
            {
                transTimes[i] = swapBytes32(ins.ReadInt32());
            }



            transTypes = new sbyte[timecnt]; //(qint8*)malloc(sizeof(qint8) * timecnt);
            for (int i = 0; i < timecnt; i++)
            {
                transTypes[i] = ins.ReadSByte();
            }

            Int32[] offset = new Int32[typecnt];
            sbyte[] dst    = new sbyte[typecnt];
            sbyte[] idx    = new sbyte[typecnt];

            //Int32 tmp32;
            //sbyte tmp8;
            for (int i = 0; i < typecnt; i++)
            {
                offset[i] = swapBytes32(ins.ReadInt32());
                dst[i]    = ins.ReadSByte();
                idx[i]    = ins.ReadSByte();

                //tmp32 = offset[i];
                //tmp8 = dst[i];
                //tmp8 = idx[i];
            }
            sbyte[] str = new sbyte[charcnt];
            for (int i = 0; i < charcnt; i++)
            {
                str[i] = ins.ReadSByte();
            }

            //tz = new TZType[typecnt];
            for (int i = 0; i < typecnt; ++i)
            {
                // find string
                int pos = idx[i];
                int end = pos;
                while (str[end] != 0)
                {
                    ++end;
                }
                //QString tmp = QString(str[pos], end-pos);
                string name = "";
                for (int p = pos; p < end; p++)
                {
                    name += (char)str[p];
                }
                //char ctmp[100];
                //memcpy(&ctmp, &str[pos], end-pos);
                //ctmp[end-pos] = 0;

                tztypes.Add(new TZType(name, offset[i], dst[i] != 0));
            }

            Int32[] leapSecs = new Int32[leapcnt * 2];
            for (int i = 0; leapcnt > 0; --leapcnt)
            {
                leapSecs[i++] = swapBytes32(ins.ReadInt32());
                leapSecs[i++] = swapBytes32(ins.ReadInt32());
            }

            //f.close();
            //in->setByteOrder(origByteOrder);

            // Set default timezone (normaltz).
            // First, set default to first non-DST rule.
            int n = 0;

            while (tztypes[n].dst && n < tztypes.Count)
            {
                ++n;
            }
            normalTZ = tztypes[n];

            // When setting "normaltz" (the default timezone) in the constructor,
            // we originally took the first non-DST rule for the current TZ.
            // But this produces nonsensical results for areas where historical
            // non-integer time zones were used, e.g. if GMT-2:33 was used until 1918.

            // This loop, based on a suggestion by Ophir Bleibergh, tries to find a
            // non-DST rule close to the current time. This is somewhat of a hack, but
            // much better than the previous behavior in this case.

            // Tricky: we need to get either the next or previous non-dst TZ
            // We shall take the future non-dst value, by trying to add 3 months at a
            // time to the current date and searching.
            // (QT 4.7 only) qint64 ts = QDateTime::currentMSecsSinceEpoch() / 1000;
            long ts = currentSecsSinceEpoch(); //::currentDateTime().toTime_t();

            //final long ts = System.currentTimeMillis() / 1000;
            for (int i = 0; i < 9; i++)
            {
                TZType currTz = getTZ(ts + secsPerThreeMonths * i);
                if (!currTz.dst)
                {
                    normalTZ = currTz;
                    break;
                }
            }
        }