Jump to content
Macro Express Forums

Integer seconds to yyyymmdd-hhmmss ?


Recommended Posts

Before I spend hours writing this I thought I'd see if anyone has something already made that they would be happy to share please?

I want to add or subtract a time in say the text form hh:mm:ss to an existing text string yyyymmdd-hhmmss to get the resultant text string YYYYMMDD-HHMMSS.

I can simply convert my delta to seconds and add or subtract it, to get the new integer seconds value.

So I'd much appreciate any ready-made macros for both conversions: TO seconds and FROM seconds. Ending up with a revised text string yyyymmdd-hhmmss.

Of course, it would be trivial if I could always be sure my arithmetic stayed within a particular day, but I can't!

 

Terry, East Grinstead, UK

 

Link to comment
Share on other sites

I don't have a ready made solution, but when dealing with these kinds of scripting projects, I have found the "Date/Time" dialog tremendously and surprisingly useful.

 

For example, I managed to make a script that calculates the date six months from now and outputs it. But if the future date is a Friday, the script changes the date to the previous Thursday. If the date happens to be a Saturday, Sunday or Monday, the script changes the date to the next Tuesday.

My script looks something like this. Hope it inspires a simple solution.

 


// Extract the day of the week, six months from today
Date/Time: Set %DayofWeek% to an adjusted date/time using "dddd" as the format // Text Variable
 
// Adjust target date (I don't book appointments on Mondays or Fridays)
Delay: 100 milliseconds // A delay is needed somewhere… Not sure why.
Switch( %DayofWeek% )
Case: Friday // Friday --> Previous Thursday
  Variable Set Integer %NumberofDays% to -1
End Case
Case: Monday // Monday --> Next Tuesday
  Variable Set Integer %NumberofDays% to 1
End Case
Case: Saturday
  Variable Set Integer %NumberofDays% to 3
End Case
Case: Sunday
  Variable Set Integer %NumberofDays% to 2
End Case
Default Case
  Variable Set Integer %NumberofDays% to 0
End Case
End Switch
 
// Insert new date
Date/Time: Type out an adjusted date/time using "dd-MMM-YYYY" as the format

 

Link to comment
Share on other sites

Thanks. I agree that the built-in Date/Time functions can be very useful. But IMO they seem most relevant for work which starts with either the current or some manually entered date and time. The hard work of getting a text string to and from the Date/Time commands, programmatically not with a user dialog, is notable by its absence.

This inability to convert a text variable to a date/time variable seems to be a long standing weakness in MX Pro's repertoire. I see it was raised at least eight years ago here, and I note Cory's hope "... I think ISS realizes these shortcoming and may be addressing this shortcoming soon."

 

Link to comment
Share on other sites

I thought I'd made a breakthrough by venturing into the tricky world of 'dynamic macros'. I used a command I'd not given much attention to before: Load Macro Text File.

I first proved I understood it by making a text file TestSimple.txt containing the code from a simple command like TextBox Display, Program Launch, etc, and then running a macro that included Load Macro Text File: TestSimple.txt

I then saved the code from the Date/Time command in the file TestDateWork.txt and included editing and re-saving that within my macro before using the command
Load Macro Text File: TestDateWork.txt

I followed that with a Text Display Box expecting to see the revised content of my variable tTime. But the macro stopped on or directly after the Load Macro Text File, no error message, no text box.

 

 

Link to comment
Share on other sites

I've also just found the attached very promising macro in my archives. Not sure of its source. But sadly it fails with messages like "Line 40: Debug Error: Undefined variable or the variable is the wrong type"

The variable is:

Variable Modify String %tTarget[0]%: Convert to Integer (%nTarget[0]%)

As an aside, how do I interpret that? Shouldn't an array variable index start at [1] not zero?

Also uploaded the MEX here, which should download automatically wihout user intervention:

https://www.dropbox.com/s/3g6qzxahnfx5t00/Date_String_to_Date_Variable_and_Decimal_Time.mex?dl=1

 

Date_String_to_Date_Variable_and_Decimal_Time.mex

Link to comment
Share on other sites

10 hours ago, Cory said:

Terry I downloaded your file but is was empty

