Click here to Skip to main content
13,625,824 members
Click here to Skip to main content

Stats

37.3K views
1.3K downloads
45 bookmarked
Posted 16 Jan 2012
Licenced CPOL

Gett.NET - A library for Ge.tt Web Services

, 18 Jan 2016
Use Ge.tt Web Services for real-time file publishing and sharing.
Gett.NET.dll
Gett.NET.pdb
GettTest.exe
GettTest.pdb
Newtonsoft.Json.dll
websocket-sharp.dll
Gett.NET
Gett.NET.suo
Gett.NET.v11.suo
Gett.NET
bin
Json.Net
Properties
WebSocket
GettTest
bin
Debug
obj
x86
Debug
Properties
.gitattributes
.gitignore
GettNETStrongName.snk
Properties
GettTest
Properties
LICENSE
packages
Newtonsoft.Json.8.0.2
lib
net40
Newtonsoft.Json.dll
Newtonsoft.Json.8.0.2.nupkg
tools
WebSocketSharp.1.0.3-rc9
lib
websocket-sharp.dll
WebSocketSharp.1.0.3-rc9.nupkg
#region License information
/*

  Copyright (c) 2016 Togocoder (http://www.codeproject.com/Members/Kim-Togo)
 
  This file is part of Gett.NET library that uses the Ge.tt REST API, http://ge.tt/developers

  Gett.NET is a free library: you can redistribute it and/or modify as nessery
  it under the terms of The Code Project Open License (CPOL) as published by
  the The Code Project, either version 1.02 of the License, or (at your option) any later version.

  Gett.NET is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY OF ANY KIND. The software is provided "as-is".
 
  Please read the The Code Project Open License (CPOL) at http://www.codeproject.com/info/cpol10.aspx

  I would appreciate getting an email, if you choose to use this library in your own work.
  Send an email to togocoder(at)live.com with a little description of your work, thanks! :-)

  ---
  This class handles converting an Unix timestamp to DateTime class.
  It is written by Christophe Geers on September 25, 2011. (About http://cgeers.com/about/)
  
  Please see http://cgeers.com/2011/09/25/writing-a-custom-json-net-datetime-converter/
  for the orginal source code.
  
  It is a JsonConverter type for Json.NET.
*/
#endregion

using System;

// ReSharper disable once CheckNamespace
namespace Newtonsoft.Json.Converters
{
    /// <summary>
    /// Convert Unix time to and from DateTime.
    /// </summary>
    public class UnixDateTimeConverter : DateTimeConverterBase
    {
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType != JsonToken.Integer)
            {
                throw new Exception(string.Format("Unexpected token parsing date. Expected Integer, got {0}.", reader.TokenType));
            }

            var ticks = (long)reader.Value;
            var date = new DateTime(1970, 1, 1);
            date = date.AddSeconds(ticks);

            return date;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            long ticks;
            if (value is DateTime)
            {
                var epoc = new DateTime(1970, 1, 1);
                TimeSpan delta = ((DateTime)value) - epoc;
                if (delta.TotalSeconds < 0)
                {
                    // ReSharper disable once NotResolvedInText
                    throw new ArgumentOutOfRangeException("Unix epoc starts January 1st, 1970");
                }

                ticks = (long)delta.TotalSeconds;
            }
            else
            {
                throw new Exception("Expected date object value.");
            }

            writer.WriteValue(ticks);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kim Togo
Software Developer Miralix
Denmark Denmark
Has worked as a programmer since 1999, starting with C++/MFC, Java, PHP and MySQL. Now it is all about C# (my favorite programming language), MS SQL, Azure and Xamarin (iOS/Android/WP8)

My primary work is to create applications that interacts with PABC/PBX, Microsoft Lync / UCMA.

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180712.1 | Last Updated 19 Jan 2016
Article Copyright 2012 by Kim Togo
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid