using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SweetLib.Utils
{
///
/// A generic class containing useful methods.
///
public static class SweetUtils
{
public static char DefaultFileNameReplaceChar { get; set; } = '-';
///
/// Legalizes a file name with the character.
///
/// File name to legalize.
/// Legalized file name.
public static string LegalizeFilename(string fileName)
{
return LegalizeFilename(fileName, '-');
}
///
/// Legalizes a file name by a given replace character.
///
/// File name to legalize.
/// Character to be used as replace character.
/// Legalized file name.
public static string LegalizeFilename(string fileName, char replaceChar)
{
var invalidChars = System.IO.Path.GetInvalidFileNameChars();
if (invalidChars.Contains(replaceChar))
throw new IOException($"Replace character {replaceChar} is an invalid file name character.");
return invalidChars.Aggregate(fileName, (current, c) => current.Replace(c, replaceChar));
}
///
/// Converts a into an Unix timestamp.
///
/// to convert into Unix timestamp.
/// Converted Unix timestamp.
public static double DateTimeToUnixTimeStamp(DateTime date)
{
return date.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
}
///
/// Converts an Unix timestamp into a .
///
/// Unix timestamp to convert.
/// Converted .
public static DateTime UnixTimestampToDateTime(double timestamp)
{
return new DateTime(1970, 1, 1).AddMilliseconds(timestamp);
}
}
}