| 
 
flyinspace 
银牌会员
 
     
 
  
  
积分 1206 
发帖 517 
注册 2007-3-25 
状态 离线
 | 
『楼 主』:
 [挑战5]:日期的前后时间计算 [难度:☆☆]
 
使用 LLM 解释/回答一下
  
目标:输入任意日期后,计算与这个日期之前或之后的时间段。。 
 
例如:Date 2007-03-31 3 就是查询3天后的日期。 
 
编写语言要求: cmd (help 包含的命令) vbs (不得使用 cdate 类似的函数,不得调用系统 
 
api函数),不得生成临时文件。 
限制:不得使用第三方工具 
 
针对目标人群:学会了批处理,但还不会活学活用的人。。希望这些人通过这里的例子能迅 
 
速学会批处理的计算技巧。。 
 
难点:变量的嵌套,日期的加减,命令行参数的使用(不需要做参数检测),不同大小的数 
 
值的溢出处理。。 
 
目标积分:每完成一样获得积分1,四项全完成或积分4。。 
 
ps : 积分不是目的。。重要的是,我们可以完成这样的挑战。。 
 
请大家根据自己的能力尽力编写吧:) 
________________________________________________________ 
 
下一期的题目 会在每周 一的时间推出。。 
 
     我们的挑战会逐渐增加难度,请大家量力而行。。 
 
个人代码见一楼。 
———————————————————————————————————— 
挑战前三期部分内容下载 
 
 Last edited by flyinspace on 2007-4-30 at 06:29 PM ] 
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 ] 
    
 
  
附件
1:   挑战.rar (2007-5-1 07:28, 4.02 KiB, 下载附件所需积分 1 点
,下载次数: 44)
 
 
  |  
                  
  
                    知,不觉多。不知,乃求知 |   
 | 
  2007-5-1 07:17 | 
  
 | 
 | 
 
flyinspace 
银牌会员
 
     
 
  
  
积分 1206 
发帖 517 
注册 2007-3-25 
状态 离线
 | 
『第 2 楼』:
 修正 1
 
使用 LLM 解释/回答一下
  
 
全删掉了 
 
发现部分 BUG 。。
 
过几天重新写过。。
 
youxi 01 和 slore 有更好的。欢迎大家的使用和测试。
 
 Last edited by flyinspace on 2007-5-2 at 08:28 AM ]  
 
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 ]  
    
 
  
  |  
                  
  
                    知,不觉多。不知,乃求知 |   
 | 
  2007-5-1 07:18 | 
  
 | 
 | 
 
lxmxn 
版主
 
        
 
  
 
积分 11386 
发帖 4938 
注册 2006-7-23 
状态 离线
 | 
『第 3 楼』:
 
 
使用 LLM 解释/回答一下
  
Re flyinspace: 
测试了一下,发现如下几个问题并提点建议
 
1、
 例如:Date 2007-03-31 3 就是查询3天后的日期。 
脚本名称与系统内部命令“Date”重名,导致测试时直接将系统日期改变,建议修改为类似"Datex"之类的脚本名称;
 
2、对于像“2007- 3-28”格式的日期还无法正常的检测和处理,建议增加检测处理机制一下;
 
3、另外,提点建议。发比较多的代码时,尽量用将代码括起来,便于别人查看和复制。
 
 Last edited by lxmxn on 2007-4-30 at 06:40 PM ]  
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 ]  
    
 
  
 |   
 | 
  2007-5-1 07:39 | 
  
 | 
 | 
 
slore 
铂金会员
 
       
 
  
  
积分 5212 
发帖 2478 
注册 2007-2-8 
状态 离线
 | 
『第 4 楼』:
 
 
使用 LLM 解释/回答一下
  
CDATE不让用汗~要复杂化么?那就当字符串处理 
 
 Last edited by slore on 2007-5-1 at 04:28 AM ] 
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 ] 
    
 
  
 |   
 | 
  2007-5-1 17:05 | 
  
 | 
 | 
 
baomaboy 
银牌会员
 
     
 
  
  
积分 1513 
发帖 554 
注册 2005-12-30 
状态 离线
 | 
『第 5 楼』:
 
 
使用 LLM 解释/回答一下
  
