1
0

add basic create and list reminder api

This commit is contained in:
2023-08-03 13:57:12 +07:00
parent e5693edc49
commit 4d16a67a2b
8 changed files with 190 additions and 246 deletions

View 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();
}
}