Skip to content

Instantly share code, notes, and snippets.

@germ13
Last active January 16, 2019 20:50
Show Gist options
  • Select an option

  • Save germ13/f38fba8fc95b5a295221115b73da67be to your computer and use it in GitHub Desktop.

Select an option

Save germ13/f38fba8fc95b5a295221115b73da67be to your computer and use it in GitHub Desktop.
FiscalDate
using System;
using System.Text;
namespace FiscalTime
{
internal class FiscalDate
{
public FiscalDate()
{
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("{");
sb.AppendLine($"\tDate: {PhysicalDate}");
sb.AppendLine($"\tYear: {Year}");
sb.AppendLine($"\tQuarter: {Quarter}");
sb.AppendLine($"\tMonth: {Month}");
sb.AppendLine($"\tDayOfWeek: {DayOfWeekNumber}");
sb.AppendLine($"\tIsBeginningOfYear: {IsBeginningOfYear}");
sb.AppendLine($"\tIsBeginningOfQuarter: {IsBeginningOfQuarter}");
sb.AppendLine($"\tIsBeginningOfMonth: {IsBeginningOfMonth}");
sb.AppendLine($"\tIsBeginningOfWeek: {IsBeginningOfWeek}");
sb.AppendLine($"\tIsHoliday: {IsHoliday}");
sb.AppendLine("}");
return sb.ToString();
//return new JavaScriptSerializer().Serialize(this);
}
public DateTime PhysicalDate { get; set; }
public int Year { get; set; }
public int Quarter { get; set; }
public int Month { get; set; }
public int DayOfWeekNumber
{
get
{
if (IsBeginningOfWeek && IsHoliday)
{
return 2;
}
else if (IsBeginningOfWeek && !IsHoliday)
{
return 1;
}
else
{
return 3;
}
}
set { }
}
public bool IsBeginningOfYear { get; set; } = false;
public bool IsBeginningOfQuarter { get; set; } = false;
public bool IsBeginningOfMonth { get; set; } = false;
public bool IsBeginningOfWeek { get; set; } = false;
public bool IsHoliday { get; set; } = false;
public bool IsWorkingDay
{
get { return !IsHoliday; }
set { }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace FiscalTime
{
internal class FiscalPeriod
{
public SortedDictionary<DateTime, FiscalDate> Period
{
get;
set;
}
public DateTime Start
{
get => Period.First().Key;
}
public DateTime End
{
get => Period.Last().Key;
}
internal void Seed(SortedDictionary<DateTime, FiscalDate> dates)
{
foreach (var day in dates)
{
// if it doesnt exist: then add, otherwise update.
if (!Period.ContainsKey(day.Key))
{
Period.Add(day.Key, day.Value);
}
else
{
Period[day.Key] = day.Value;
}
}
}
internal void Process(DateTime? startingAt)
{
if (startingAt == null) { startingAt = Start; }
}
internal void Display()
{
Console.BackgroundColor = ConsoleColor.DarkGray; Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("-- -- --: BEGINNING :-- -- --"); Console.ResetColor();
foreach (var day in Period)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(day.Key);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(day.Value);
Console.WriteLine();
}
}
public FiscalPeriod()
{
Period = new SortedDictionary<DateTime, FiscalDate>();
}
}
}
using System;
namespace FiscalTime
{
internal class Program
{
private static void Main(string[] args)
{
var year = new FiscalPeriod();
year.Seed(SpecialDates.MonthStarts);
year.Seed(SpecialDates.QuarterStarts);
year.Seed(SpecialDates.Holidays);
Console.WriteLine(year.Start);
Console.WriteLine(year.End);
//year.Display();
Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("DONE!"); Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
namespace FiscalTime
{
internal static class SpecialDates
{
public static SortedDictionary<DateTime, FiscalDate> MonthStarts
{
get
{
return new SortedDictionary<DateTime, FiscalDate>(){
//TODO handle duplicates
// {
// new DateTime(2018, 12, 31),
// new FiscalDate()
// {
// PhysicalDate = new DateTime(2018, 12, 31),
// Year = 2019,
// Quarter = 01,
// Month = 01,
// IsBeginningOfYear = true,
// IsBeginningOfQuarter = true,
// IsBeginningOfMonth = true,
// IsBeginningOfWeek = true,
// }
// },
{
new DateTime(2019, 02, 04),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 02, 04),
Year = 2019,
Quarter = 01,
Month = 02,
IsBeginningOfQuarter = false,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
},
{
new DateTime(2019, 03, 04),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 03, 04),
Year = 2019,
Quarter = 01,
Month = 03,
IsBeginningOfQuarter = false,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
},
{
new DateTime(2019, 05, 06),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 05, 06),
Year = 2019,
Quarter = 02,
Month = 05,
IsBeginningOfQuarter = false,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
},
{
new DateTime(2019, 06, 03),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 06, 03),
Year = 2019,
Quarter = 02,
Month = 06,
IsBeginningOfQuarter = false,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
},
{
new DateTime(2019, 08, 05),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 08, 05),
Year = 2019,
Quarter = 03,
Month = 08,
IsBeginningOfQuarter = false,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
},
{
new DateTime(2019, 09, 02),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 09, 02),
Year = 2019,
Quarter = 03,
Month = 09,
IsBeginningOfQuarter = false,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
IsHoliday = true
}
},
{
new DateTime(2019, 11, 04),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 11, 04),
Year = 2019,
Quarter = 11,
Month = 04,
IsBeginningOfQuarter = false,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
},
{
new DateTime(2019, 12, 02),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 12, 02),
Year = 2019,
Quarter = 04,
Month = 12,
IsBeginningOfQuarter = false,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
}
};
}
}
public static SortedDictionary<DateTime, FiscalDate> QuarterStarts
{
get
{
return new SortedDictionary<DateTime, FiscalDate>(){
{
new DateTime(2018, 12, 31),
new FiscalDate()
{
PhysicalDate = new DateTime(2018, 12, 31),
Year = 2019,
Quarter = 01,
Month = 01,
IsBeginningOfYear = true,
IsBeginningOfQuarter = true,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
},
{
new DateTime(2019, 04, 01),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 04, 01),
Year = 2019,
Quarter = 02,
Month = 04,
IsBeginningOfQuarter = true,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
},
{
new DateTime(2019, 07, 01),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 07, 01),
Year = 2019,
Quarter = 03,
Month = 07,
IsBeginningOfQuarter = true,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
},
{
new DateTime(2019, 09, 30),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 09, 30),
Year = 2019,
Quarter = 4,
Month = 10,
IsBeginningOfQuarter = true,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
},
{
new DateTime(2019, 12, 30),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 12, 30),
Year = 2020,
Quarter = 01,
Month = 01,
IsBeginningOfYear = true,
IsBeginningOfQuarter = true,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
}
}
};
}
}
public static SortedDictionary<DateTime, FiscalDate> Holidays
{
get
{
return new SortedDictionary<DateTime, FiscalDate>(){
{
new DateTime(2019, 01, 01),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 01, 01),
Year = 2019,
Quarter = 01,
Month = 01,
IsHoliday = true
}
},
{
new DateTime(2019, 05, 27),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 05, 27),
Year = 2019,
Quarter = 02,
Month = 05,
IsBeginningOfWeek = true,
IsHoliday = true
}
},
{
new DateTime(2019, 07, 04),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 07, 04),
Year = 2019,
Quarter = 03,
Month = 07,
IsHoliday = true
}
},
{
new DateTime(2019, 09, 02),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 09, 02),
Year = 2019,
Quarter = 03,
Month = 09,
IsBeginningOfMonth = true,
IsBeginningOfWeek = true,
IsHoliday = true
}
},
{
new DateTime(2019, 11, 28),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 11, 28),
Year = 2019,
Quarter = 04,
Month = 11,
IsHoliday = true
}
},
{
new DateTime(2019, 11, 29),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 11, 29),
Year = 2019,
Quarter = 04,
Month = 11,
IsHoliday = true
}
},
{
new DateTime(2019, 12, 24),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 12, 24),
Year = 2019,
Quarter = 04,
Month = 12,
IsHoliday = true
}
},
{
new DateTime(2019, 12, 25),
new FiscalDate()
{
PhysicalDate = new DateTime(2019, 12, 25),
Year = 2019,
Quarter = 04,
Month = 12,
IsHoliday = true
}
},
};
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment