ID:1485254
 
(See the best response by Ter13.)
Just trying to make a clock tell the exact time whenever a player looks at it, for where ever the host server is.

The Time, PM or AM, and the Day (ie: 4:40 A.M. Fri)

I looked up world.timeofday & time2text() but I can't seem to figure out how to set it up properly.



Perhaps later I will have events happen based on the time, but for now I just want a player to be able to look at a clock and know what the server time is.
The reference for time2text() is pretty specific, and enlightening.

world.timeofday is also based on GMT only.

To put the time in the format you want, we need to do some work, though, because we want civilian, and not military time.

var/hour = round(world.timeofday/36000) //get current hour by dividing timeofday by 60 seconds times 60 minutes times ten.
var/ampm = "AM"
//adjust hour into civilian time from military time
//also change am/pm to proper value.
if(hour>12)
hour -= 12
ampm = "PM"
else if(hour==12)
ampm = "PM"
else if(hour==0)
hour = 12
var/time = time2text(world.timeofday,"[hour]:mm [ampm] DDD.")


The above example, if run at the current time on my system clock, would return: "10:06 AM Fri."
would these be in the clocks syntax, or worlds?

I guess where my problem lies, is calling the time for the player.

Like so?

 Clock
icon_state="clock"
Click()
usr << world.time// just a mundane example not code
Best response
As for how to adjust the time to your current time, you are going to need to tell the server what time zone it is in:

var
GMTOffset = -5


Now, applying GMTOffset to timeofday can return negative values, as well as values greater than the timeofday limit. This is okay, because unless we need to the 1/10th second precision, we can still use the value by adding in world.realtime.

So let's take a look at the function in my first post, and convert to local time:

var/curtime = world.timeofday
var/hour = round(curtime/36000) + GMTOffset
var/ampm = "AM"

//ensure date is accurate
if(hour>23)
hour -= 24
curtime = world.realtime + GMTOffset * 36000
else if(hour<0)
hour += 24
curtime = world.realtime + GMTOffset * 36000

//adjust hour into civilian time
if(hour>12)
hour -= 12
ampm = "PM"
else if(hour==12)
ampm = "PM"
else if(hour==0)
hour = 12

var/time = time2text(curtime,"[hour]:mm [ampm] DDD.")


The above example will now display: "5:06 AM, Fri.", when GMTOffset is set to -5 (which is my current time zone, EDT.) Daylight savings is your own mess to deal with. I don't even want to touch that concept with a 10 foot pole.
In response to Komuroto
Komuroto wrote:
would these be in the clocks syntax, or worlds?

The above question doesn't actually mean anything. The clock doesn't have a "syntax", nor does the world. The clock and world are both defined using the DM syntax.

I think what you meant is the world or the clock's scope.

The thing is, since we're using only local and world variables, it doesn't matter where you use my example, provided it follows DM's rules.

You are going to want to embed this behavior at the lowest level possible in order for it to make sense, but the highest level reasonable.

Second, you can't define new things in the world scope. The world is a global object, and things that are accessible to the entire project should be in the global scope.

Komuroto wrote:
I guess where my problem lies, is calling the time for the player.

I don't really have any advice for that. I can't make that decision for you, because I don't know what you want to do, or how you want it to work. I can give you pointers on what's more and less sensible, but how it "should" work is difficult to say without a very, very specific use-case scenario.

Komuroto wrote:
Like so?

 Clock
> icon_state="clock"
> Click()
> usr << world.time// just a mundane example not code
>


I'm not sure what you are trying to get across, with this, really. world.time is the number of ticks the world has been running for in 1/10th seconds. world.realtime is the real time since Jan 1st 2000 GMT in 1/10th seconds. world.timeofday is the current time of the day in 1/10th seconds, relative to GMT.

But yeah, the above example would be in the proper scope if you wanted to tell the player the time whenever they clicked on the clock object.
I guess what I'm trying to say is I honestly haven't the slightest clue what I'm doing when it comes to time, and world vars. I just haven't touched base with that yet.

While I understand how your code is working, and what its doing, I just don't understand where I'm suppose to apply it, or how.

