China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-02 13:59
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Challenge 5]: Calculation of Time Before and After a Date [Difficulty: ☆☆] View 4,767 Replies 27
Original Poster Posted 2007-05-01 07:17 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Goal: After entering any date, calculate the time period before or after this date.

For example: Date 2007-03-31 3 is to query the date 3 days later.

Language requirements for writing: cmd (commands included in help) vbs (no cdate-like functions can be used, no system API functions can be called), no temporary files can be generated.

Limitations: No third-party tools can be used

Target audience: Those who have learned batch processing but haven't been able to apply it flexibly. I hope these people can quickly learn the calculation skills of batch processing through these examples..

Difficulty: Nested variables, addition and subtraction of dates, use of command line parameters (no parameter detection is required), overflow handling of different sizes of values.

Target points: 1 point for completing each item, 4 points for completing all four items or 4 points.

ps: Points are not the goal.. The important thing is that we can complete such challenges.

Please everyone do your best to write according to your ability: )
________________________________________________________

The next issue of the topic will be launched every Monday.

Our challenges will gradually increase in difficulty, please do it according to your ability.

Personal code is on the first floor.
————————————————————————————————————
Download of part of the content of the first three issues of the challenge

[ Last edited by flyinspace on 2007-4-30 at 06:29 PM ]
Attachments
挑战.rar (4.02 KiB, Credits to download 1 pts, Downloads: 44)
知,不觉多。不知,乃求知
Floor 2 Posted 2007-05-01 07:18 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline

All deleted


Found some bugs. ..

Will rewrite it in a few days. ..

youxi 01 and slore have better ones. Welcome everyone's use and testing.

[ Last edited by flyinspace on 2007-5-2 at 08:28 AM ]
知,不觉多。不知,乃求知
Floor 3 Posted 2007-05-01 07:39 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Re flyinspace:

Tested it and found the following problems and put forward some suggestions

1、
For example: Date 2007-03-31 3 is to query the date 3 days later.

The script name has the same name as the system internal command "Date", which causes the system date to be directly changed during testing. It is recommended to modify it to a script name like "Datex";

2、For the date format like "2007-3-28", it cannot be detected and processed normally. It is recommended to add a detection and processing mechanism;

3、In addition, put forward a suggestion. When sending more code, try to enclose the code with to facilitate others to view and copy.

[ Last edited by lxmxn on 2007-4-30 at 06:40 PM ]
Floor 4 Posted 2007-05-01 17:05 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
CDATE can't be used, sigh~ Does it need to be complicated? Then treat it as a string processing.

[ Last edited by slore on 2007-5-1 at 04:28 AM ]
Floor 5 Posted 2007-05-01 18:02 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
It's really like that. There's a simple method but can't be used. I most admire the conciseness of兄's code.
Floor 6 Posted 2007-05-01 18:50 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
vbs:
execute("msgbox #2007-03-31#+3")
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flyinspace +3 2007-05-02 17:12
Floor 7 Posted 2007-05-01 20:32 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
Originally posted by slore at 2007-5-1 18:50:
vbs:
execute("msgbox #2007-03-30#+3")


This is well used, avoiding functions and using #. Haha, wonderful
Floor 8 Posted 2007-05-01 23:34 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
vbs (No use of functions like cdate, no calling of system API functions)

It feels like it's saying: No use of fuels like gasoline, how do you start a car
Floor 9 Posted 2007-05-02 13:53 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Oh.. What I originally meant was just to use VBS to write a class by myself..

For example:

class xdata
{
int Year;
int Month;
int Day;
AddDay();
……
};

Something similar to this function......

Who knew that someone came up with another way.. (Roughly looked at the VBS tutorial. It seems that VBS can write its own classes..)
知,不觉多。不知,乃求知
Floor 10 Posted 2007-05-02 13:54 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by zh159 at 2007-5-1 10:34 AM:

It feels like saying: Do not use fuel such as gasoline, how do you start the car

This statement of mine is not rigorous enough... Change the statement (Do not call time-related APIs)
知,不觉多。不知,乃求知
Floor 11 Posted 2007-05-02 17:12 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
VBS can write classes:


'------------------------Define class---------------------
Class MyClass
'-----------------Define variables used by the class-------------
Private class_name
'---------------------Class initialization process---------------
Private Sub Class_Initialize()
class_name="MyClass"
End Sub
'-----------------------Get class property-------------------
Public Property Get MyName
MyName = class_name
End Property
'-----------------------Write class property-------------------
Public Property Let MyName(ByVal strName)
class_name = strName
End Property
'-----------------------Define class procedure-------------------
Public Sub CheckName()
If Me.MyName = "MyClass" Then
MsgBox "Currently is the default name",,"Status"
Else
MsgBox "Name has been modified to:" & Me.MyName,,"Status"
End If
End Sub
'-----------------------Class termination process-------------------
Private Sub Class_Terminate()
End Sub
End Class
'-----------------------Class definition completed-------------------
'
'
'
'----------Simple process to call the class---------------
Dim X
Set X = New MyClass
MsgBox X.MyName
X.CheckName
X.MyName = "I am a new name"
X.CheckName
MsgBox X.MyName
Set X = Nothing
'----------------Call completed-----------------


Haha, but I don't know how to apply the class...
P is too slow, tried calculating 8000 days directly.....
Floor 12 Posted 2007-05-02 17:20 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by slore at 2007-5-2 04:12 AM:
VBS can write classes:



Haha, but I don't know how to apply the class...
P is too slow, tried to calculate 8000 days directly.....


Thanks for pointing out the problem...

In the next version, the query days problem will be judged.. So it can calculate the fastest.

For days greater than 30, subtract by months.
For days greater than one year, judge whether it is a leap year and then subtract..

This can be much faster.
知,不觉多。不知,乃求知
Floor 13 Posted 2007-05-02 17:22 ·  中国 广东 清远 英德市 电信
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
In fact, the old moderator willsort has already written the corresponding code. However, so far, I still haven't figured out the relevant calculation principle.

Using this code, the requirements related to the challenge question can be written.

@echo off
title Date Query
color 1f
if == call :Help
call :Date2Day %1 days
set/a days=%days%+%2
call :Day2Date %days% date
echo.
echo -----------Result---------------
echo.
echo The date you inquired is: %date%
echo ------------------------------
cmd /k

:Date2Day
setlocal ENABLEEXTENSIONS
for /f "tokens=1-3 delims=/-, " %%a in ('echo/%1') do (
set yy=%%a & set mm=%%b & set dd=%%c
)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
endlocal&set %2=%j%&goto :EOF

:Day2Date
setlocal ENABLEEXTENSIONS
set /a i=%1,a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
endlocal&set %2=%yy%-%mm%-%dd%&goto :EOF

:Help
cls
echo ________________________________________________
echo Usage: %~nx0
echo.
echo Date format: 2007-03-31
echo Days format: +/- Days to query
echo Example: %~nx0 2007-03-31 3 is to query the date 3 days later
echo ________________________________________________
echo.
cmd /k
Floor 14 Posted 2007-05-02 17:34 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by youxi01 at 2007-5-2 04:22 AM:
In fact, the old moderator willsort has already written the corresponding code. However, so far, I still haven't figured out the related calculation principle.

Using this code, the requirements related to the challenge question can be written. ...

The algorithm is very simple. ..

It is also smarter than mine... But because the existence of leap years is not considered. ..

Problems will occur when calculating longer days...

If the judgment of leap years is added, it will be much better.
知,不觉多。不知,乃求知
Floor 15 Posted 2007-05-02 17:47 ·  中国 广东 清远 英德市 电信
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Didn't consider leap years, have you tested it?
Why does my test "feel" that it has already considered leap years?!
In fact, it must have considered it. As for the calculation principle, I really don't know.
Forum Jump: