C
C#10mo ago
thefake

confused, passing tuple?

// fixes times like more than 60 seconds
// int, int, int
public (int, int, int) Fix(int hours, int minutes, int seconds)
{
return realFix(hours, minutes, seconds);
}
// string
public string Fix(string timeString)
{
return ConvertVariablesToTimeString(realFix(ConvertTimeStringToVariables(timeString)));
}
// NOTE: maybe 1 convert method instead of 2?
// Converts Time String to variables to use
public (int, int, int) ConvertTimeStringToVariables(string timeString)
{
string[] times = timeString.Split(':');
int hours = int.Parse(times[0]);
int minutes = int.Parse(times[1]);
int seconds = int.Parse(times[2]);
return (hours, minutes, seconds);
}

// Converts Variables to Time String
public string ConvertVariablesToTimeString(int hours, int minutes, int seconds)
{
return $"{hours}:{minutes}:{seconds}";
}
public (int, int, int) realFix(int hours, int minutes, int seconds)
{
while (seconds >= 60)
{
seconds -= 60;
minutes++;
}
while (minutes >= 60)
{
minutes -= 60;
hours++;
}
return (hours, minutes, seconds);
}
// fixes times like more than 60 seconds
// int, int, int
public (int, int, int) Fix(int hours, int minutes, int seconds)
{
return realFix(hours, minutes, seconds);
}
// string
public string Fix(string timeString)
{
return ConvertVariablesToTimeString(realFix(ConvertTimeStringToVariables(timeString)));
}
// NOTE: maybe 1 convert method instead of 2?
// Converts Time String to variables to use
public (int, int, int) ConvertTimeStringToVariables(string timeString)
{
string[] times = timeString.Split(':');
int hours = int.Parse(times[0]);
int minutes = int.Parse(times[1]);
int seconds = int.Parse(times[2]);
return (hours, minutes, seconds);
}

// Converts Variables to Time String
public string ConvertVariablesToTimeString(int hours, int minutes, int seconds)
{
return $"{hours}:{minutes}:{seconds}";
}
public (int, int, int) realFix(int hours, int minutes, int seconds)
{
while (seconds >= 60)
{
seconds -= 60;
minutes++;
}
while (minutes >= 60)
{
minutes -= 60;
hours++;
}
return (hours, minutes, seconds);
}
8 Replies
thefake
thefakeOP10mo ago
what I want: Fix time: "00:00:60" --> "00:01:00:" problem: the method returns 3 ints as a tuple and the other method accepts 3 ints that are not in a tuple
Anchy
Anchy10mo ago
You can use TimeSpan for this, extract your hours, minutes, seconds and then use the TimeSpan class to construct a time that you can format using your code, minus the tuples:
static void Main(string[] args)
{
string input = "00:00:60";

string output = ConvertSecondsToMinutes(input);

Console.WriteLine(output);
}

static string ConvertSecondsToMinutes(string input)
{
string[] times = input.Split(":");

int hour = int.Parse(times[0]);
int minutes = int.Parse(times[1]);
int seconds = int.Parse(times[2]);

TimeSpan time = TimeSpan.FromHours(hour).Add(TimeSpan.FromMinutes(minutes)).Add(TimeSpan.FromSeconds(seconds));

return time.ToString();
}
static void Main(string[] args)
{
string input = "00:00:60";

string output = ConvertSecondsToMinutes(input);

Console.WriteLine(output);
}

static string ConvertSecondsToMinutes(string input)
{
string[] times = input.Split(":");

int hour = int.Parse(times[0]);
int minutes = int.Parse(times[1]);
int seconds = int.Parse(times[2]);

TimeSpan time = TimeSpan.FromHours(hour).Add(TimeSpan.FromMinutes(minutes)).Add(TimeSpan.FromSeconds(seconds));

return time.ToString();
}
thefake
thefakeOP10mo ago
whaaat is this better? I just had the problem passing tuple thats what was in my mind
i like chatgpt
i like chatgpt10mo ago
Complete version using better descriptive names:
class MyTime
{
public (int, int, int) Fix(int hours, int minutes, int seconds) =>
Normalize((hours, minutes, seconds));

public string Fix(string timeString) =>
TupleToString(Normalize(TimeToTuple(timeString)));

public (int, int, int) TimeToTuple(string timeString)
{
string[] times = timeString.Split(':');
int hours = int.Parse(times[0]);
int minutes = int.Parse(times[1]);
int seconds = int.Parse(times[2]);
return (hours, minutes, seconds);
}

public string TupleToString((int hours, int minutes, int seconds) t)
{
return $"{t.hours}:{t.minutes}:{t.seconds}";
}

public (int, int, int) Normalize((int hours, int minutes, int seconds) t)
{
var seconds = t.seconds;
var minutes = t.minutes;
var hours = t.hours;

while (seconds >= 60)
{
seconds -= 60;
minutes++;
}
while (minutes >= 60)
{
minutes -= 60;
hours++;
}
return (hours, minutes, seconds);
}
}
class MyTime
{
public (int, int, int) Fix(int hours, int minutes, int seconds) =>
Normalize((hours, minutes, seconds));

public string Fix(string timeString) =>
TupleToString(Normalize(TimeToTuple(timeString)));

public (int, int, int) TimeToTuple(string timeString)
{
string[] times = timeString.Split(':');
int hours = int.Parse(times[0]);
int minutes = int.Parse(times[1]);
int seconds = int.Parse(times[2]);
return (hours, minutes, seconds);
}

public string TupleToString((int hours, int minutes, int seconds) t)
{
return $"{t.hours}:{t.minutes}:{t.seconds}";
}

public (int, int, int) Normalize((int hours, int minutes, int seconds) t)
{
var seconds = t.seconds;
var minutes = t.minutes;
var hours = t.hours;

while (seconds >= 60)
{
seconds -= 60;
minutes++;
}
while (minutes >= 60)
{
minutes -= 60;
hours++;
}
return (hours, minutes, seconds);
}
}
MODiX
MODiX10mo ago
💃Hazel | へいぜる
REPL Result: Success
var hours = 0;
var minutes = 0;
var seconds = 60;
var fixedTime = new TimeSpan(hours, minutes, seconds);
Console.WriteLine(fixedTime);
var hours = 0;
var minutes = 0;
var seconds = 60;
var fixedTime = new TimeSpan(hours, minutes, seconds);
Console.WriteLine(fixedTime);
Console Output
00:01:00
00:01:00
Compile: 414.243ms | Execution: 24.919ms | React with ❌ to remove this embed.
Hazel 🌊💃
Hazel 🌊💃10mo ago
You should really justify the need for a tuple because more often than not either a type already exists or you can easily create a concrete model to represent it.
thefake
thefakeOP10mo ago
i see
Want results from more Discord servers?
Add your server