Get current time in Unix Timestamp Kotlin? - kotlin

How can I get current time in Unix Timestamp format in kotlin as integer?

If you're using Kotlin on the JVM or Android, it's as simple as
val unixTime = System.currentTimeMillis();
for the unix time in milliseconds, or
val unixTime = System.currentTimeMillis() / 1000;
for the unix time in seconds (rounded down).

Related

Create a variable to count from 1 to n in AnyLogic

I am looking to add a variable to count from 1 to 217 every hour in AnyLogic, in order to use as a choice condition to set a parameters row reference.
I am assuming I either need to use an event or a state chart however I am really struggling with the exact and cannot find anything online.
If you have any tips please let me know, any help would be appreciated
Thank you,
Tash
A state machine isn't necessary in this case as this can be achieve using a calculation or a timed event. AnyLogic has time() function which returns time since model start as a double in model time units of measurements.
For example: if model time units is seconds and it has been running for 2hr 2min 10sec then time(SECOND) will return 7330.0 (it is always a double value). 1/217th of an hour corresponds to about 3600/217 = 16.58 seconds. Also, java has a handy function Math.floor() which rounds down a double value, so Math.floor(8.37) = 8.0.
Assembling it all together:
// how many full hours have elapsed from the start of the model
double fullHrsFromStart = Math.floor(time(HOUR));
// how many seconds have elapsed in the current model hour
double secondsInCurrentHour = time(SECOND) - fullHrsFromStart * 3600.0;
// how many full 16.58 (1/217th of an hour) intervals have elapsed
int fullIntervals = (int)(secondsInCurrentHour / 16.58);
This can be packaged into a function and called any time and it is pretty fast.
Alternatively: an Event can be created which increments some count by 1 every 16.58 seconds and ten resets it back to 0 when the count reaches 217.

WAIT UP TO <milliseconds> in ABAP

According to ABAP Documentation, the command WAIT UP TO x SECONDS needs an operand of type i. However, I'd like to WAIT UP TO x Milliseconds or something similar. Neither official documentation nor several other forum posts have been helpful thus far.
Is there any way to specify a wait for a fraction of a second?
You can simply pass a decimal value like:
WAIT UP TO '0.5' SECONDS
or something like:
WAIT UP TO '0.01' SECONDS
See also How to make an abap program pause.
If you want to avoid implicit commit with WAIT UP TO, create a simple RFC function:
FUNCTION ZSLEEP .
*"--------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*" IMPORTING
*" VALUE(DURATION) TYPE SDURATION_SECONDS
*"--------------------------------------------------------------------
* To wait 50 milliseconds write this:
* DATA duration TYPE sduration_seconds VALUE '0.050'.
* CALL FUNCTION 'ZSLEEP' DESTINATION 'NONE' KEEPING LOGICAL UNIT OF WORK EXPORTING duration = duration.
WAIT UP TO duration SECONDS.
ENDFUNCTION.
I've just solved it like this:
DATA: timestart TYPE timestampl,
timeend TYPE timestampl,
millisecs TYPE timestampl,
imilli TYPE i VALUE 200.
GET TIME STAMP FIELD timestart.
millisecs = imilli / 1000.
timestart = timestart + millisecs.
DO.
GET TIME STAMP FIELD timeend.
IF timestart < timeend.
EXIT.
ENDIF.
ENDDO.
WRITE timeend.
If I now rewrite this as a function taking an integer as an import parameter (in place of imilli) I'll - to my knowledge - have exactly what I wanted.
I'll leave this up for a little before tagging it as the correct answer in the hopes that someone may have a better / more elegant solution.
Without asking about the requirement, 2 ways to do this are
GET RUN TIME
where SET RUN TIME CLOCK RESOLUTION can be important.
or
GET TIME STAMP using a target field TIMESTAMPL
Do not use WAIT UP TO for fine time frames due to the Workprocess switching.
Wait also carries other side effects not immediately obvious.

MPMusicPlayerController currentPlaybackTime - Milliseconds

Is there any way que retrieve from MPMusicPlayerController the elapsed time os a second in milliseconds?
The currentPlaybackTime returns seconds and this doesn't work for me, thanks.
The currentPlaybackTime returns seconds
Yes, but it returns those seconds as a Double (a TimeInterval). So it has a fractional part. It is giving you milliseconds; they are the stuff after the decimal point.

TimeZoneInfo which was defined by local machine not found by local machine?

I'm having trouble with timezones, a class I've built finds the local timezone of the user using:
Dim Timezone As String = TimeZoneInfo.Local.ToString
This is then stored in a MySQL DB.
When I pull the timezone, I compare it once again with the local timezone of the user to convert the time to the local timezone:
Dim D_0 As DateTime
Dim D_1 As DateTime
Dim Event_Timezone As TimeZoneInfo
Dim User_Timezone As TimeZoneInfo
Event_Timezone = TimeZoneInfo.FindSystemTimeZoneById(U_1(5).ToString)
User_Timezone = TimeZoneInfo.Local()
D_0 = TimeZoneInfo.ConvertTime(U_1(i + 4), Event_Timezone, User_Timezone)
D_1 = TimeZoneInfo.ConvertTime(U_1(i + 8), Event_Timezone, User_Timezone)
This returns the following error:
The time zone ID '(UTC-05:00) Eastern Time (US & Canada)' was not found on the local computer.
This is a confusing error because this is the timezone the local computer specified only seconds earlier. It works with nearly every other timezone. Is there a better way I should be doing this? Does anyone know why a timezone defined by the local machine is not found by the local machine seconds later?
You're calling ToString() on TimeZoneInfo - that doesn't give the ID, it gives the display name. Often they're the same in English cultures, but they don't have to be, and usually won't be in non-English cultures.
Basically you should persist TimeZoneInfo.Local.Id instead of TimeZoneInfo.Local.ToString().
(Note that using the Windows system time zone identifiers pins you down to Windows pretty heavily. You might want to consider using TZDB time zone information instead, e.g. via my Noda Time project. That's a separate decision though.)

Last Time Stamp not within valid period

Am rather newbie to Objective C (from Java background):
I got the following code to check if the last time stamp are within the minimum period, how do I do the opposite? E.g. I want to check if the last time stamp is NOT within a valid period, the valid period are in seconds.
[lastTimestamp timeIntervalSinceNow] > -MINIMUM_TIME
This seems too obvious:
if ([lastTimestamp timeIntervalSinceNow] <= -MINIMUM_TIME)
{
// execute some code
}
Where MINIMUM_TIME is a NSTimeInterval variable, representing the time you want to check against.