真是啊 有简单的方法不能用啊,最佩服slore兄代码的简练之处了. 
It's really like that. There's a simple method but can't be used. I most admire the conciseness of兄's code. 
    
 
  
  |  
                  
  
                    好多菩提树,好多明镜台。本来好多物,好多的尘埃。 |   
 | 
  2007-5-1 18:02 | 
  
 | 
 | 
 
slore 
铂金会员
 
       
 
  
  
积分 5212 
发帖 2478 
注册 2007-2-8 
状态 离线
 | 
  『第 6 楼』:
 
 
使用 LLM 解释/回答一下
  
vbs: 
execute("msgbox #2007-03-31#+3") 
vbs: 
execute("msgbox #2007-03-31#+3") 
    
 
 
 
  
 |   
 | 
  2007-5-1 18:50 | 
  
 | 
 | 
 
baomaboy 
银牌会员
 
     
 
  
  
积分 1513 
发帖 554 
注册 2005-12-30 
状态 离线
 | 
『第 7 楼』:
 
 
使用 LLM 解释/回答一下
  
Originally posted by slore at 2007-5-1 18:50: 
vbs: 
execute("msgbox #2007-03-31#+3")  
这个用的好,避开函数,利用了# 哈哈 妙  
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  
    
 
  
  |  
                  
  
                    好多菩提树,好多明镜台。本来好多物,好多的尘埃。 |   
 | 
  2007-5-1 20:32 | 
  
 | 
 | 
 
zh159 
金牌会员
 
      
 
  
 
积分 3687 
发帖 1467 
注册 2005-8-8 
状态 离线
 | 
『第 8 楼』:
 
 
使用 LLM 解释/回答一下
  
vbs (不得使用 cdate 类似的函数,不得调用系统api函数) 
感觉像是说:不得使用汽油等燃料,你如何开动汽车  
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  
    
 
  
 |   
 | 
  2007-5-1 23:34 | 
  
 | 
 | 
 
flyinspace 
银牌会员
 
     
 
  
  
积分 1206 
发帖 517 
注册 2007-3-25 
状态 离线
 | 
『第 9 楼』:
 
 
使用 LLM 解释/回答一下
  
噢。。我本来的意思只是说 用 vbs 的自己编写个类。。 
 
例如: 
 
class xdata 
{ 
         int Year; 
         int Month; 
         int Day; 
         AddDay(); 
         …… 
}; 
 
类似的功能的。。。。。 
 
      谁知道。居然有人想出了另外的法子。。(粗略看了一下vbs的教程。好象 vbs可以自己编写类吧。。) 
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..) 
    
 
  
  |  
                  
  
                    知,不觉多。不知,乃求知 |   
 | 
  2007-5-2 13:53 | 
  
 | 
 | 
 
flyinspace 
银牌会员
 
     
 
  
  
积分 1206 
发帖 517 
注册 2007-3-25 
状态 离线
 | 
『第 10 楼』:
 
 
使用 LLM 解释/回答一下
  
Originally posted by zh159 at 2007-5-1 10:34 AM: 
 
感觉像是说:不得使用汽油等燃料,你如何开动汽车  
这个我的说法不够严谨。。。换个说法(不得调用与时间相关的api)  
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)  
    
 
  
  |  
                  
  
                    知,不觉多。不知,乃求知 |   
 | 
  2007-5-2 13:54 | 
  
 | 
 | 
 
slore 
铂金会员
 
       
 
  
  
积分 5212 
发帖 2478 
注册 2007-2-8 
状态 离线
 | 
『第 11 楼』:
 
 
使用 LLM 解释/回答一下
  
VBS可以写类: 
 
'------------------------定义类--------------------- 
Class MyClass 
'-----------------定义类使用的变量------------- 
Private class_name 
'---------------------类初始化过程--------------- 
Private Sub Class_Initialize() 
    class_name="MyClass" 
End Sub 
'-----------------------获得类属性------------------- 
Public Property Get MyName 
MyName = class_name 
End Property 
'-----------------------写入类属性------------------- 
Public Property Let MyName(ByVal strName) 
class_name = strName 
End Property 
'-----------------------定义类过程------------------- 
Public Sub CheckName() 
    If Me.MyName = "MyClass" Then 
        MsgBox "目前为默认名",,"状态" 
    Else 
        MsgBox "名已经修改为:" & Me.MyName,,"状态" 
    End If 
