Search code examples
datebatch-filejulian-date

Trying to rename a file using variable julian date and last digit of year


I am trying to rename a file like so:

idYDDD.apl

Where: id is a string id, which is constant not worried about that. Y is the last digit of the current year DDD is the julian date, not the true julian date the julian format. (Jan 1st + days)

I have a batch file i found on here that is helping me with the Julian date conversion. But i am trying to pull out the single digit from the year.

This is how i am trying to grab the single digit:

set digit=%DATE:~12,1%

I keep getting the error

"Invalid number. Numerical constants either decimal, hexadecimal or octal."

I have a few files, The DATETOJULIAN.bat and SendAPL.BAT

Date to julian is converting the date and the SendAPL.Bat is renaming and sending the file over an FTP after copying it. Below is the Julian conversion:

REM CONVERT DATE TO JULIAN DAY NUMBER

Echo %Date%

REM GET MONTH, DAY, YEAR VALUES
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C

REM ELIMINATE LEFT ZEROS
SET /A DD=10%DD% %% 100, MM=10%MM% %% 100

REM CALCULATE JULIAN DAY NUMBER
IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10,         JDN=C+DD+E+F-1524

Below is the Send APL:

@echo off

set digit=%DATE:~12,1%



call c:\wic\DATETOJULIAN.BAT %DATE%

call c:\wic\ip.bat

copy c:\wic\download\*04.apl c:\wic\archive\%DAY%_04.apl
copy c:\wic\download\*04.apl c:\wic\download\KY%digit%%JDN%.APL

Pause 


ftp -s:C:\wic\sendapl.ftp %IP%

del c:\wic\download\*04.APL

Solution

  • It appears you have picked up batch file that obtains the real Julian day number. As it says in the description of the julian tag on stack exchange, the Julian day number is the number of whole days (integer) since noon November 24, 4714 BC, in the proleptic Gregorian calendar, universal time. Your file DATETOJULIAN.BAT does that if you give it a date in the format you expect; for example it is now 7 PM June 25, 2015. You enter 6/25/2015 as the parameter and it gives JDN = 2,457,199 which is correct. But it appears you want the day of the year, which would be 176. That's not a julian date, that's an ordinal date.

    The solution is to call DATETOJULIAN.BAT twice. First, find the JDN of December 31 of the year before the year you want and call it JDNDEC31. Then find the JDN of the date you want. Then find the ordinal date, JDN-JDNDEC31.

    If anyone else will ever read or maintain your code, you should document that you are not calculating a true Julian day number, because you are not accounting for the fact that the Julian day number always uses Greenwich time, and the Julian day begins at noon, not midnight. If you want a similar day number that uses local time and and begins at midnight, use the Lilian date, which has a Wikipedia article about it. If your file times are reported in Greenwich time, and you would like a day number that starts at midnight, use the modified julian date, which is described in the Wikipedia article about Julian day.