Sorry. Dropbox made drastic changes to Public folder links recently causing problems like this for tens of thousands of users. I've now edited the post and seems OK.

Hopy you (or anyone) can identify the problem. I expect it will prove embarrassingly obvious! From its style, it looks like an Insight macro I've downloaded. So you may find you already have it.

Link to comment
Share on other sites

41 minutes ago, Cory said:

I downloaded both links again but I still don't see a macro inside. 

Not sure what steps you're taking but works fine here. This is what I see.

https://www.dropbox.com/s/7y1hamlarrxdlm1/CoryDownloadProblem.jpg?dl=0

Also sent to my wife in an email and she sees similar.

Maybe this is a consequence of the radical Dropbox Public folder change I mentioned. Could you screenshot what you see please?

Also try this third alternative and see if it appears in you downloads folder. Failing that I'll email it until I discover what's going wrong.

https://dl.dropboxusercontent.com/s/3g6qzxahnfx5t00/Date_String_to_Date_Variable_and_Decimal_Time.mex?dl=1

 

CoryDownloadProblem.thumb.jpg.a3027c23976cc8c58586487b1302dfa5.jpg in her different browser. Did you look in your Downloads folder?

Link to comment
Share on other sites

1 hour ago, Samrae said:

Terry, Why don't you just attach the files to the forum post? I see "Drag files here to attach, or choose files..." below.

 

That's what I did! But as you see it displayed the link because it can't be opened directly. How about this screenshot below, which I've again attached using the normal drag method. Looks fine here. Can non-Dropbox use see it?

 

DownloadProblem-2.thumb.jpg.5974429c9bcb11ce2a027097e6a40d17.jpg

Link to comment
Share on other sites

I get this. And here is a screenshot of the file properties. I'm not having any problem downloading it. And MEP sees the file as a valid MEX file. There's simply nothing in it. 63,918 bytes so it seems there should be something in it. I made an empty MEX just now and it's only 60 bytes. You might try to "Pack" command in the Tools menu. 

I don think you have attached the file as Samrae suggests. You're inserting links. See in this post how something is inserted. 

Test.mex

Link to comment
Share on other sites

Terry, I'm sorry. You did use the attachment link. I don't know why it would refer to dropbox, however.

Like Cory, I downloaded the file from the link and opened it in Macro Express Pro. There were no macros in the file. I then packed the file and the size was set to 60 bytes. That is the size of an empty macro file.

Maybe it will work better if you copy it from the dropbox link to a folder on your hard drive and then attempt to attach it from there.

Link to comment
Share on other sites

Thanks Sam.

My bad! I gave the wrong link. That gives the result you and Cory found. (I'll aim to test the many others I have that I think were downloaded at the same time.)

The correct macro that gives the problem I described is called Date String to Date Variable and hopefully it's attached! Keen to get it working if possible.

I''ll correct the original post too.

--------------------

Apologies for going OT, but I'm still trying to establish what happens to my new Dropbox links in forums that I use for those who do not have Dropbox accounts. (Do you?) Anyway, here are four different links to that same valid file, and I'd much appreciate feedback on them please.

#1
https://www.dropbox.com/s/2xetvq1ie0a7bau/Date String to Date Variable.mex?dl=0
Here, this opens in my browser (Firefox), tells me it cannot be previewed, and - after a couple of seconds when it's presumably checking whether I have a DB account- it offers a Download button.

#2
https://www.dropbox.com/s/2xetvq1ie0a7bau/Date String to Date Variable.mex?dl=1
Here, this automatically downloads it to my Downloads folder, with no user intervention.

#3
https://www.dropbox.com/s/2xetvq1ie0a7bau/Date String to Date Variable.mex?raw=1
Here, this does exactly the same as #1

#4
https://dl.dropboxusercontent.com/s/2xetvq1ie0a7bau/Date String to Date Variable.mex?dl=0
Here, this opens the content of the MEX in Firefox. If I select it, copy/paste to a text editor and save as an MEX, it runs OK.

 

Date String to Date Variable.mex

Link to comment
Share on other sites

I can simulate not having a DropBox account with Incognito in Chrome. You can too. I think it's called Private in FireFox.

  1. I see the same thing you describe and can optionally do the "Direct Download".
  2. It doesn't direct download. I get the same as #1. 
  3. Same as #1 again.
  4. This time it doesn't prompt me and I the file automatically downloads. I don't see anything of DropBox.
Link to comment
Share on other sites

1 hour ago, Cory said:

The problem with your macro is that array variables in MEP are not zero based. IE unlike normal programming languages, they start at 1.

Thanks Cory. As you saw in my Saturday post, that was my first suspect. But it's odd. The macro uses proper variable names, introduced in ME Pro. Can't see how it can ever have worked if zero is not allowed?

I just tried changing all five [0] strings to [1].  That got to the TBD at line 161, giving an incorrect warning: "Date entered is not in correct format mm/dd/yyyy
or is invalid, Macro will Terminate"
for any date entry!

Anyway, this tempers my enthusiasm for using this to help with the macro I described, if significant debugging is likely,

 

Link to comment
Share on other sites

2 hours ago, Cory said:

I can simulate not having a DropBox account with Incognito in Chrome. You can too. I think it's called Private in FireFox.

  1. I see the same thing you describe and can optionally do the "Direct Download".
  2. It doesn't direct download. I get the same as #1. 
  3. Same as #1 again.
  4. This time it doesn't prompt me and I the file automatically downloads. I don't see anything of DropBox.

Thanks Cory. Hoping for feedback from a non-DB user now! But I'll investigate your suggestions about simulating it.

Link to comment
Share on other sites

I'm very behind with work and don't have time. Can you make a simple macro that demonstrates the problem. Also instead of using a dialog box to prompt the user, can you just set a text variable? I'm thinking it should be a 2 line macro.

Link to comment
Share on other sites

30 minutes ago, Cory said:

I'm very behind with work and don't have time. Can you make a simple macro that demonstrates the problem. Also instead of using a dialog box to prompt the user, can you just set a text variable? I'm thinking it should be a 2 line macro.

By 'the problem" do you mean my original objective: "...to add or subtract a time in say the text form hh:mm:ss to an existing text string yyyymmdd-hhmmss to get the resultant text string YYYYMMDD-HHMMSS"?

If so I'd be very impressed to see a two line macro that does it!

I'm just putting the finishing touches to mine now: 114 lines. It includes a Repeat for applying it to a set of successive filenames which include a prefix in that form and has future scope (with variables set up) for 'adding' years, months, days, hours, minutes and seconds. It edits the originals in situ, in their File Explorer folder. It uses Run Macro in Variable, as I wanted to learn how to do that with Date/Time. I'm sure a more direct approach along the lines of my first post (working in seconds with conversions from and to text) would be shorter ... but not that short!

Note that I gave up on trying to use existing macros for this exercise. The PGM macros looked promising but I could get none working, and as I mentioned in that thread I now hear it's a known issue, encountered by others. And the many others I tried are either buggy or 'empty' or corrupted, with no macro displayed once the MEX is opened in MEP. So I got stuck in and wrote my own.

BTW, could you use Quote in your replies please Cory, to minimise ambiguity in long threads like this.

Link to comment
Share on other sites

This isn't two lines, but it's not many:

Macro builds a text file containing the VBScript command DateAdd, with the appropriate number of seconds added to the date/time that the macro provides.  
For example: MsgBox(DateAdd("s",30,"31-Jan-10 08:50:00"))
adds 30 seconds to the starting date/time and displays it in a pop-up message box.  That one line is the entire text file that the macro must build.  

Macro saves the text file as testscr.vbs

Macro  uses the Program Launch command to run wscript.exe, passing it “testscr.vbs” as its parameter.  

This can be done easily with ME.  It has the advantage that Microsoft’s VB Script language does all the date calculations for you – no small matter when you consider leap years, leap centuries, additions that take you from one month or one year to the next, and so on.  

I have used MsgBox for my example, and you can use it for testing.  But for your live macro you will probably want to find a different VBS function that sends the result to another text file where you can retrieve it further down in your macro – or sends the result to some other easy-to-access location.  I don’t know what VBS function to suggest, but starting from abject ignorance (almost) I got this far in half an hour, so you can take it from here.  Keep us posted.  

 

vbs_example.jpg

vbs_example2.jpg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...