How can I get the current time in Godot? - godot

I'm trying to make a digital clock in my game and need the current time (hours and minutes), formatted as so: HH:MM:SS How can I get the time necessary for this?

You can use the Time singleton, with the get_datetime_dict_from_system method to get the time dictionary from the current system time.
# Get the time dictionary
var time = Time.get_time_dict_from_system()
print(time) # {day:X, dst:False, hour:xx, minute:xx, month:xx, second:xx, weekday:x, year:xxxx}
In your case, to format it as HH:MM, you can use the less noisy function get_time_dict_from_system which only has hours, minutes, and seconds
var time = Time.get_time_dict_from_system()
# we can use format strings to pad it to a length of 2 with zeros, e.g. 01:20:12
print("%02d:%02d:%02d" % [time.hour, time.minute, time.second])
For older versions of Godot (-3.4), OS.get_time() also works but in 3.5+ it's deprecated and 4.x it's removed:

Related

How i can turn datetime to like "one minute age" in prisma node js graphql?

I'm trying to set the date time in Prisma migrate to human like one minute age, one month ago how I can manage to do it?
You can use the Internationalization API Intl to do just that.
It would look something like:
const formatter = new Intl.RelativeTimeFormat('en') // specify language here
console.log(formatter.format(-5, 'minutes')) // 5 minutes ago
console.log(formatter.format(5, 'months')) // in 5 months
Note that you need to specify the amount of time in English but if you change the language of the formatter, it will adapt the output to the specified language.
Of course you first need to calculate the amount of time first, but that shouldn't be difficult :)

Discord.py send dynamic time string

I'm currently working on a Bot where users can ask to play a game and set when to play it:
.play Valorant #30 -> "#anyone_who_is_playing_valorant, up for a round in 30 minutes?"
Currently, I'm editing the message every minute to display the correct time left, which unfortunately makes implementing other commands and functions way more difficult as it should be (while loop etc).
My only solution would be to display the time at which 30 minutes would be passed:
Message written at 17:00 -> "#anyone_who_is_playing_valorant, up for a round at 17:30?"
but due to timezones the displayed timestring has to be different for each user.
Actually, Discord supports such a function for embeds, but I haven't found a standart support for sending dynamic timestrings. I would imagine it to be like Pings, where there is no function but a syntax for it: <#!authorid>
Is there a way to send dynamic timestrings?
There is no dynamic time syntax in Discord messaging, the only such functionality is the timestamp in embeds as you mentioned.
Actually there is a way to have a dynamic string in your message but it does not work for the footer in the embed.
You only need to make a unix code,
from datetime import datetime
import calendar
date = datetime.utcnow()
utc_time = calendar.timegm(date.utctimetuple())
print(utc_time)
your output looks something like this: 1657723929
You format the code to this: <t:unixcode:R>
and it gives you
7 minutes ago
You can also make a timestamp which leads to the future with just adding the secconds to the unix code you just made e.g.:
this code <t:2665824929:R> represents
in 32 years
depends on when you see this because its dynamic

why is output of date function on node.js server wrong?

When date was 2018-03-21 19:40, i tried following code
var date = new Date();
console.log(date);
Output :
2018-03-21T16:40:53.755Z
Server is missing for 3 hours as you see. I fixed it by adding 3 hours but I think it's not a good way. How can i fix this problem with better way ?
I don't think the date is incorrect, if you look closely at the format it is being printed, it has a Z at the end, which means:
A suffix which, when applied to a time, denotes a UTC offset of 00:00;
often spoken "Zulu" from the ICAO phonetic alphabet representation of
the letter "Z".
I guess you are in a place separated by 3 hours from UTC.
Node.js uses this format to print Date objects by default, but you can print your local time using toLocaleString():
console.log(date.toLocaleString());
Your server is most likely in another time zone.

Print duration of every step in terminal, during watir-cucumber execution

Is there a standard way of printing the duration of every step in the terminal, during a watir-cucumber execution?
It might be possible to make some kind of invention, using the "after step" and a global variable. But, there is a proper way?
You can use hooks like this:
Before do
#last_time = Time.new
end
AfterStep do
now = Time.new
puts "Step duration: #{((now - #last_time)*100).round} ms" # if (now-#last_time) > 10
#last_time = now
end
This is more verbose by outputting the duration on a new line, but unless you want to get more in-depth (by creating your own formatter), you cannot output stuff where you indicated it.
Uncomment the if if you are only interested in steps that take longer than a certain threshold.
The simplest way to do this would be using Ruby's built in Time class. Using Time.new we can record the current time into a local variable before and after the code within the step definition is executed. Then we work out the duration by subtracting start_time from end_time.
#get the start time
start_time = Time.new
#do whatever the step needs to do here
#get the end time
end_time = Time. new
#calculate duration
duration = end_time - start_time
puts duration

Wildcards in Assertion on groovy-generated dates?

We are using SoapUI to test a Webservice of ours. This Webservice generates different Timestamps after a predictable pattern, so we use a groovy script to generate them on the SoapUI side and use these in assertions to assert the correct calculation on the webserver side. So far so good, it works like a charm on dates (pattern yyyy-MM-dd) and Timestamps with fixed hours / minutes (pattern yyyy-MM-dd'T'HH:mm':00.000'). We use variations of this script to do this, sometimes adding days, months, hours and so forth:
def sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
Calendar instance = Calendar.getInstance();
instance.setTime(new Date());
Date date= instance.getTime();
s = sdf.format(date)
return s
===
Now I run into the following problem: This new project generates exact timestamps, down to the millisecond. Problem is, I cannot accuratly predict this, SoapUI generates the dates slightly earlier (thats no surprise, just a fact) than the webservice, and so there is a discrepancy of around 100 to 500 milliseconds between the timestamps.
I tried to add wildcards (*) (and enabled them, too) into this pattern to ignore the millisecond part, since it was deemed uneccessary as long as the second is right, but I guess they don't work within groovy scripts. Is there a way to make them work? Are there other approaches to check wether the timestamp down to the second is correct, and then ignore the milliseconds?
This is very easy! In a Script Assertion find the difference between the date you get back and "now", and assert that the difference is less than some tolerance.
def nowDate = new Date()
def sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
def currentDate = sdf.parse(context.expand('${getCurrentDateTime#ResponseAsXml#//*:current}'))
def minutesDiff = Math.abs(nowDate.getTime() - currentDate.getTime()) / 1000 / 60
assert minutesDiff < 5 : "Too great of a difference with server!"

Resources