add basic create and list reminder api
This commit is contained in:
58
Budet-cho-bot/Helpers/Utility.cs
Normal file
58
Budet-cho-bot/Helpers/Utility.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user