Jump to content

Date calculation


Recommended Posts

I have posted to bug report but thought I would throw this out here to see if anyone else has encountered this or can see some error in my code.

I am using v3.0.102.

I have a simple script that is scheduled to run at one minute after midnight. The script will create a folder with the previous date as the folder name (i.e. 20050102). This has been running just fine for several weeks. One January 1, the script created a folder named 20050100 instead of 20041231. On January 2, everything was fine again. Here is the script in its entirety;

; this script should be scheduled to run daily at 12:01 AM.

$folderdate = @year & @Mon & @mday

; the following will remove old files from 5 - 10 days back.  This is just in case there is
; a problem and this script doesn't run correctly for a day or two.

for $i = 10 to 5 step -1
    DirRemove("\\sjs2kcommoncl\sjsfslab$\Ltube\" & $folderdate-$i,1)
next

; create a folder with yesterday's date as the folder name

DirCreate("\\sjs2kcommoncl\sjsfslab$\Ltube\" & $folderdate-1)

; move all files from the 'Today' folder to the newly created yesterday folder

Filemove("\\sjs2kcommoncl\sjsfslab$\Ltube\today\*.*", "\\sjs2kcommoncl\sjsfslab$\Ltube\" & $folderdate-1)

Does anyone see a problem with this code or is it an AutoIt issue?

Link to comment
Share on other sites

You will have (deletion) problems during the first 10 days of the month: do a dry run where the day = 1, 2, 10, 11.

Similar problem every first of the month for the issue you raised.

:idiot:

Link to comment
Share on other sites

Why will I have problems?  Is there a problem with my code or is this a problem with AutoIt?

<{POST_SNAPBACK}>

With your logic :D ... a dry run means "pretend with these values".

So.. pretending that @day=1: when you subtract 1 from 20050101, you get 20050100 (whereas I bet you wanted to get 20041231)

:idiot:

Similarly - when you want to delete a folder 8 days old, and @day is 3 (which makes today's folder say 20050103) .. your code will end up trying to delete a folder called .. what???

Edited by trids
Link to comment
Share on other sites

It's a problem with your code.

$folderdate = @year & @Mon & @mday

This line creates a number: e.g. 20 050 101 (or: twentymillionfiftythousandonehundredone)

Now if you substract 1 from this number you get: 20 050 100.

AutoIt does exactly what it's told but you told it to do the wrong things.

Calculating with dates isn't easy in AutoIt. You first have to substract 1 from @mday. then check if it's fallen below 1. If it did, you have to also substract 1 from @mon. then check if @mon has fallen below 1. if it did, you have to also substract 1 from @year.

And after you've checked if you had to substract 1 from @mon you have to set the variable you stored @mday in to the last day of the month which can be 28, 29, 30 or 31.

Edited by sugi
Link to comment
Share on other sites

Gotch. I see the error in the logic. I need to do the math on the particular day or month and use if...then statements to get the correct folder names. I'm still in a VB mindset thinking along the lines of the dateadd() function.

Thanks

Link to comment
Share on other sites

  • Developers

Gotch.  I see the error in the logic.  I need to do the math on the particular day or month and use if...then statements to get the correct folder names.  I'm still in a VB mindset thinking along the lines of the dateadd() function.

Thanks

<{POST_SNAPBACK}>

Well maybe you should look at the UDF _DateAdd() then :idiot:

It is added in the current "Beta" ....

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I usually prefer to use Julian date format when calculating dates, but that's no easy feat either.

Since you're not using the beta version, try searching the fourms for "DateNew.au3" and see if that helps. Its a default #include for all my scripts at work because we do a lot of julian date work and I'm always transferring between Gregorian (mm/dd/yyyy) and Julian (yyyyddd).

*scratches head*

Maybe that's why I'm having so many headaches........but not because of the DateNew.au3 functions. :idiot:

Using the datenew functions to determine the age of a document in our system:

CODE

#include DateNew.au3

WinActivate($a)

MouseClick($secondary, 150, 150, 2, 0)

getscreen ($a)

; Extract the 13 digit DCN from the clipboard location

$tem = StringInStr(ClipGet(), "DCN: ")

$dcn = StringTrimLeft(StringMid(ClipGet(), $tem, 18), 5)

; Julian date of document in system

$jul = StringLeft($dcn, 4)

; Current document age

$claimjul = "200" & $jul

$today = @YEAR & "/" & @MON & "/" & @MDAY

$claimgreg = _JulianToDate ($claimjul) ; <<== Very useful

$age = _DateDiff("d", $claimgreg, $today) <<== Calculates w/o having to worr about the year gap.

Now, this code will only work for years 2000 through 2009.....but I'm not too worried about that right now....I'm sure I could modifiy it to work for the next 100 years.....

$claimjul = StringTrimLeft(@year,2) & $jul

but...I'm not living that long.

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...