private TimeZoneInfo.AdjustmentRule GetApplicableRule(ZTimeZone ourzone, DateTime time)
        {
            TimeZoneInfo zone = ourzone.Underlier;

            if (zone == TimeZoneInfo.Utc)
            {
                return(null);
            }

            var Tclock = time.Ticks + zone.BaseUtcOffset.Ticks;

            // use local to avoid another thread changing reference during test
            var srule = _rule;

            if (srule != null && IsAppropriateRule(srule, Tclock))
            {
                return(srule);
            }

            var rulelist = zone.GetAdjustmentRules();

            foreach (var rule in rulelist)
            {
                if (IsAppropriateRule(rule, Tclock))
                {
                    return(_rule = rule);
                }
            }

            return(null);
        }
Example #2
0
 /// <summary>
 /// Convert time to the specified zone.
 /// </summary>
 /// <param name="zone">Zone.</param>
 public ZDateTime Convert(ZTimeZone zone)
 {
     if (zone == TimeZone)
     {
         return(this);
     }
     else
     {
         return(new ZDateTime(Clock, zone));
     }
 }
Example #3
0
        /// <summary>
        /// Parse date from date & zone spec string
        /// </summary>
        /// <param name='date'>
        /// Date as string in some default known format
        /// </param>
        /// <param name='zone'>
        /// Zone.
        /// </param>
        public static ZDateTime Parse(string date, string zone)
        {
            if (date == null || zone == null)
            {
                throw new ArgumentException("date string or timezone is null");
            }

            ZTimeZone czone = ZTimeZone.Find(zone);

            return(new ZDateTime(date, czone));
        }
Example #4
0
        // Operations


        /// <summary>
        /// Time now in appropriate timezone
        /// </summary>
        /// <param name='zone'>
        /// Zone.
        /// </param>
        public static ZDateTime TimeNowFor(ZTimeZone zone)
        {
            if (zone == ZTimeZone.GMT)
            {
                return(Now);
            }
            else
            {
                return(new ZDateTime(SystemUtils.ClockMillis, zone));
            }
        }
Example #5
0
        /// <summary>
        /// Create date from string & zone spec
        /// </summary>
        /// <param name='date'>
        /// Date as string in some default known format
        /// </param>
        /// <param name='zone'>
        /// Zone.
        /// </param>
        public ZDateTime(string date, ZTimeZone zone = null)
        {
            if (zone == null)
            {
                zone = DefaultZoneFor(date);
            }

            ZDateTime parsed = Parse(date, zone);

            _utc   = parsed.Clock;
            _zone  = parsed.TimeZone;
            _parts = parsed._parts;
        }
Example #6
0
        // Operations


        /// <summary>
        /// Convert to date/time (within current day, may be before current time)
        /// </summary>
        /// <returns>
        /// The date/time set according to this time spec.
        /// </returns>
        /// <param name='zone'>
        /// Zone.
        /// </param>
        public ZDateTime ToTime(long Tnow = 0L, ZTimeZone zone = null)
        {
            if (zone == null)
            {
                zone = ZTimeZone.Local;
            }

            if (Tnow == 0L)
            {
                Tnow = Clock.Now;
            }

            ZDateTime now = new ZDateTime(Tnow, zone);

            return(new ZDateTime(now.Year, now.Month, now.Day, Hour, Minute, Second, MilliSecond, zone));
        }
Example #7
0
        /// <summary>
        /// Create a zoned datetime, converting the local time into the equivalent time of given zone
        /// </summary>
        /// <param name='local'>
        /// Local time
        /// </param>
        /// <param name='zone'>
        /// Zone to convert to
        /// </param>
        public ZDateTime(DateTime local, ZTimeZone zone)
        {
            _parts = 0L;
            switch (local.Kind)
            {
            case DateTimeKind.Local:
                DateTime utc = local.ToUniversalTime();
                _utc  = (utc.Ticks - 621355968000000000L) / 10000L;
                _zone = zone;
                break;

            case DateTimeKind.Utc:
                _utc  = (local.Ticks - 621355968000000000L) / 10000L;
                _zone = zone;
                break;

            default:
                throw new ArgumentException("cannot handled unspecified datetimes");
            }
        }
