public void MinusOperator()
        {
            SocialDateTime a = new SocialDateTime(2016, 3, 4, 21, 0, 0);
            SocialDateTime b = new SocialDateTime(2016, 3, 3, 21, 0, 0);

            Assert.AreEqual(24, (a - b).TotalHours);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            switch (reader.TokenType)
            {
            case JsonToken.Null:
                return(null);

            case JsonToken.Integer:
                return(SocialDateTime.FromUnixTimestamp((long)reader.Value));

            case JsonToken.Float:
                return(SocialDateTime.FromUnixTimestamp((double)reader.Value));

            case JsonToken.String:
                Double value;
                if (Double.TryParse(reader.Value + "", out value))
                {
                    return(SocialDateTime.FromUnixTimestamp(value));
                }
                throw new JsonSerializationException("String value doesn't match a Unix timestamp: " + reader.Value);

            default:
                throw new JsonSerializationException("Unexpected token type: " + reader.TokenType);
            }
        }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance from the specified <code>date</code>.
 /// </summary>
 /// <param name="date">An instance of <see cref="SocialDateTime"/> representing the full date.</param>
 public SocialPartialDate(SocialDateTime date)
 {
     Year     = date == null ? 0 : date.Year;
     Month    = date == null ? 0 : date.Month;
     Day      = date == null ? 0 : date.Day;
     DateTime = new DateTime(Year == 0 ? 1 : Year, Month == 0 ? 1 : Month, Day == 0 ? 1 : Day);
 }
        public void MinusOperator() {

            SocialDateTime a = new SocialDateTime(2016, 3, 4, 21, 0, 0);
            SocialDateTime b = new SocialDateTime(2016, 3, 3, 21, 0, 0);

            Assert.AreEqual(24, (a - b).TotalHours);

        }
        public void InequalityOperator()
        {
            SocialDateTime a = new SocialDateTime(2016, 3, 4, 21, 0, 0);
            SocialDateTime b = new SocialDateTime(2016, 3, 3, 21, 0, 0);
            SocialDateTime c = new SocialDateTime(2016, 3, 4, 21, 0, 0);

            Assert.AreEqual(true, a != b);
            Assert.AreEqual(false, a != c);
        }
Exemple #6
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (!(value is DateTime))
            {
                return;
            }
            SocialDateTime dt = (DateTime)value;

            writer.WriteValue(dt.UnixTimestamp);
        }
        public void InequalityOperator() {

            SocialDateTime a = new SocialDateTime(2016, 3, 4, 21, 0, 0);
            SocialDateTime b = new SocialDateTime(2016, 3, 3, 21, 0, 0);
            SocialDateTime c = new SocialDateTime(2016, 3, 4, 21, 0, 0);

            Assert.AreEqual(true, a != b);
            Assert.AreEqual(false, a != c);

        }
