1
0
This repository has been archived on 2023-08-03. You can view files and clone it, but cannot push or open issues or pull requests.
budet-cho-bot/Budet-cho-bot/Helpers/Utility.cs

58 lines
1.7 KiB
C#

using Budet_cho_bot.Models;
using Newtonsoft.Json.Linq;
using System.Linq;
namespace Budet_cho_bot.Helpers;
public static class Utility
{
public static DateTime ParseDate(string message)
{
if (TimeSpan.TryParse(message, out var parsedTime) == false)
{
throw new ArgumentException("Хуйня твоё время");
}
var today = DateTime.Today; //returns xx/xx/xxxx 00:00:00, so adding time span would set correct daate
var date = today + parsedTime;
return date;
}
public static TimeSpan DeserializeTime(string token)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<TimeSpan>(token);
}
public static string PackTimetableIntoString(CronRecord[] timetable)
{
return string.Join(';', timetable.Select(x =>
{
var unixRunTime = x.LastRun.ToUniversalTime().ToUnixTimeSeconds();
var secondsPeriod = (int)(x.Period.TotalSeconds);
return $"{unixRunTime}:{secondsPeriod}:{x.IsOneShot}";
}));
}
public static CronRecord[] UnpackTimetableFromString(string timetable)
{
if(timetable == null)
{
return new CronRecord[0];
}
return timetable.Split(';').Select(x =>
{
var record = x.Split(':'); //crude but whatever
var lastRun = DateTimeOffset.FromUnixTimeSeconds(long.Parse(record[0]));
var period = TimeSpan.FromSeconds(long.Parse(record[1]));
bool oneShot = bool.Parse(record[2]);
return new CronRecord()
{
LastRun = lastRun,
Period = period,
IsOneShot = oneShot
};
}).ToArray();
}
}