All I'm wanting to do is have a basic ingame object that tells the player the time when he/she clicks on it.

But whenever I try something I just get undefined or unused errors.
Take my example in post #3, and put it in your clock object's Click() proc.

If you get errors, show me exactly what you did, the error message, and the exact lines that each error message points to.

I can explain to you why the errors mean what they mean, but until then, I can't really offer you much assistance with errors.
Um put it under obj/clock?

Like?

Clock
icon_state="clock"
Click()
var/GMTOffset = -5
proc
Time_Check()
var/curtime = world.timeofday
var/hour = round(curtime/36000) + GMTOffset
var/ampm = "AM"

//ensure date is accurate
if(hour>23)
hour -= 24
curtime = world.realtime + GMTOffset * 36000
else if(hour<0)
hour += 24
curtime = world.realtime + GMTOffset * 36000

//adjust hour into civilian time
if(hour>12)
hour -= 12
ampm = "PM"
else if(hour==12)
ampm = "PM"
else if(hour==0)
hour = 12

var/time = time2text(curtime,"[hour]:mm [ampm] DDD.")
usr << time


test.dm:52:error: proc definition not allowed inside another proc

Which I'm positive that occurred because I put a proc under Click which i'm sure is a "no-no"

wouldn't it be better to just make a proc, like Time_Check() and then call that proc under click() ?
Nevermind Got it.

Stupid stupid stupid me.... this is what happens when you leave for a year...

Wow I feel a tad bit dumb at the moment.

Can't believe I was trying to do all that unnecessary stuff.

Thanks for your patience, I was being stupid, perhaps I should go to bed since its 6:22AM :P
wouldn't it be better to just make a proc, like Time_Check() and then call that proc under click() ?

Yes and no. Every time you call a function, there is a slight overhead incurred. It will take slightly longer to perform Time_Check() than it would to perform the entire contents of Time_Check() within Click().

Defining behavior is good when you want to do something multiple places, and embedding code is good when you want it to be fast, or only need to do it in one place.

Second, I'd recommend making GMTOffset a global variable, not a local variable in Click(), or an instance variable. Since it refers to the server's current time, you don't need multiple copies floating around.

Try something more like this:

var/GMTOffset = -5

obj
Clock
icon_state="clock"
Click()
var/curtime = world.timeofday
var/hour = round(curtime/36000) + GMTOffset
var/ampm = "AM"

//ensure date is accurate
if(hour>23)
hour -= 24
curtime = world.realtime + GMTOffset * 36000
else if(hour<0)
hour += 24
curtime = world.realtime + GMTOffset * 36000

//adjust hour into civilian time
if(hour>12)
hour -= 12
ampm = "PM"
else if(hour==12)
ampm = "PM"
else if(hour==0)
hour = 12

usr << time2text(curtime,"[hour]:mm [ampm] DDD.")
Thanks for the follow up reply, but I got it about 10mins ago.

I just took a deep breath, I knew this, just for some reason I was being a complete moron....

Forgive my stupidity.
In response to Komuroto
Komuroto wrote:
Thanks for the follow up reply, but I got it about 10mins ago.

I just took a deep breath, I knew this, just for some reason I was being a complete moron....

Forgive my stupidity.

Make sure you don't miss out on the bit that I wrote about global variables and proper encapsulation of behavior. That bit's really important, and will save you a lot of headache down the road. Just because it works doesn't mean it's right. ;P

Also: Since every question in this forum is archived and searchable, we definitely like to keep information as complete as possible for other people. Just because you understand now, doesn't mean the thread is over. Someone else might be having the same problem six months down the line, so if we just stopped at: "I get it now." without providing the solution, this thread isn't a good candidate for helping others.

Just something to keep in mind when paying your dues to the Developer Help gods.
Yes indeed, got that part loud and clear.

I was just looking at what I was doing, and remembered I had done something like this before.. and it just clicked. (no pun intended)

I was setting up my click() wrong, and jamming extra stuff that just didn't need to be there. So moved somethings around, made sure everything was indented properly and moved the gmt to global.

But GMT in general, sounds simple,but never would've thought of that, thanks for the tip.