Example #8
0
        /// <summary>
        /// Specify a time in the given time zone
        /// </summary>
        /// <param name='year'>
        /// Year.
        /// </param>
        /// <param name='month'>
        /// Month.
        /// </param>
        /// <param name='day'>
        /// Day.
        /// </param>
        /// <param name='hr'>
        /// Hr.
        /// </param>
        /// <param name='mins'>
        /// Mins.
        /// </param>
        /// <param name='secs'>
        /// Secs.
        /// </param>
        /// <param name='ms'>
        /// Ms.
        /// </param>
        /// <param name='zone'>
        /// Zone.
        /// </param>
        public ZDateTime(int year, int month, int day, int hr, int mins, int secs, int ms, ZTimeZone zone)
        {
            DateTime baseutc = new DateTime(year, month, Math.Max(day, 1), hr, mins, secs, ms, DateTimeKind.Utc);
            DateTime utc     = (baseutc - zone.BaseUtcOffset);

            var dst = zone.GetDSTInfoFor(utc);

            if (zone.IsDaylightSavingTime(utc))
            {
                _utc = (utc.Ticks - 621355968000000000L) / 10000L - dst.Offset;
            }
            else
            {
                _utc = (utc.Ticks - 621355968000000000L) / 10000L;
            }

            _zone  = zone;
            _parts =
                ((ulong)year << 36) | ((ulong)month << 32) | ((ulong)day << 27) |
                ((ulong)hr << 22) | ((ulong)mins << 16) | ((ulong)secs << 10) | (ulong)ms;
        }
Example #9
0
        /// <summary>
        /// Convert to date/time (within current day if time after current time, otherwise following day)
        /// </summary>
        public ZDateTime ToNextTime(long Tnow = 0L, ZTimeZone zone = null)
        {
            if (zone == null)
            {
                zone = ZTimeZone.Local;
            }

            if (Tnow == 0L)
            {
                Tnow = Clock.Now;
            }

            var Tproj = ToTime(Tnow, zone);

            if (Tproj.Clock >= Tnow)
            {
                return(Tproj);
            }
            else
            {
                return(Tproj.Add(1, 0, 0, 0, 0));
            }
        }
 public ZDateTimeInfoGenerator(ZTimeZone zone)
 {
     _zone        = zone;
     _zone_offset = (long)zone.BaseUtcOffset.TotalMilliseconds;
 }
Example #11
0
 public ZDateTimeRange(string start, string end, ZTimeZone zone)
 {
     _start = new ZDateTime(start, zone);
     _end   = new ZDateTime(end, zone);
 }
Example #12
0
 /// <summary>
 /// Get Time component of given date
 /// </summary>
 /// <param name='clock'>
 /// Clock (UTC ms since Jan 1 1970).
 /// </param>
 /// <param name='zone'>
 /// Zone.
 /// </param>
 public static ZTime TimeOf(long clock, ZTimeZone zone = null)
 {
     zone = zone ?? ZTimeZone.Local;
     return(TimeOf(new ZDateTime(clock, zone)));
 }
Example #13
0
 /// <summary>
 /// Create time zone from .NET timezone
 /// </summary>
 /// <param name='zone'>
 /// Zone.
 /// </param>
 public ZTimeOfDay(ZTimeZone zone)
 {
     _zone        = zone;
     _zone_offset = (long)zone.BaseUtcOffset.TotalMilliseconds;
 }
Example #14
0
 /// <summary>
 /// Create a date in given timezone given UTC clock value
 /// </summary>
 /// <param name='clock'>
 /// # of ms since Jan 1 1970
 /// </param>
 /// <param name='zone'>
 /// Zone to present in
 /// </param>
 public ZDateTime(long clock, ZTimeZone zone)
 {
     _utc   = clock;
     _zone  = zone;
     _parts = 0L;
 }
Example #15
0
        private static ZDateTime Parse(string date, ZTimeZone zone)
        {
            DateParser parser = DateParser.DefaultParser;

            return(parser.Parse(date, zone));
        }
Example #16
0
        /// <summary>
        /// provide date as excel gmt form (2007-01-04 23:11:02.340).
        /// </summary>
        public string ToExcelDateTime(ZTimeZone zone)
        {
            var ztime = new ZDateTime(Clock, zone);

            return(ztime.ToExcelDateTime());
        }