Access current date and time at execution time

Dear GAMS experts,

Is there a way to retrieve the current date and time at execution time? I would like to write it to an individual output file in a loop at execution time, however, I can only find system.date and system.Time which do not change over time.

Thank you!

Hi,

The jnow functions returns the current time as serial number. An overview about the time and calendar functions in GAMS can be found here. And here you can find an example how they can be used.

Hope that helps,

Lutz

1 Like

Hi Lutz,

thank you very much for the quick answer! That seems to be exactly what I need. However, I’m having trouble writing this to a file in a meaningful way. When I try this:

Parameter i / 4 /;

Scalar now;

now = jnow;

File runtime /runtime.txt /;

runtime.pc = 5;

putclose runtime gyear(now) “-” gmonth(now) “-” gday(now) “GAMS” i;

runtime.txt contains the following:

2025.00,“-”,10.00,“-”,27.00,“GAMS”,4.00

where I would like to have

2025-10-27,“GAMS”,4.00

Do you now how to achieve this? Thank you!

Parameter i / 4 /;

Scalar now;

now = jnow;

File runtime /runtime.txt /;

putclose runtime gyear(now):0:0 "-" gmonth(now):0:0 "-" gday(now):0:0 ',"GAMS",' i:0;

Does this work for you?

Yes, perfect. Thank you!

Hi Lutz,

one (hopefully last) question: is there a way to write the leading zero?

2025-10-30 01:05:29

instead of

2025-10-30 1:5:29

Thanks!

There is a way, but none that is available as direct function. One basically has to program this, since GAMS allows for white padding at the beginning of a number in put statements (e.g. put m:2:0; to have a width of 2 for the scalar m), but not for zero padding. Here is a solution using a macro:


File fout / 'formatted_time.txt' /;

Scalar current_hour, current_minute, current_second;

current_hour   = ghour(jnow);
current_minute = gminute(jnow);
current_second = gsecond(jnow);

* Check if < 10 for each element and print '0' if not
$macro formattedTime(h,m,s) if(h < 10, Put '0';); Put h:0:0, ':'; if(m < 10, Put '0';); Put m:0:0, ':'; if (s < 10, Put '0';); Put s:0:0;

Put fout;
Put / 'The current time is: ';
formattedTime(current_hour,current_minute,current_second)
PutClose;

Cool, thank you for your help!