views:

263

answers:

5

Hey! I am trying to change a number of seconds in to a proper time stamp format. Like I do this to change 180 to 03:00

private void writeTime(int tempo)
        {
            TimeSpan otempo = new TimeSpan(0, 0, tempo);        


            string minutos = ((otempo.Minutes <= 9) ? "0" : "") + otempo.Minutes.ToString();
            string segundos = ((otempo.Seconds <= 9) ? "0" : "") + otempo.Seconds.ToString();

            label1.Text = minutos + ":" + segundos;

            centrarLabel();
        }

This does give me 180 into a proper format. I just want to know if there is a simpler way. This function might be called many many times and I don't want to create a new instance of TimeSpan every single time as I think this might pose a problem with memory etc. I tried using the DateTime class but... I just simply don't see how I can pass it the seconds and it gives me the proper format :(

I am not that great with c#. I am really trying to learn :) Thanks

+3  A: 
    int tempo = 180;
    TimeSpan time = TimeSpan.FromSeconds(tempo);
    string txt = string.Format(
        "{0:00}:{1:00}", time.Minutes, time.Seconds);

(edit) As has already been observed - there are no immediate memory concerns with TimeSpan, since it is a struct. However, if you want to be paranoid:

int tempo = 180;
string txt = new StringBuilder(5)
    .Append((tempo / 60).ToString().PadLeft(2, '0')).Append(':')
    .Append((tempo % 60).ToString().PadLeft(2, '0')).ToString();
Marc Gravell
+3  A: 
TimeSpan t = TimeSpan.FromSeconds(180);
string s = String.Format("{0:00}:{1:00}", t.Hours, t.Seconds);
DrJokepu
The is no ToString(string format) overload :-(
Think Before Coding
That is what I thought at first, but none such exists
Marc Gravell
Very true, sorry. I'll delete my answer then.
DrJokepu
Fixed it. Not as good as a ToString() overload, but at least it works.
DrJokepu
+3  A: 

use

label.Text = string.Format("{0:d2}:{1:d2}", otempo.Minutes, otempo.Seconds);

You can use oTempo.TotalMinutes if your minute count can grow bigger that 60...

And don't forget to specify a culture also.

Think Before Coding
FormatException: Input string was not in a correct format.
Marc Gravell
Oops sorry, changed that
Think Before Coding
+1  A: 

TimeSpan is a struct, it is (in this case) allocated on the stack so the cost it negligible, especially considering that you have to make at least one string allocation anyway which is the expensive part.

TimeSpan.FromSeconds(tempo).ToString();

should be sufficient for your needs so long as 180 seconds -> "00:03:00" is acceptable

ShuggyCoUk
string allocation expensive? I think I need to switch careers...
leppie
I also hope you realise TimeSpan (or any struct/valuetype for that matter) will be boxed when you call a virtual method on it :)
leppie
I beg to differ and direct you to the wonderful opcode constrained.t.ToString(); (t is a TimeSpan)becomesldloca.s spanconstrained [mscorlib]System.TimeSpancallvirt instance string [mscorlib]System.Object::ToString()In this case the constrained makes the virtual call *not* trigger a box
ShuggyCoUk
Oh and since the OP brought up the perf aspects I thought I would indicate that the creation of TimeSpan was negligible in comparison to the rest of what he was doing...
ShuggyCoUk
+1  A: 

Like others have mentioned, there's probably no need to worry about creating a new TimeSpan to do the conversion, but the simplest way to format it without creating any temporary objects is this:

String.Format("{0:00}:{1:00}", tempo / 60, tempo % 60)
Matthew Crumley