public void TestStrFTime() { Liquid.UseRubyDateFormat = true; DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat; Assert.AreEqual(dateTimeFormat.GetMonthName(5), StandardFilters.Date(DateTime.Parse("2006-05-05 10:00:00"), "%B")); Assert.AreEqual(dateTimeFormat.GetMonthName(6), StandardFilters.Date(DateTime.Parse("2006-06-05 10:00:00"), "%B")); Assert.AreEqual(dateTimeFormat.GetMonthName(7), StandardFilters.Date(DateTime.Parse("2006-07-05 10:00:00"), "%B")); Assert.AreEqual(dateTimeFormat.GetMonthName(5), StandardFilters.Date("2006-05-05 10:00:00", "%B")); Assert.AreEqual(dateTimeFormat.GetMonthName(6), StandardFilters.Date("2006-06-05 10:00:00", "%B")); Assert.AreEqual(dateTimeFormat.GetMonthName(7), StandardFilters.Date("2006-07-05 10:00:00", "%B")); Assert.AreEqual("05/07/2006 10:00:00", StandardFilters.Date("05/07/2006 10:00:00", string.Empty)); Assert.AreEqual("05/07/2006 10:00:00", StandardFilters.Date("05/07/2006 10:00:00", null)); Assert.AreEqual("07/05/2006", StandardFilters.Date("2006-07-05 10:00:00", "%m/%d/%Y")); Assert.AreEqual("07/16/2004", StandardFilters.Date("Fri Jul 16 2004 01:00:00", "%m/%d/%Y")); Assert.AreEqual(null, StandardFilters.Date(null, "%M")); Assert.AreEqual("hi", StandardFilters.Date("hi", "%M")); Template template = Template.Parse(@"{{ hi | date:""%M"" }}"); Assert.AreEqual("hi", template.Render(Hash.FromAnonymousObject(new { hi = "hi" }))); }
public void TestDate() { Liquid.UseRubyDateFormat = false; DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat; Assert.AreEqual(dateTimeFormat.GetMonthName(5), StandardFilters.Date(DateTime.Parse("2006-05-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(6), StandardFilters.Date(DateTime.Parse("2006-06-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(7), StandardFilters.Date(DateTime.Parse("2006-07-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(5), StandardFilters.Date("2006-05-05 10:00:00", "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(6), StandardFilters.Date("2006-06-05 10:00:00", "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(7), StandardFilters.Date("2006-07-05 10:00:00", "MMMM")); Assert.AreEqual("05/07/2006 10:00:00", StandardFilters.Date("05/07/2006 10:00:00", string.Empty)); Assert.AreEqual("05/07/2006 10:00:00", StandardFilters.Date("05/07/2006 10:00:00", null)); Assert.AreEqual(new DateTime(2006, 7, 5).ToString("MM/dd/yyyy"), StandardFilters.Date("2006-07-05 10:00:00", "MM/dd/yyyy")); Assert.AreEqual(new DateTime(2004, 7, 16).ToString("MM/dd/yyyy"), StandardFilters.Date("Fri Jul 16 2004 01:00:00", "MM/dd/yyyy")); Assert.AreEqual(null, StandardFilters.Date(null, "MMMM")); Assert.AreEqual("hi", StandardFilters.Date("hi", "MMMM")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("now", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("today", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("Now", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("Today", "MM/dd/yyyy")); Template template = Template.Parse(@"{{ hi | date:""MMMM"" }}"); Assert.AreEqual("hi", template.Render(Hash.FromAnonymousObject(new { hi = "hi" }))); }
public void TestDate() { Liquid.UseRubyDateFormat = false; DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat; Assert.AreEqual(dateTimeFormat.GetMonthName(5), StandardFilters.Date(DateTime.Parse("2006-05-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(6), StandardFilters.Date(DateTime.Parse("2006-06-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(7), StandardFilters.Date(DateTime.Parse("2006-07-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(5), StandardFilters.Date("2006-05-05 10:00:00", "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(6), StandardFilters.Date("2006-06-05 10:00:00", "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(7), StandardFilters.Date("2006-07-05 10:00:00", "MMMM")); Assert.AreEqual("08/01/2006 10:00:00", StandardFilters.Date("08/01/2006 10:00:00", string.Empty)); Assert.AreEqual("08/02/2006 10:00:00", StandardFilters.Date("08/02/2006 10:00:00", null)); Assert.AreEqual(new DateTime(2006, 8, 3, 10, 0, 0).ToString(), StandardFilters.Date(new DateTime(2006, 8, 3, 10, 0, 0), string.Empty)); Assert.AreEqual(new DateTime(2006, 8, 4, 10, 0, 0).ToString(), StandardFilters.Date(new DateTime(2006, 8, 4, 10, 0, 0), null)); Assert.AreEqual(new DateTime(2006, 7, 5).ToString("MM/dd/yyyy"), StandardFilters.Date("2006-07-05 10:00:00", "MM/dd/yyyy")); Assert.AreEqual(new DateTime(2004, 7, 16).ToString("MM/dd/yyyy"), StandardFilters.Date("Fri Jul 16 2004 01:00:00", "MM/dd/yyyy")); Assert.AreEqual(null, StandardFilters.Date(null, "MMMM")); Assert.AreEqual("hi", StandardFilters.Date("hi", "MMMM")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("now", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("today", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("Now", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("Today", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("now", null)); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("today", null)); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("now", string.Empty)); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("today", string.Empty)); Assert.AreEqual("345000", StandardFilters.Date(DateTime.Parse("2006-05-05 10:00:00.345"), "ffffff")); Template template = Template.Parse(@"{{ hi | date:""MMMM"" }}"); Assert.AreEqual("hi", template.Render(Hash.FromAnonymousObject(new { hi = "hi" }))); Helper.AssertTemplateResult( expected: "14, 16", template: "{{ \"March 14, 2016\" | date: \"%d, %y\" }}"); // Test disabled due to bug https://github.com/dotliquid/dotliquid/issues/391 //// Helper.AssertTemplateResult( //// expected: "Mar 14, 16", //// template: "{{ \"March 14, 2016\" | date: \"%b %d, %y\" }}"); //// Helper.AssertTemplateResult( //// expected: $"This page was last updated at {DateTime.Now.ToString("yyyy-MM-dd HH:mm")}.", //// template: "This page was last updated at {{ 'now' | date: '%Y-%m-%d %H:%M' }}."); }
public void TestDate() { Liquid.UseRubyDateFormat = false; DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat; Assert.AreEqual(dateTimeFormat.GetMonthName(5), StandardFilters.Date(DateTime.Parse("2006-05-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(6), StandardFilters.Date(DateTime.Parse("2006-06-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(7), StandardFilters.Date(DateTime.Parse("2006-07-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(5), StandardFilters.Date("2006-05-05 10:00:00", "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(6), StandardFilters.Date("2006-06-05 10:00:00", "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(7), StandardFilters.Date("2006-07-05 10:00:00", "MMMM")); Assert.AreEqual("08/01/2006 10:00:00", StandardFilters.Date("08/01/2006 10:00:00", string.Empty)); Assert.AreEqual("08/02/2006 10:00:00", StandardFilters.Date("08/02/2006 10:00:00", null)); Assert.AreEqual(new DateTime(2006, 8, 3, 10, 0, 0).ToString(), StandardFilters.Date(new DateTime(2006, 8, 3, 10, 0, 0), string.Empty)); Assert.AreEqual(new DateTime(2006, 8, 4, 10, 0, 0).ToString(), StandardFilters.Date(new DateTime(2006, 8, 4, 10, 0, 0), null)); Assert.AreEqual(new DateTime(2006, 7, 5).ToString("MM/dd/yyyy"), StandardFilters.Date("2006-07-05 10:00:00", "MM/dd/yyyy")); Assert.AreEqual(new DateTime(2004, 7, 16).ToString("MM/dd/yyyy"), StandardFilters.Date("Fri Jul 16 2004 01:00:00", "MM/dd/yyyy")); Assert.AreEqual(null, StandardFilters.Date(null, "MMMM")); Assert.AreEqual("hi", StandardFilters.Date("hi", "MMMM")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("now", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("today", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("Now", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("Today", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("now", null)); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("today", null)); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("now", string.Empty)); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("today", string.Empty)); Assert.AreEqual("345000", StandardFilters.Date(DateTime.Parse("2006-05-05 10:00:00.345"), "ffffff")); Template template = Template.Parse(@"{{ hi | date:""MMMM"" }}"); Assert.AreEqual("hi", template.Render(Hash.FromAnonymousObject(new { hi = "hi" }))); Assert.AreEqual(new DateTime(2018, 10, 10, 19, 44, 35).ToString(("yyyy/MM/dd HH:mm:ss")), StandardFilters.Date(1539200675, "yyyy/MM/dd HH:mm:ss")); Liquid.UseRubyDateFormat = true; Assert.AreEqual(new DateTime(2018, 10, 10, 19, 44, 35).ToString(("yyyy/MM/dd HH:mm:ss")), StandardFilters.Date(1539200675, "%Y/%m/%d %H:%M:%S")); }
/// <summary> /// Date filter which is aware of locale settings for date formats. /// </summary> /// <param name="input"></param> /// <param name="format"></param> /// <returns></returns> public static string Date(object input, string format) { if (!String.IsNullOrEmpty(format)) { var loc = String.Format("date_formats.{0}", format); var newFormat = TranslationFilter.T(loc); if (newFormat != loc) { format = newFormat; } if (format == "long") { format = "%d %b %Y %X"; } } return(StandardFilters.Date(input, format)); }
/// <summary> /// Date filter which is aware of locale settings for date formats. /// </summary> /// <param name="input"></param> /// <param name="format"></param> /// <returns></returns> public static string Date(object input, string format) { if (!String.IsNullOrEmpty(format) && !format.Contains("%")) // special formats that can be defined in settings { var loc = String.Format("date_formats.{0}", format); var newFormat = TranslationFilter.T(loc); if (newFormat != loc) { format = newFormat; } if (format == "long") { format = "%d %b %Y %X"; } } return(StandardFilters.Date(input, format)); }
public async Task TestDate() { Liquid.UseRubyDateFormat = false; DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat; Assert.AreEqual(dateTimeFormat.GetMonthName(5), StandardFilters.Date(DateTime.Parse("2006-05-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(6), StandardFilters.Date(DateTime.Parse("2006-06-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(7), StandardFilters.Date(DateTime.Parse("2006-07-05 10:00:00"), "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(5), StandardFilters.Date("2006-05-05 10:00:00", "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(6), StandardFilters.Date("2006-06-05 10:00:00", "MMMM")); Assert.AreEqual(dateTimeFormat.GetMonthName(7), StandardFilters.Date("2006-07-05 10:00:00", "MMMM")); Assert.AreEqual("08/01/2006 10:00:00", StandardFilters.Date("08/01/2006 10:00:00", string.Empty)); Assert.AreEqual("08/02/2006 10:00:00", StandardFilters.Date("08/02/2006 10:00:00", null)); Assert.AreEqual(new DateTime(2006, 8, 3, 10, 0, 0).ToString(), StandardFilters.Date(new DateTime(2006, 8, 3, 10, 0, 0), string.Empty)); Assert.AreEqual(new DateTime(2006, 8, 4, 10, 0, 0).ToString(), StandardFilters.Date(new DateTime(2006, 8, 4, 10, 0, 0), null)); Assert.AreEqual(new DateTime(2006, 7, 5).ToString("MM/dd/yyyy"), StandardFilters.Date("2006-07-05 10:00:00", "MM/dd/yyyy")); Assert.AreEqual(new DateTime(2004, 7, 16).ToString("MM/dd/yyyy"), StandardFilters.Date("Fri Jul 16 2004 01:00:00", "MM/dd/yyyy")); Assert.AreEqual(null, StandardFilters.Date(null, "MMMM")); Assert.AreEqual("hi", StandardFilters.Date("hi", "MMMM")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("now", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("today", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("Now", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString("MM/dd/yyyy"), StandardFilters.Date("Today", "MM/dd/yyyy")); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("now", null)); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("today", null)); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("now", string.Empty)); Assert.AreEqual(DateTime.Now.ToString(), StandardFilters.Date("today", string.Empty)); Assert.AreEqual("345000", StandardFilters.Date(DateTime.Parse("2006-05-05 10:00:00.345"), "ffffff")); var template = Template.Parse(@"{{ hi | date:""MMMM"" }}"); Assert.AreEqual("hi", await template.RenderAsync(Hash.FromAnonymousObject(new { hi = "hi" }))); }