Author Topic: Time Help  (Read 1731 times)

I got the getDateTime function and simplified it to just the time:

restWords(getDateTime());

Now instead of it using the 24 hour format, I want it to use the
12 hour format or whatever you call it.

Code: [Select]
%time2=getDateTime();
%time=substr(%time2,0,1);
if(%time<=12)
{
    %a=am;
    %time=%time@subStr(%time2,2,8)@%a;
}
else if(%time>=13)
{
    %a=pm;
    %time=%time-12@subStr(%time2,2,8)@%a;
}

Something like that maybe?

Code: [Select]
%time2=getDateTime();
%time=substr(%time2,0,1);
if(%time<=12)
{
    %a=am;
    %time=%time@subStr(%time2,2,8)@%a;
}
else if(%time>=13)
{
    %a=pm;
    %time=%time-12@subStr(%time2,2,8)@%a;
}

Something like that maybe?
No. Many problems with this.

Here's what I use.
Code: [Select]
function getTime()
{
%hour = getSubStr(getWord(getDateTime(), 1), 0, strPos(getWord(getDateTime(), 1), ":"));
%minute = getSubStr(getWord(getDateTime(), 1), 3, 2);
%word = "AM";

if(%hour >= 12 && %hour != 24)
{
if(%hour >= 13)
{
%hour -= 12;
}

%word = "PM";
}

return %hour @ ":" @ %minute SPC %word;
}
Returns HOUR:MINUTE AM/PM.

Code: [Select]
%time2=getDateTime();
%time=substr(%time2,0,1);
if(%time<=12)
{
    %a=am;
    %time=%time@subStr(%time2,2,8)@%a;
}
else if(%time>=13)
{
    %a=pm;
    %time=%time-12@subStr(%time2,2,8)@%a;
}

Something like that maybe?
Close, but not quite. subStr (it's getSubStr()), isn't a default function, and once you checked if %time is less than 12, you can just use else instead of an else if. Also, you didn't have whatever this code is return anything, and all it does is define values, and never does anything with them.

Here's one I wrote around 2 days ago (gives you HOUR MINUTE AM/PM):


function get12Time()
{
   nextToken(nextToken(getWord(getDateTime(), 1), "hour", ":"), "minutes", ":");
   %new = %hour;
   return ((%hour > 12 ? %hour-=12 : %hour) SPC %minutes SPC (%new > 12 ? "PM" : "AM"));
}

If anyone would kindly help me into getting this to fit into one line, I would appreciate that.
« Last Edit: July 09, 2014, 09:04:41 PM by Cruxeis »

Here's one I wrote around 2 days ago (gives you HOUR MINUTE AM/PM):


function get12Time()
{
   nextToken(nextToken(getWord(getDateTime(), 1), "hour", ":"), "minutes", ":");
   %new = %hour;
   return ((%hour > 12 ? %hour-=12 : %hour) SPC %minutes SPC (%new > 12 ? "PM" : "AM"));
}

If anyone would kindly help me into getting this to fit into one line, I would appreciate that.
1. That won't even work. %hour is an undefined variable.
2. The whole nextToken thing you're doing does not even return the right thing.
3. You're not even setting the nextToken thing to a variable.
4. Why would you want it all in one line? Not very readable that way.
« Last Edit: July 09, 2014, 10:19:08 PM by jes00 »

Close, but not quite. subStr (it's getSubStr()), isn't a default function, and once you checked if %time is less than 12, you can just use else instead of an else if. Also, you didn't have whatever this code is return anything, and all it does is define values, and never does anything with them.
Yeah sorry, I was close. I don't write things in to be copied and pasted right in, I tend to like to give general ideas. Also, getSubStr is default.
Quote from: ConsoleDump
   virtual string getSubStr(string str, int start, int numChars) {}

Yeah sorry, I was close. I don't write things in to be copied and pasted right in, I tend to like to give general ideas. Also, getSubStr is default.
he's saying subStr() isn't default, but getSubStr() is.

1. That won't even work. %hour is an undefined variable.
2. The whole nextToken thing you're doing does not even return the right thing.
3. You're not even setting the nextToken thing to a variable.
4. Why would you want it all in one line? Not very readable that way.
The first 3 of these are false. Test the actual code. The 2nd arg is what sets the variable. Go ahead, try it.
==>%this = "12:14:05"; nextToken(%this, "asdf", ":"); echo(%asdf);
I have tested my code many times, the entire thing works just fine. Actually run it and see what it does before you start saying "it won't work", because it does.
« Last Edit: July 09, 2014, 10:53:01 PM by Cruxeis »

The first 3 of these are false. Test the actual code. The 2nd arg is what sets the variable. Go ahead, try it.
==>%this = "12:14:05"; nextToken(%this, "asdf", ":"); echo(%asdf);
14:05
I have tested my code many times, the entire thing works just fine. Actually run it and see what it does before you start saying "it won't work", because it does.
Weird. There are almost no default functions in TorqueScript that set a variable. Almost all of them just return the new value, not set it. Also, when I tested it, I just echoed the function. Because I expected it to return the result, not set it to a variable.
« Last Edit: July 09, 2014, 10:51:52 PM by jes00 »

Weird. There are almost no default functions in TorqueScript that set a variable. Almost all of them just return the new value, not set it. Also, when I tested it, I just echoed the function. Because I expected it to return the result, not set it to a variable.
Oh okay, I understand why you thought that. I actually had to look at a couple of mods made by Truce to figure out how to properly understand it. It is weird though.


function get12Time()
{
   nextToken(nextToken(getWord(getDateTime(), 1), "hour", ":"), "minutes", ":");
   %new = %hour;
   return ((%hour > 12 ? %hour-=12 : %hour) SPC %minutes SPC (%new > 12 ? "PM" : "AM"));
}

If anyone would kindly help me into getting this to fit into one line, I would appreciate that.
Code: [Select]
function get12Time(){nextToken(nextToken(getWord(getDateTime(), 1), "hour", ":"), "minutes", ":");%new = %hour;return ((%hour > 12 ? %hour-=12 : %hour) SPC %minutes SPC (%new > 12 ? "PM" : "AM"));}

It's not that hard, torque ignores spaces for the most part.

If you don't need all that text, you can just do

%time = (%s = getSubStr(%t = restWords(getDateTime()),0,2)) > 12 ? (%s-12) @ getSubStr(%t,2,strLen(%t)-2) SPC "PM" : %t;

edit: forgot to add PM in the case of pm

If anyone would kindly help me into getting this to fit into one line, I would appreciate that.

hehe, didn't even realize you requested it, but there ya go.
« Last Edit: July 09, 2014, 11:36:17 PM by $trinick »