DataTplCalcDateTime.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This example demonstrates the available date and time functions
    // that can be used with the 'calc' report templates feature.
    public class DataTplCalcDateTime
    {
        public GcWordDocument CreateDocx()
        {
            // Dummy data source (the date/time functions shown in this example do not need the data):
            var data = new int[] { 123 };

            var doc = new GcWordDocument();

            // Add the data source:
            doc.DataTemplate.DataSources.Add("ds", data);

            // Styles and templates to show results:
            var bulletListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "bulletListTemplate");
            var exStyle = doc.Styles[BuiltInStyleId.Heading3];
            var resStyle = doc.Styles[BuiltInStyleId.Strong];

            var paras = doc.Body.Paragraphs;
            add("{{ calc Now() }}");
            add("{{ calc Today() }}");
            add("{{ calc UtcNow() }}");

            add("{{ calc AddTicks(Now(), 100) }}");
            add("{{ calc AddMilliSeconds(Now(), 60 * 1000) }}");
            add("{{ calc AddSeconds(Now(), 60) }}");
            add("{{ calc AddMinutes(Now(), 1) }}");
            add("{{ calc AddHours(Now(), 2) }}");

            add("{{ calc AddDays(Today(), 7) }}");
            add("{{ calc AddMonths(Today(), 4) }}");
            add("{{ calc AddYears(Today(), 10) }}");
            // add("{{ calc AddTimeSpan(Today(), new TimeSpan(123)) }}");

            add("{{ calc DateDiffTick(\"1-Jan-2001\", Today()) }}");
            add("{{ calc DateDiffMilliSecond(Today(), Now()) }}");
            add("{{ calc DateDiffSecond(Today(), Now()) }}");
            add("{{ calc DateDiffMinute(Today(), Now()) }}");
            add("{{ calc DateDiffHour(Today(), Now()) }}");
            add("{{ calc DateDiffDay(\"1-Jan-2001\", Today()) }}");

            add("{{ calc GetTimeOfDay(Now()) }}");
            add("{{ calc GetMilliSecond(Now()) }}");
            add("{{ calc GetSecond(Now()) }}");
            add("{{ calc GetMinute(Now()) }}");
            add("{{ calc GetHour(Now()) }}");

            add("{{ calc GetDate(Now()) }}");
            add("{{ calc GetDayOfWeek(Now()) }}");
            add("{{ calc GetDay(Now()) }}");
            add("{{ calc GetDayOfYear(Now()) }}");
            add("{{ calc GetMonth(Now()) }}");
            add("{{ calc GetYear(Now()) }}");

            // Process the templates:
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));

            // Add a short note describing the demo at the top of the document:
            paras.Insert(
                "This example demonstrates the available date and time functions " +
                "that can be used with the 'calc' report templates feature. " +
                "Please see this example's source code for full details.",
                InsertLocation.Start);
            paras.Insert("Report templates: available calc date/time functions", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);

            // Done:
            return doc;

            void add(string expr)
            {
                // \x200B is a zero-width space used to prevent template expansion:
                paras.Add(expr.Insert(1, "​​​\x200B") + " :  ", exStyle).ListFormat.Template = bulletListTemplate;
                paras.Last.GetRange().Runs.Add(expr, resStyle);
            }
        }
    }
}