public void DateTimeToUnixTimestamp_NonDST_ExpectedResult() { var nonDst = new DateTime(2016, 3, 1, 11, 0, 0, DateTimeKind.Utc); const int expected = 1456830000; Assert.AreEqual(UnixTimeConverter.DateTimeToUnixTimestamp(nonDst), expected); Assert.AreEqual(UnixTimeConverter.DateTimeToUnixTimestamp(nonDst.ToLocalTime()), expected); }
public void DateTimeToUnixTimestamp_DST_ExpectedResult() { var dst = new DateTime(2016, 4, 4, 10, 0, 0, DateTimeKind.Utc); const int expected = 1459764000; Assert.AreEqual(UnixTimeConverter.DateTimeToUnixTimestamp(dst), expected); Assert.AreEqual(UnixTimeConverter.DateTimeToUnixTimestamp(dst.ToLocalTime()), expected); }
public void DateTimeToUnixTimestamp_Zero_ExpectedResult() { var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); const int expected = 0; Assert.AreEqual(UnixTimeConverter.DateTimeToUnixTimestamp(epoch), expected); Assert.AreEqual(UnixTimeConverter.DateTimeToUnixTimestamp(epoch.ToLocalTime()), expected); }
protected override QueryStringParametersList GetQueryStringParameters() { if (Location == null) { throw new ArgumentException("Location is required"); } if (TimeStamp == null) { throw new ArgumentException("TimeStamp is required"); } var parameters = base.GetQueryStringParameters(); parameters.Add("location", this.Location.LocationString); parameters.Add("timestamp", UnixTimeConverter.DateTimeToUnixTimestamp(this.TimeStamp).ToString()); if (!string.IsNullOrWhiteSpace(Language)) { parameters.Add("language", Language); } return(parameters); }
/// <summary> /// Selects daily weather forecast for location's estimated date. /// </summary> /// <param name="location">Location</param> /// <param name="forecast">Forecasts for location</param> /// <returns>Forecast for the day</returns> public ForecastDailyEntry SelectLongTermEntry(LocationDetail location, ForecastLongTerm forecast) { if (location == null) { throw new ArgumentNullException("Location is null"); } if (forecast == null) { throw new ArgumentNullException("Forecast is null"); } var entry = forecast.Entries.FirstOrDefault(x => x.ForecastTime.Date == location.Time.Date); if (entry == null) { string msg = "No forecast for the day selected"; log.Error(msg); int hrs = 12; int min = 0; var lastEntry = forecast.Entries.LastOrDefault(); if (lastEntry != null) { hrs = lastEntry.ForecastTime.Hour; min = lastEntry.ForecastTime.Minute; } return(new ForecastDailyEntry() { Clouds = 0, Deg = 0, Humidity = 0, Pressure = 0, Rain = 0, Speed = 0, Temp = new Temp() { Day = 0, Eve = 0, Max = 0, Min = 0, Morn = 0, Night = 0 }, UnixTimestamp = (int)UnixTimeConverter.DateTimeToUnixTimestamp( new DateTime() .AddYears(location.Time.Year) .AddMonths(location.Time.Month) .AddDays(location.Time.Day) .AddHours(hrs) .AddMinutes(min)), WeatherDescription = new List <WeatherDescription>() { new WeatherDescription() { Description = "N/A", Icon = "N/A", Id = 0, Main = "N/A" } } }); //throw new NullReferenceException(msg); } return(entry); }
protected override QueryStringParametersList GetQueryStringParameters() { if (string.IsNullOrWhiteSpace(Origin)) { throw new ArgumentException("Must specify an Origin"); } if (string.IsNullOrWhiteSpace(Destination)) { throw new ArgumentException("Must specify a Destination"); } if (!Enum.IsDefined(typeof(AvoidWay), Avoid)) { throw new ArgumentException("Invalid enumeration value for 'Avoid'"); } if (!Enum.IsDefined(typeof(TravelMode), TravelMode)) { throw new ArgumentException("Invalid enumeration value for 'TravelMode'"); } if (TravelMode == TravelMode.Transit && (DepartureTime == default(DateTime) && ArrivalTime == default(DateTime))) { throw new ArgumentException("You must set either DepatureTime or ArrivalTime when TravelMode = Transit"); } var parameters = base.GetQueryStringParameters(); parameters.Add("origin", Origin); parameters.Add("destination", Destination); parameters.Add("mode", TravelMode.ToString().ToLower()); if (Alternatives) { parameters.Add("alternatives", "true"); } if (Avoid != AvoidWay.Nothing) { parameters.Add("avoid", Avoid.ToString().ToLower()); } if (!string.IsNullOrWhiteSpace(Language)) { parameters.Add("language", Language); } if (Waypoints != null && Waypoints.Any()) { IEnumerable <string> waypoints; if (OptimizeWaypoints) { const string optimizeWaypoints = "optimize:true"; waypoints = new string[] { optimizeWaypoints }.Concat(Waypoints); } else { waypoints = Waypoints; } parameters.Add("waypoints", string.Join("|", waypoints)); } if (ArrivalTime != default(DateTime)) { parameters.Add("arrival_time", UnixTimeConverter.DateTimeToUnixTimestamp(ArrivalTime).ToString(CultureInfo.InvariantCulture)); } if (DepartureTime != default(DateTime)) { parameters.Add("departure_time", UnixTimeConverter.DateTimeToUnixTimestamp(DepartureTime).ToString(CultureInfo.InvariantCulture)); } return(parameters); }
protected override QueryStringParametersList GetQueryStringParameters() { if (string.IsNullOrWhiteSpace(this.Origin)) { throw new ArgumentException("Must specify an Origin"); } if (string.IsNullOrWhiteSpace(this.Destination)) { throw new ArgumentException("Must specify a Destination"); } if (!Enum.IsDefined(typeof(AvoidWay), this.Avoid)) { throw new ArgumentException("Invalid enumeration value for 'Avoid'"); } if (!Enum.IsDefined(typeof(TravelMode), this.TravelMode)) { throw new ArgumentException("Invalid enumeration value for 'TravelMode'"); } if (this.TravelMode == TravelMode.TRANSIT && this.DepartureTime == default(DateTime) && this.ArrivalTime == default(DateTime)) { throw new ArgumentException("You must set either DepatureTime or ArrivalTime when TravelMode = Transit"); } var _parameters = base.GetQueryStringParameters(); _parameters.Add("origin", this.Origin); _parameters.Add("destination", this.Destination); _parameters.Add("units", this.Units.ToString().ToLower()); _parameters.Add("mode", this.TransitMode.ToString().ToLower()); if (this.Region != null) { _parameters.Add("region", this.Region); } if (this.Alternatives) { _parameters.Add("alternatives", "true"); } if (this.Avoid != AvoidWay.NOTHING) { _parameters.Add("avoid", this.Avoid.ToString('|')); } if (!string.IsNullOrWhiteSpace(this.Language)) { _parameters.Add("language", this.Language); } if (this.Waypoints != null && this.Waypoints.Any()) { _parameters.Add("waypoints", string.Join("|", this.OptimizeWaypoints ? new[] { "optimize:true" }.Concat(Waypoints) : this.Waypoints)); } if (this.TravelMode == TravelMode.TRANSIT) { _parameters.Add("transit_mode", this.TransitMode.ToString('|')); if (this.TransitRoutingPreference != TransitRoutingPreference.NOTHING) { _parameters.Add("transit_routing_preference", this.TransitRoutingPreference.ToString('|')); } if (this.ArrivalTime != default(DateTime)) { _parameters.Add("arrival_time", UnixTimeConverter.DateTimeToUnixTimestamp(this.ArrivalTime).ToString(CultureInfo.InvariantCulture)); } if (this.DepartureTime != default(DateTime)) { _parameters.Add("departure_time", UnixTimeConverter.DateTimeToUnixTimestamp(this.DepartureTime).ToString(CultureInfo.InvariantCulture)); } } return(_parameters); }
public override string ToString() { return(IsNow ? "now" : UnixTimeConverter.DateTimeToUnixTimestamp(Value).ToString(CultureInfo.InvariantCulture)); }