Perl and 24 hour time format

General discussions and other topics.
3 posts Page 1 of 1
by Steve » Wed Jun 19, 2013 2:40 pm
Hello.

I'm writing a script in Perl and dealing with some times that have been stored as integers in a MYSQL database. The times are stored in 24-hour format ( example: 233000 ). This is the first time I've dealt with this date format and need help converting it to display as 11:30 PM. I've googled like crazy and still can't figure it out.

Second problem is that I need to also add a duration to this time to determine the end time. The duration is stored as minutes, so I need to add the duration to the original time and also display that result.

Any help is greatly appreciated.
by fmc » Wed Jun 19, 2013 3:55 pm
I would convert both numbers to seconds: the timestamp as second-of-day (there are 86400 seconds in most days, which may be a good enough assumption for your task) and the duration as seconds. Then adding them together becomes, well, adding them together. Then write a sub to convert second-of-day to your display form.

Converting that SQL timestamp to seconds:

Code: Select all

  $ssec = $sqltime % 100;
  $smin = int($sqltime / 100) % 100;
  $shour = int($sqltime / 10000);
  $secofday = $ssec + ($smin * 60) + ($shour * 3600);
Getting the second-of-day closer to printable:

Code: Select all

  if ($secofday >= 43200) {
    $pampm = "PM";
  } else {
    $pampm = "AM";
  }
  $hr = $secofday / 3600;
  $phr = $hr % 12;
  $phr = 12 if ($phr == 0); 
  $pmin = int(($secofday % 3600)/60);
  $psec = $secofday % 60;
by Steve » Wed Jun 19, 2013 5:07 pm
Thanks a whole bunch. Just discovered the time is being stored with a 4 hour offset so I am adjusting for that as well.

I really appreciate the help.
3 posts Page 1 of 1

Who is online

In total there are 20 users online :: 0 registered, 0 hidden and 20 guests (based on users active over the past 5 minutes)
Most users ever online was 999 on Mon May 10, 2021 1:02 am

Users browsing this forum: No registered users and 20 guests