End Sub 
'-----------------------类结束过程------------------- 
Private Sub Class_Terminate() 
End Sub 
End Class 
'-----------------------类定义完毕------------------- 
' 
' 
' 
'----------调用类的简单过程--------------- 
Dim X 
Set X = New MyClass 
MsgBox X.MyName 
X.CheckName 
X.MyName = "我是新名字" 
X.CheckName 
MsgBox X.MyName 
Set X = Nothing 
'----------------调用完毕----------------- 
 
哈哈,但是不知道类要怎么应用…… 
P的太慢了,试了下算8000天直接.....  
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.....  
    
 
  
 |   
 | 
  2007-5-2 17:12 | 
  
 | 
 | 
 
flyinspace 
银牌会员
 
     
 
  
  
积分 1206 
发帖 517 
注册 2007-3-25 
状态 离线
 | 
『第 12 楼』:
 
 
使用 LLM 解释/回答一下
  
Originally posted by slore at 2007-5-2 04:12 AM: 
VBS可以写类: 
 
 
 
哈哈,但是不知道类要怎么应用…… 
P的太慢了,试了下算8000天直接.....  
谢谢提出问题所在。。
 
下个版本,会判断 查询的天数问题。。这样就可以最快的计算了。
 
大于30天的。用月减。 
大于一年的。判断是否闰年然后减。。
 
这样就可以快很多了。  
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.  
    
 
  
  |  
                  
  
                    知,不觉多。不知,乃求知 |   
 | 
  2007-5-2 17:20 | 
  
 | 
 | 
 
youxi01 
高级用户
 
    
 
  
 
积分 846 
发帖 247 
注册 2006-10-27 来自 湖南==》广东 
状态 离线
 | 
『第 13 楼』:
 
 
使用 LLM 解释/回答一下
  
其实,老斑竹 willsort 早就写过了 相应的代码。不过,相关计算原理我到目前为止,还是没有想明白。 
利用该代码,就可以写出挑战题目相关要求。
  
@echo off 
title 日期查询 
color 1f 
if == call :Help 
call :Date2Day %1 days 
set/a days=%days%+%2 
call :Day2Date %days% date 
echo. 
echo -----------结果--------------- 
echo. 
echo 你查询的日期为:%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 使用方法:%~nx0    
    echo.                            
    echo 日期格式:2007-03-31                      
    echo 天数格式:+/-查询的天数                         
    echo 示例:%~nx0 2007-03-31 3 就是查询3天后的日期   
    echo ________________________________________________ 
    echo. 
    cmd /k 
  
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 
  
    
 
  
 |   
 | 
  2007-5-2 17:22 | 
  
 | 
 | 
 
flyinspace 
银牌会员
 
     
 
  
  
积分 1206 
发帖 517 
注册 2007-3-25 
状态 离线
 | 
『第 14 楼』:
 
 
使用 LLM 解释/回答一下
  
Originally posted by youxi01 at 2007-5-2 04:22 AM: 
其实,老斑竹 willsort 早就写过了 相应的代码。不过,相关计算原理我到目前为止,还是没有想明白。 
 
利用该代码,就可以写出挑战题目相关要求。 ...  
算法很简单。。
 
也比我的用得聪明。。。但因为没有考虑闰年的存在。。
 
计算长一点的天数时就会出问题。。。
 
若加入闰年的判断。。会好很多。  
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.  
    
 
  
  |  
                  
  
                    知,不觉多。不知,乃求知 |   
 | 
  2007-5-2 17:34 | 
  
 | 
 | 
 
youxi01 
高级用户
 
    
 
  
 
积分 846 
发帖 247 
注册 2006-10-27 来自 湖南==》广东 
状态 离线
 | 
『第 15 楼』:
 
 
使用 LLM 解释/回答一下
  
没考虑闰年,你测试过了吗? 
我的测试怎么“觉得”它已经考虑过闰年呢?! 
其实,它肯定考虑过了,至于计算原理我真的是不清楚。 
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. 
    
 
  
 |   
 | 
  2007-5-2 17:47 | 
  
 |