0

我正在尝试将以下公式转换为 C 但未能获得正确的语法。基本上,我使用今天的日期减去一组定义的日期来查看 mod 余数是否与 28 天的轮换匹配,如果为真则提供所需的答案。下面是我试图转换为 C 的工作 Excel 公式...

=IF(OR(
MOD(TODAY()-DATE(2013,1,7),28)=0,
MOD(TODAY()-DATE(2013,1,16),28)=0,
MOD(TODAY()-DATE(2013,1,25),28)=0,
MOD(TODAY()-DATE(2013,2,4),28)=0,
MOD(TODAY()-DATE(2013,2,13),28)=0,
MOD(TODAY()-DATE(2013,2,22),28)=0)=TRUE,"Option1",IF(
OR(
MOD(TODAY()-DATE(2013,1,9),28)=0,
MOD(TODAY()-DATE(2013,1,18),28)=0,
MOD(TODAY()-DATE(2013,1,28),28)=0,
MOD(TODAY()-DATE(2013,2,6),28)=0,
MOD(TODAY()-DATE(2013,2,15),28)=0,
MOD(TODAY()-DATE(2013,2,25),28)=0)=TRUE,"Option2",IF(
OR(
MOD(TODAY()-DATE(2013,1,11),28)=0,
MOD(TODAY()-DATE(2013,1,21),28)=0,
MOD(TODAY()-DATE(2013,1,30),28)=0,
MOD(TODAY()-DATE(2013,2,8),28)=0,
MOD(TODAY()-DATE(2013,2,18),28)=0,
MOD(TODAY()-DATE(2013,2,27),28)=0)=TRUE,"Option3",IF(
OR(
MOD(TODAY()-DATE(2013,1,14),28)=0,
MOD(TODAY()-DATE(2013,1,23),28)=0,
MOD(TODAY()-DATE(2013,2,1),28)=0,
MOD(TODAY()-DATE(2013,2,11),28)=0,
MOD(TODAY()-DATE(2013,2,20),28)=0,
MOD(TODAY()-DATE(2013,3,1),28)=0)=TRUE,"Option4","Not Required"))))

有什么好心人能帮我指引正确的方向吗?

4

2 回答 2

1

我认为这可能是相关的:

http://www.codeproject.com/Articles/2750/Excel-serial-date-to-Day-Month-Year-and-vise-versa

转换后的代码如下

#include <time.h>
#include <stdio.h>

int DMYToExcelSerialDate(int nYear, int nMonth, int nDay)
{
// Excel/Lotus 123 have a bug with 29-02-1900. 1900 is not a
// leap year, but Excel/Lotus 123 think it is...
if (nDay == 29 && nMonth == 02 && nYear==1900)
    return 60;

// DMY to Modified Julian calculatie with an extra substraction of 2415019.
long nSerialDate = 
        (int)(( 1461 * ( nYear + 4800 + (int)(( nMonth - 14 ) / 12) ) ) / 4) +
        (int)(( 367 * ( nMonth - 2 - 12 * ( ( nMonth - 14 ) / 12 ) ) ) / 12) -
        (int)(( 3 * ( (int)(( nYear + 4900 + (int)(( nMonth - 14 ) / 12) ) / 100) ) ) / 4) +
        nDay - 2415019 - 32075;

if (nSerialDate < 60)
{
    // Because of the 29-02-1900 bug, any serial date 
    // under 60 is one off... Compensate.
    nSerialDate--;
}

return (int)nSerialDate;
}

int main (void)
{
int serial_date;
time_t now;
struct tm *date_time;

time (&now);

date_time = localtime(&now);
serial_date = DMYToExcelSerialDate( (date_time->tm_year+1900), (date_time->tm_mon + 1) ,date_time->tm_mday);


if ( ( (serial_date - DMYToExcelSerialDate(2013,1,7))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,1,16))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,1,25))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,4))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,13))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,22))%28 == 0) )
{
    printf("option1\n");
    return 1;
}
else if ( ( (serial_date - DMYToExcelSerialDate(2013,1,9))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,1,18))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,1,28))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,6))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,15))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,25))%28 == 0) )
{
   printf("option2\n");
   return 2;
}
else if ( ( (serial_date - DMYToExcelSerialDate(2013,1,11))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,1,21))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,1,30))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,8))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,18))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,27))%28 == 0) )
{
    printf("option3\n");
    return 3;
}
else if ( ( (serial_date - DMYToExcelSerialDate(2013,1,14))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,1,23))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,1))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,11))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,20))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,3,1))%28 == 0) )
{
    printf("option4\n");
    return 4;
}
else
{
    printf("not required\n");
    return 0;
}

}

请注意,转换后的代码使用根据通用开发和分发许可 (CDDL) 许可的源代码,如上文所述。

于 2013-08-21T03:34:40.653 回答
0

我不是 Excel 函数向导,但我很确定以下三个函数可能会有所帮助:time()、localtime() 和 mktime()。每个都是 ANSI C 并且有据可查。通过获取当前时间,然后在函数之间来回传递它(通过指向 struct 或 time_t 值的指针),您可以操作或检查不同的时间值。 简单的例子:

#include <windows.h>
#include <ansi_c.h>

void timeFunc(void);

void main(void)
{
    timeFunc();
}

void timeFunc(void)
{
    struct  tm *today;
    struct tm *modified_time;
    time_t  secCntGMT1900 = 0; 
    time_t  modifiedTime;
    // Get GMT time in seconds since 1900
    time( &secCntGMT1900 );

    // Convert GMT time to local time to local time format
    today = localtime( &secCntGMT1900 );

    //modify 'today' members:
    today->tm_hour = 0;
    today->tm_min = 0;
    today->tm_sec = 0;
    today->tm_mon = 2;
    today->tm_mday =1;
    today->tm_year;

    //modified struct passed to mktime()
    modifiedTime = mktime(today);   
}
于 2013-08-21T01:25:21.383 回答