C
C#5mo 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
thefake5mo 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
Anchy5mo 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
thefake5mo ago
whaaat is this better? I just had the problem passing tuple thats what was in my mind
i like chatgpt
i like chatgpt5mo 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
MODiX5mo 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 🌊💃5mo 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
thefake5mo ago
i see
Want results from more Discord servers?
Add your server
More Posts
UWP Digest credentials not supported?i have the following code in my xamarin.forms project. (yeah i know its deprecated, but thats what iHow can I convert null entries in JSON data to a default value?I have JSON data that contains null entries for some values. It would make the business code a lot eHow do I take text from input element of a form and put it in AspNetUsers table properly?I'm passing it from view to controller action through parameters, if that's relevant. This input eleWindows keyboard hook (SetWindowsHookExA) doesn't get called.Simplified version: ```cs KeyboardListener.KeyPress += (key) => { Console.WriteLine(Enum.GetName✅ What's a good strategy to load serverside resources into your controller?Right now I have a few images and videos that so far i've been loading to views using Directory callhow can i get the data from database and put them to a dropdownhow can i get the data from database and put them to a dropdown , when im trying to do that it showsDont know what i did, program now giving CS0101 and CS0111 errors.Cant launch the debug thingamabob. How would I fix it?✅ Changed something manually in AspNetUsers and now can't loginI went in with SQL Server Object Explorer and manually changed the NormalizedUserName column and now✅ How to automate update-databaseI am bilding a Web server but i have to typ in update-database how can i automate this in Blazor in ✅ Regex help pls:`^[a-z]+(?:_[a-z]+)?[a-z]+$` how can I check so that there are atleast 4 characters?