George Boulton
George Boulton

Reputation: 101

List of TimeSpan calculated by results from other list

I'm trying to create a list based on another list. The list looks like - {T1, T1, T2, T2, T3, T3} however the integer is subject to change based on user inputs. I am trying to assign a TimeSpan value on a new list based on the index of the old list, and the integers will vary the result. For example, if the start time is given as 11:00, and the time gap given by the user is 5 (minutes), the new list should look like - {11:00, 11:00, 11:05, 11:05, 11:10; 11:10}

Here is my current function:

public List<string> TimeGet(List<string> heatList, TimeSpan startTimeSpan, TimeSpan timeGap)
{
    List<string> timeList = new List<string>();
    string timeToAddString;
    for (int i = 0; i < heatList.Count; i++)
    {
        if (heatList[i].Contains("1"))
        {
            TimeSpan timeToAdd = startTimeSpan;
            timeToAddString = Convert.ToString(timeToAdd);
            timeList.Add(timeToAddString);
        }
        else
        {
            string resultString = Regex.Match(heatList[i], @"\d+").Value;
            int resultInt = Int32.Parse(resultString);
            timeGap = TimeSpan.FromMinutes(timeGap.Minutes * resultInt);
            TimeSpan timeToAdd = startTimeSpan + timeGap;
            timeToAddString = Convert.ToString(timeToAdd);
            timeList.Add(timeToAddString);
        }
    }
}

I would like the new list to correspond to the values on heatlist depending on the integer based in each string on the list, and produce a timespan or time of day to correspond with it on a new list.

Upvotes: 1

Views: 1660

Answers (4)

Hamid Pourjam
Hamid Pourjam

Reputation: 20764

you can do that with simple linq statement

public static List<string> TimeGet(List<string> heatList, TimeSpan startTimeSpan, TimeSpan timeGap)
{
    return heatList
        .Select(x =>
            startTimeSpan.Add(TimeSpan.FromMinutes(timeGap.Minutes*(int.Parse(x.Substring(1)) - 1)))
                .ToString(@"hh\:mm")).ToList();
}

this will select an item from heatList one by one, parse the number in it and subtract 1 from it (so T1 result is 0 and T2 in 1 and ...), now add timeGap times the resulted number to startTimeSpan and format it in hh:mm format.

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112512

Use LINQ

return heatList
    .Select(t => TimeSpan.FromTicks(
                     (Int32.Parse(t.Substring(1))-1) * timeGap.Ticks
                 )
                 + startTimeSpan).ToString("hh:mm")
    .ToList();

Explanation:
.Select(t => ...) enumerates the strings from the heatList assigning each temperature to t.
t.Substring(1) skips the "T" in "T123".
Int32.Parse(t.Substring(1)) - 1 creates the number range 0, 1, 2 ...
* timeGap.Ticks gives the offset for a given temperature in ticks.
TimeSpan.FromTicks(...) + startTimeSpan) yields the resulting time by adding timespans.
.ToString("hh:mm") converts the resulting timespan into a string.
.ToList(); creates a new list.

Ticks is the unit TimeSpan uses to store time spans internally.

Upvotes: 2

user4356796
user4356796

Reputation:

namespace Test
{
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;

    class MainClass
    {
        public static void Main (string[] args)
        {
            List<string> inputdata = new List<string> ();
            List<TimeSpan> outputdata = new List<TimeSpan> ();

            string input = null;
            while ((input = Console.ReadLine ()) != string.Empty) {
                inputdata.Add (input);
                TimeSpan t = new TimeSpan (11, 0, 0) + new TimeSpan (0, Convert.ToInt32 (Regex.Match (input, "\\d+").ToString ()), 0);
                outputdata.Add (t);
            }

            for (int i = 0; i < inputdata.Count; i++) {
                Console.WriteLine ("Inputdata: {0}, Outputdata: {1}", inputdata [i], outputdata [i].ToString ());
            }
        }
    }
}

Upvotes: 2

jan.h
jan.h

Reputation: 524

This function can be made much simpler, assuming every element in heatList follows that T\d+ pattern.

public IEnumerable<string> TimeGet(List<string> heatList, TimeSpan startTimeSpan, TimeSpan timeGap)
{
    foreach (var element in heatList)
    {
        var input = element.Substring(1); // Takes everything from index 1 = the first digit in the string
        int multiplier = Int32.Parse(input) - 1;
        var additionalTime = new TimeSpan(0, (int)(timeGap.Minutes * multiplier), 0);
        yield return (startTimeSpan + additionalTime).ToString();
    }
}

Sample usage:

string[] sBaseList = { "T1", "T1", "T2", "T2", "T4", "T6" };
var sList = sBaseList.ToList();
TimeSpan startSpan = new TimeSpan(11, 0, 0);
TimeSpan gapSpan = new TimeSpan(0, 5, 0);
var result = TimeGet(sList, startSpan, gapSpan);
foreach (var s in result)
    Console.WriteLine(s);
Console.ReadKey();

Result: 11:00:00, 11:00:00, 11:05:00, 11:05:00, 11:15:00, 11:25:00.

Upvotes: 2

Related Questions