question

Kevin S7 avatar image
0 Likes"
Kevin S7 asked Jason Lightfoot commented

error in code when changing from date and time to seconds


Greetings community I have a problem when importing an Excel I have a date and time and I want to convert those Excel input data to seconds in my global table but it gives me very high values which should not be, what could be wrong in my code? import source

1698158740317.png


The times must go to seconds regardless of when the simulation starts running and be able to change the start date and time of the simulation.

code of import


int onreset = param(1); //1 if this code is being called on Reset of the model, 0 otherwise

Table table = Table(Model.find("llegadas (Global Table"));

DateTime ModelStart = Model.dateTime; //get model start time as a number

for (int i = 1; i <= table.numRows; i++){

string StartTime = table[i][1];

DateTime date = DateTime(StartTime, "%m/%d/%Y "); //get string as a number

table[i][1] = date - ModelStart;//calculate the difference between start time and arrival time in number of seconds

}



timw.fsm

FlexSim 23.2.1
time table
1698158740317.png (102.8 KiB)
timw.fsm (56.0 KiB)
· 4
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jordan Johnson avatar image Jordan Johnson ♦♦ commented ·
Can you post your Excel file as well?
0 Likes 0 ·
Kevin S7 avatar image Kevin S7 Jordan Johnson ♦♦ commented ·
0 Likes 0 ·
datatim2.xlsx (9.2 KiB)
Kevin S7 avatar image Kevin S7 Jordan Johnson ♦♦ commented ·

@Jordan Johnson Greetings, I just uploaded my file. I just wanted you to help me correct the code. I don't know what's wrong to import the date and time into seconds and for the time to adapt to the start time.

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦ commented ·

Hi @Kevin S7, was Jordan Johnson's answer helpful? If so, please click the "Accept" button at the bottom of their answer. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.

0 Likes 0 ·

1 Answer

·
Jordan Johnson avatar image
0 Likes"
Jordan Johnson answered Jason Lightfoot commented

In Excel, Dates and Times often look like text, but they are usually stored as numbers. If I import your excel data without changing anything, I get the following data:

1698183417193.png

Notice that all the date values are around 45000. Excel date/times are stored as the number of days since 1900.

FlexSim DateTime objects, on the other hand, count the number of seconds since 1601 (or maybe 1600, I can't recall).

So, knowing this, it is incorrect to try to use the text of the Excel data. Instead, you can use the number. To convert between Excel time and FlexSim time, you can use the convert() command:

DateTime date = DateTime(convert(StartTime, XL_DATETIME, FS_DATETIME));

But there are other problems as well:

  • The global table "llegadas" has a space after the name. It is best not to use trailing whitespace like that.
  • To access a global table, you just need the following syntax:
    Table table = Table("llegadas");
  • To access the start time as a date time, use
    Model.startDateTime

Those are all the issues I've found so far. See if you can get somewhere making those changes. Best of luck!


1698183417193.png (11.5 KiB)
· 9
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Kevin S7 avatar image Kevin S7 commented ·

@Jordan Johnson I enter the lines of code that you provided in the imported code section and I get an error

1698184799991.png

0 Likes 0 ·
1698184799991.png (5.0 KiB)
Felix Möhlmann avatar image Felix Möhlmann Kevin S7 commented ·

When you click "cancel" the compiler should show you where the error is in the code plus a brief description of what is wrong. (The error can be in a previous line, like a missing semicolon or parentheses, but generally you should be able to spot the mistake quite quickly with that information.)

The code Jordan provided should work though.

Table table = Table(tableName);  // placeholder name
DateTime ModelStart = Model.dateTime; //get model start time as a number for (int i = 1; i <= table.numRows; i++) {     double StartTime = table[i][1];     DateTime date = DateTime(convert(StartTime, XL_DATETIME, FS_DATETIME));     table[i][1] = date - ModelStart; }
0 Likes 0 ·
Kevin S7 avatar image Kevin S7 Felix Möhlmann commented ·

@Felix Möhlmann Greetings I just entered the code in the import code part but it doesn't work, it should give me in seconds and it gives me a very high value which is not

1698238766521.png

code1.fsm



0 Likes 0 ·
1698238766521.png (119.9 KiB)
code1.fsm (55.9 KiB)
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.