public void CurrentTimeAtBeginningOfTimeStep(int remainingSeconds)
        {
            $"Given a time step of {ThirtySeconds} seconds"
            .x(() => {});

            "And a current UTC Time at the beginning of a time step"
            .x(() => DateTime.SetupGet(x => x.UtcNow)
               .Returns(UnixEpoch.AddSeconds(ThirtySeconds * Faker.Random.Int(1, 5000))));

            "When I calculate the remaining seconds in the current time interval"
            .x(() => remainingSeconds = TestInstance.RemainingSecondsInCurrentInterval());

            $"Then the remaining seconds should be {ThirtySeconds}"
            .x(() => remainingSeconds.ShouldBe(ThirtySeconds));
        }
Exemplo n.º 2
0
        internal static ApiLimit ParseRateLimit(IDictionary <string, string> header, string prefix)
        {
            var limitCount  = (int?)ParseHeaderValue(header, prefix + "Limit");
            var limitRemain = (int?)ParseHeaderValue(header, prefix + "Remaining");
            var limitReset  = ParseHeaderValue(header, prefix + "Reset");

            if (limitCount == null || limitRemain == null || limitReset == null)
            {
                return(null);
            }

            var limitResetDate = UnixEpoch.AddSeconds(limitReset.Value).ToLocalTime();

            return(new ApiLimit(limitCount.Value, limitRemain.Value, limitResetDate));
        }
        /// <summary>
        /// Converts the string representation of a <see cref="input"/> Unix
        /// time value to a <see cref="DateTime"/> value.
        /// </summary>
        /// <param name="input">
        /// The object to convert.
        /// </param>
        /// <returns>
        /// Result of the conversion.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// The <see cref="input"/> does not represent a <see cref="DateTime"/>
        /// value.
        /// </exception>
        public override DateTime Convert(object input)
        {
            var x = input as DateTime?;

            if (x != null)
            {
                return(x.Value);
            }

            Int64 value;

            if (Int64.TryParse(input.ToString(), result: out value))
            {
                var dateTime = UnixEpoch.AddSeconds(value).ToLocalTime();
                return(dateTime);
            }

            throw new InvalidDataException(string.Format("Expected {0}: {1}", TypeName, input));  // TODO: improved diagnostices
        }
        /// <summary>
        /// Converts the string representation of a <paramref name="input"/> Unix
        /// time value to a <see cref="DateTime"/> value.
        /// </summary>
        /// <param name="input">
        /// The object to convert.
        /// </param>
        /// <returns>
        /// Result of the conversion.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// The <paramref name="input"/> does not represent a <see cref="DateTime"/>
        /// value.
        /// </exception>
        public override DateTime Convert(object input)
        {
            var x = input as DateTime?;

            if (x != null)
            {
                return(x.Value);
            }

            Double value;

            if (Double.TryParse(input.ToString(), result: out value))
            {
                var dateTime = UnixEpoch.AddSeconds(value).ToLocalTime();
                return(dateTime);
            }

            throw NewInvalidDataException(input);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Converts a UNIX time value to a <see cref="DateTime"/> value
 /// </summary>
 public static DateTime FromUnixTime(uint unixTime)
 {
     return(UnixEpoch.AddSeconds(unixTime));
 }
Exemplo n.º 6
0
 public static DateTime FromUnixTime(this long timestamp)
 {
     return(UnixEpoch.AddSeconds(timestamp));
 }
Exemplo n.º 7
0
 public static DateTime FromUnixTime(this long self)
 {
     return(UnixEpoch.AddSeconds(self).ToLocalTime());
 }
Exemplo n.º 8
0
        //-----------------------------------------------------------------------------------------------------------------------------
        // UTC Time
        //-----------------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// 지정된 초 수를 더한 새로운 (UTC) System.DateTime을 반환합니다.
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public static DateTime ConvertToUtcTime(Int64 seconds)
        {
            return(UnixEpoch.AddSeconds(seconds).ToUniversalTime());
        }
Exemplo n.º 9
0
 /// <summary>
 /// Unix 時間 [s] から ローカライズされた DateTime を返す.
 /// </summary>
 public static DateTime GetLocalizedDateTime(ulong time)
 {
     return(TimeZone.CurrentTimeZone.ToLocalTime(UnixEpoch.AddSeconds(time)));
 }
Exemplo n.º 10
0
        void LoadInternal(BEncodedDictionary torrentInformation, InfoHash infoHash)
        {
            Check.TorrentInformation(torrentInformation);
            AnnounceUrls = new List <IList <string> > ().AsReadOnly();

            foreach (KeyValuePair <BEncodedString, BEncodedValue> keypair in torrentInformation)
            {
                switch (keypair.Key.Text)
                {
                case ("announce"):
                    // Ignore this if we have an announce-list
                    if (torrentInformation.ContainsKey("announce-list"))
                    {
                        break;
                    }
                    AnnounceUrls = new List <IList <string> > {
                        new List <string> {
                            keypair.Value.ToString()
                        }.AsReadOnly()
                    }.AsReadOnly();
                    break;

                case ("creation date"):
                    try {
                        try {
                            CreationDate = UnixEpoch.AddSeconds(long.Parse(keypair.Value.ToString()));
                        } catch (Exception e) {
                            if (e is ArgumentOutOfRangeException)
                            {
                                CreationDate = UnixEpoch.AddMilliseconds(long.Parse(keypair.Value.ToString()));
                            }
                            else
                            {
                                throw;
                            }
                        }
                    } catch (Exception e) {
                        if (e is ArgumentOutOfRangeException)
                        {
                            throw new BEncodingException("Argument out of range exception when adding seconds to creation date.", e);
                        }
                        else if (e is FormatException)
                        {
                            throw new BEncodingException($"Could not parse {keypair.Value} into a number", e);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    break;

                case ("nodes"):
                    if (keypair.Value is BEncodedList list)
                    {
                        Nodes = list;
                    }
                    break;

                case ("comment.utf-8"):
                    if (keypair.Value.ToString().Length != 0)
                    {
                        Comment = keypair.Value.ToString();            // Always take the UTF-8 version
                    }
                    break;                                             // even if there's an existing value

                case ("comment"):
                    if (string.IsNullOrEmpty(Comment))
                    {
                        Comment = keypair.Value.ToString();
                    }
                    break;

                case ("publisher-url.utf-8"):                          // Always take the UTF-8 version
                    PublisherUrl = keypair.Value.ToString();           // even if there's an existing value
                    break;

                case ("publisher-url"):
                    if (string.IsNullOrEmpty(PublisherUrl))
                    {
                        PublisherUrl = keypair.Value.ToString();
                    }
                    break;

                case ("created by"):
                    CreatedBy = keypair.Value.ToString();
                    break;

                case ("encoding"):
                    Encoding = keypair.Value.ToString();
                    break;

                case ("info"):
                    InfoHash = infoHash;
                    ProcessInfo(((BEncodedDictionary)keypair.Value));
                    break;

                case ("name"):                                                   // Handled elsewhere
                    break;

                case ("announce-list"):
                    if (keypair.Value is BEncodedString)
                    {
                        break;
                    }

                    var result    = new List <IList <string> > ();
                    var announces = (BEncodedList)keypair.Value;
                    for (int j = 0; j < announces.Count; j++)
                    {
                        if (announces[j] is BEncodedList bencodedTier)
                        {
                            var tier = new List <string> (bencodedTier.Count);

                            for (int k = 0; k < bencodedTier.Count; k++)
                            {
                                tier.Add(bencodedTier[k].ToString());
                            }

                            Toolbox.Randomize(tier);

                            var resultTier = new List <string> ();
                            for (int k = 0; k < tier.Count; k++)
                            {
                                resultTier.Add(tier[k]);
                            }

                            if (resultTier.Count != 0)
                            {
                                result.Add(tier.AsReadOnly());
                            }
                        }
                        else
                        {
                            throw new BEncodingException(
                                      $"Non-BEncodedList found in announce-list (found {announces[j].GetType ()})");
                        }
                    }
                    if (result.Count > 0)
                    {
                        AnnounceUrls = result.AsReadOnly();
                    }
                    break;

                case ("httpseeds"):
                    // This form of web-seeding is not supported.
                    break;

                case ("url-list"):
                    if (keypair.Value is BEncodedString httpSeedString)
                    {
                        HttpSeeds.Add(httpSeedString.Text);
                    }
                    else if (keypair.Value is BEncodedList httpSeedList)
                    {
                        foreach (BEncodedString str in httpSeedList)
                        {
                            HttpSeeds.Add(str.Text);
                        }
                    }
                    break;

                default:
                    break;
                }
            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// Converts <paramref name="unixTimestamp"/> to DateTime instance.
 /// </summary>
 /// <remarks>Unix timestamp represents number of seconds passed since 1.1.1970. 00:00.</remarks>
 /// <remarks>From .NET Framework v4.6 there is built-in support for this conversion: DateTimeOffset.FromUnixTimeSeconds.</remarks>
 /// <param name="unixTimestamp">Unix timestamp.</param>
 /// <returns>DateTime instance created from <paramref name="unixTimestamp"/>.</returns>
 public static DateTime FromUnixTimestamp(this long unixTimestamp)
 => UnixEpoch.AddSeconds(unixTimestamp);
Exemplo n.º 12
0
 /// <summary>
 /// Helper method which converts a Unix timestamp to a DateTime
 /// </summary>
 /// <param name="timestamp"></param>
 /// <returns></returns>
 internal static DateTime ConvertFromUnixTimestamp(double timestamp)
 {
     return(UnixEpoch.AddSeconds(timestamp));
 }