Exemple #8
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (!(value is SocialDateTime))
            {
                return;
            }
            SocialDateTime dt = (SocialDateTime)value;

            base.WriteJson(writer, dt.DateTime, serializer);
        }
        public void PlusTimeSpanOperator()
        {
            SocialDateTime a   = new SocialDateTime(2016, 3, 4, 21, 0, 0);
            SocialDateTime b   = new SocialDateTime(2016, 3, 5, 21, 0, 0);
            TimeSpan       ts1 = TimeSpan.FromHours(23);
            TimeSpan       ts2 = TimeSpan.FromHours(24);
            TimeSpan       ts3 = TimeSpan.FromHours(25);

            Assert.AreEqual(false, (a + ts1).DateTime == b.DateTime);
            Assert.AreEqual(true, (a + ts2) == b.DateTime);
            Assert.AreEqual(false, (a + ts3) == b.DateTime);
        }
        public void MinusTimeSpanOperator() {

            SocialDateTime a = new SocialDateTime(2016, 3, 4, 21, 0, 0);
            SocialDateTime b = new SocialDateTime(2016, 3, 3, 21, 0, 0);
            TimeSpan ts1 = TimeSpan.FromHours(23);
            TimeSpan ts2 = TimeSpan.FromHours(24);
            TimeSpan ts3 = TimeSpan.FromHours(25);

            Assert.AreEqual(false, (a - ts1).DateTime == b.DateTime);
            Assert.AreEqual(true, (a - ts2) == b.DateTime);
            Assert.AreEqual(false, (a - ts3) == b.DateTime);

        }
        /// <summary>
        /// Initialize a new instance based on the specified <code>timestamp</code>.
        /// </summary>
        /// <param name="timestamp">A timestamp.</param>
        public SocialDateWeek(SocialDateTime timestamp) {

            Week = SocialUtils.Time.GetIso8601WeekNumber(timestamp);
            Start = SocialUtils.Time.GetFirstDayOfWeek(timestamp.DateTime);
            End = SocialUtils.Time.GetLastDayOfWeek(timestamp.DateTime);

            if (End.Month == 1 && Week == 1) {
                Year = End.Year;
            } else if (Start.Month == 12 && Week >= 50) {
                Year = Start.Year;
            } else {
                Year = timestamp.Year;
            }

        }
        /// <summary>
        /// Initialize a new instance based on the specified <code>timestamp</code>.
        /// </summary>
        /// <param name="timestamp">A timestamp.</param>
        public SocialDateWeek(SocialDateTime timestamp)
        {
            Week  = SocialUtils.Time.GetIso8601WeekNumber(timestamp);
            Start = SocialUtils.Time.GetFirstDayOfWeek(timestamp.DateTime);
            End   = SocialUtils.Time.GetLastDayOfWeek(timestamp.DateTime);

            if (End.Month == 1 && Week == 1)
            {
                Year = End.Year;
            }
            else if (Start.Month == 12 && Week >= 50)
            {
                Year = Start.Year;
            }
            else
            {
                Year = timestamp.Year;
            }
        }
        public void LessThanOperator()
        {
            // Notice: this unit test will proably fail if run exactly at midnight

            // ReSharper disable ExpressionIsAlwaysNull
            // ReSharper disable EqualExpressionComparison

            SocialDateTime nada  = null;
            SocialDateTime now   = SocialDateTime.Now;
            SocialDateTime today = SocialDateTime.Today;

            Assert.AreEqual(false, now < nada);
            Assert.AreEqual(true, nada < now);
            Assert.AreEqual(true, today < now);
            Assert.AreEqual(false, now < today);
            Assert.AreEqual(false, nada < nada);

            // ReSharper restore ExpressionIsAlwaysNull
            // ReSharper restore EqualExpressionComparison
        }
        public void GreaterThanOperator()
        {
            // Notice: this unit test will proably fail if run exactly at midnight

            // ReSharper disable ExpressionIsAlwaysNull
            // ReSharper disable EqualExpressionComparison

            SocialDateTime nada  = null;
            SocialDateTime now   = SocialDateTime.Now;
            SocialDateTime today = SocialDateTime.Today;

            Assert.AreEqual(true, now > nada, "#1");
            Assert.AreEqual(false, nada > now, "#2");
            Assert.AreEqual(false, today > now, "#3");
            Assert.AreEqual(true, now > today, "#4");
            Assert.AreEqual(false, nada > nada, "#5");

            // ReSharper restore ExpressionIsAlwaysNull
            // ReSharper restore EqualExpressionComparison
        }
 /// <summary>
 /// Initializes a new instance from the specified <code>timestamp</code>.
 /// </summary>
 /// <param name="timestamp">A timestamp represented by an instance of <see cref="SocialDateTime"/>.</param>
 public SocialDateYear(SocialDateTime timestamp) {
     if (timestamp == null) throw new ArgumentNullException("timestamp");
     Year = timestamp.Year;
 }
Exemple #16
0
 /// <summary>
 /// Gets the week number of <code>date</code> according to the ISO8601 specification.
 /// </summary>
 /// <param name="date">The date.</param>
 /// <returns>Returns the week number.</returns>
 public static int GetIso8601WeekNumber(SocialDateTime date) {
     return GetIso8601WeekNumber(date.DateTime);
 }
 /// <summary>
 /// Initializes a new instance from the specified <code>date</code>.
 /// </summary>
 /// <param name="date">An instance of <see cref="SocialDateTime"/> representing the full date.</param>
 public SocialPartialDate(SocialDateTime date) {
     Year = date == null ? 0 : date.Year;
     Month = date == null ? 0 : date.Month;
     Day = date == null ? 0 : date.Day;
     DateTime = new DateTime(Year == 0 ? 1 : Year, Month == 0 ? 1 : Month, Day == 0 ? 1 : Day);
 }
 /// <summary>
 /// Gets the week number of <code>date</code> according to the ISO8601 specification.
 /// </summary>
 /// <param name="date">The date.</param>
 /// <returns>Returns the week number.</returns>
 public static int GetIso8601WeekNumber(SocialDateTime date)
 {
     return(GetIso8601WeekNumber(date.DateTime));
 }