1

I need to pass from microseconds (saved inside a unsigned long long int variable) to its representation as hours, minutes, seconds, milliseconds, that is:

from 47072349659 to 13:04:32.350

I found this conversion from milliseconds, but I don't seem to manage to make it work in my case. Maybe the problem is that the number is too long to be stored in certain variable type? I'm using unsigned long long int for input time and tried int, long, unsigned long long int for outputs.

Here is my C++ code:

unsigned long long int timestamp;

long milliseconds   = (long) (timestamp / 1000000) % 1000;
long seconds    = (long) ((timestamp / (1000)) % 60);
long minutes    = (long) ((timestamp / (60000)) % 60);
long hours      = (long) ((timestamp / (3600000)) % 24);
Community
  • 1
  • 1
ocramot
  • 1,361
  • 28
  • 55
  • Which language? Show some code. – Jon Jan 29 '13 at 09:52
  • Any language. I'm writing in c++, but I just want the pseudocode. I'm trying to use the code shown on the link I provided, but it's not working for microseconds. – ocramot Jan 29 '13 at 09:56
  • You're doing something wrong. Manual calculations show that your numbers are OK and a 64-bit integer is definitely enough. Show us your code. – Alexey Frunze Jan 29 '13 at 10:08
  • Sorry, I copied the first number from the wrong line. Anyway I corrected the numbers and I added my code. – ocramot Jan 29 '13 at 10:23
  • why do you devide by 1 000 000 when you want milliseconds. a division by 1 000 would be sufficient. – artragis Jan 29 '13 at 10:25
  • Basically because I made several attempts and I must have messed with the numbers in the last one... anyway, you're answer seems to work, thanks. – ocramot Jan 29 '13 at 10:48

1 Answers1

3

I think your mistake is in your deviders :

long milliseconds   = (long) (timestamp / 1000) % 1000;
long seconds    = (((long) (timestamp / 1000) - milliseconds)/1000)%60 ;
long minutes    = (((((long) (timestamp / 1000) - milliseconds)/1000) - seconds)/60) %60 
long hours      = ((((((long) (timestamp / 1000) - milliseconds)/1000) - seconds)/60) - minutes)/60
artragis
  • 3,677
  • 1
  • 18
  • 30