Example #1
0
        public static TimeZoneInfo ToSystemTimeZone(this PosixTz posixTz, int year, bool isDaylightSavingTime = true)
        {
            if ((object)posixTz == null)
            {
                return(null);
            }
            var id = posixTz.name;

            if (id.Length > 32)
            {
                id = id.Substring(0, 32);
            }
            var baseOffset = TimeSpan.FromSeconds(-posixTz.offset);

            if ((object)posixTz.dst == null || !isDaylightSavingTime)
            {
                return(TimeZoneInfo.CreateCustomTimeZone(
                           id, baseOffset, posixTz.Format(), posixTz.name
                           ));
            }
            var daylightDelta = TimeSpan.FromSeconds(
                posixTz.offset - posixTz.dst.offset
                );
            var start = posixTz.dst.start.ToSystemTransitionTime(year);
            var end   = posixTz.dst.end.ToSystemTransitionTime(year);

            var adjustmentRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(
                new DateTime(year, 1, 1), new DateTime(year, 12, 31), daylightDelta, start, end
                );

            return(TimeZoneInfo.CreateCustomTimeZone(
                       id, baseOffset, posixTz.Format(), posixTz.name, posixTz.dst.name, new[] { adjustmentRule }
                       ));
        }
Example #2
0
        public static DateTime ConvertUtcTimeToLocal(this PosixTz posixTz, DateTime time, bool isDaylightSavingTime = true)
        {
            if ((object)posixTz == null)
            {
                throw new ArgumentNullException("posixTz");
            }
            var tz = posixTz.ToSystemTimeZone(time.Year, isDaylightSavingTime);

            return(TimeZoneInfo.ConvertTimeFromUtc(time, tz));
        }
        public void WriteTimeZone(PosixTz tz)
        {
            WriteName(tz.name);
            WriteTimeOffset(tz.offset);
            var dst = tz.dst;

            if (dst != null)
            {
                WriteName(dst.name);
                if (dst.offset != PosixTz.GetDefaultDstOffset(tz.offset))
                {
                    WriteTimeOffset(dst.offset);
                }
                sb.Append(",");
                WriteRule(dst.start);
                sb.Append(",");
                WriteRule(dst.end);
            }
        }
Example #4
0
        //std offset [dst[offset],start[/time],end[/time]]
        public PosixTz ReadExpandedTimeZone()
        {
            SkipWhiteSpaces();
            string std_name = null;

            if (!ReadName(ref std_name) || std_name.Length < 3)
            {
                //std is required and should be no less than three characters
                RaiseParseError();
            }

            SkipWhiteSpaces();
            int std_ofs = 0;

            if (!ReadTimeOffset(ref std_ofs))
            {
                //The offset following std shall be required.
                RaiseParseError();
            }

            SkipWhiteSpaces();
            if (AtEnd())
            {
                //if dst is missing, then the alternative time does not apply in this locale.
                return(new PosixTz(std_name, std_ofs, null));
            }

            //parse dst clause
            string dst_name = null;

            if (!ReadName(ref dst_name))
            {
                //dst clause should begin with dst name which should be no less than three characters
                RaiseParseError();
            }

            SkipWhiteSpaces();
            int dst_offset = 0;

            if (!ReadTimeOffset(ref dst_offset))
            {
                //if no offset follows dst, the alternative time is assumed to be one hour ahead
                //of standard time.
                dst_offset = PosixTz.GetDefaultDstOffset(std_ofs);
            }

            SkipWhiteSpaces();
            if (!ReadSpecChar(',', ';'))
            {
                //no rules is specified
                RaiseParseError();
            }

            SkipWhiteSpaces();
            PosixTz.DstRule start = null;
            if (!ReadRule(ref start))
            {
                //start rule is not specified
                RaiseParseError();
            }

            SkipWhiteSpaces();
            if (!ReadSpecChar(',', ';'))
            {
                //end rule is not specified
                RaiseParseError();
            }

            SkipWhiteSpaces();
            PosixTz.DstRule end = null;
            if (!ReadRule(ref end))
            {
                //end rule is not specified
                RaiseParseError();
            }

            var dst = new PosixTz.Dst(dst_name, dst_offset, start, end);

            SkipWhiteSpaces();
            if (!AtEnd())
            {
                //unexpected
                RaiseParseError();
            }

            return(new PosixTz(std_name, std_ofs, dst));
        }