Converts a DateTime object into a unix timestamp number.
using System;
using System.IO;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
namespace FlickrNet
{
/// <summary>
/// Internal class providing certain utility functions to other classes.
/// </summary>
internal sealed class Utils
{
private static readonly DateTime unixStartDate = new DateTime(1970, 1, 1, 0, 0, 0);
/// <summary>
/// Converts a <see cref="DateTime"/> object into a unix timestamp number.
/// </summary>
/// <param name="date">The date to convert.</param>
/// <returns>A long for the number of seconds since 1st January 1970, as per unix specification.</returns>
internal static long DateToUnixTimestamp(DateTime date)
{
TimeSpan ts = date - unixStartDate;
return (long)ts.TotalSeconds;
}
/// <summary>
/// Converts a string, representing a unix timestamp number into a <see cref="DateTime"/> object.
/// </summary>
/// <param name="timestamp">The timestamp, as a string.</param>
/// <returns>The <see cref="DateTime"/> object the time represents.</returns>
internal static DateTime UnixTimestampToDate(string timestamp)
{
if (timestamp == null || timestamp.Length == 0) return DateTime.MinValue;
return UnixTimestampToDate(long.Parse(timestamp));
}
/// <summary>
/// Converts a <see cref="long"/>, representing a unix timestamp number into a <see cref="DateTime"/> object.
/// </summary>
/// <param name="timestamp">The unix timestamp.</param>
/// <returns>The <see cref="DateTime"/> object the time represents.</returns>
internal static DateTime UnixTimestampToDate(long timestamp)
{
return unixStartDate.AddSeconds(timestamp);
}
}
}
Related examples in the same category