Thread overview
How do I port date calculations from C code to D?
Jan 29, 2006
John C
Jan 30, 2006
Walter Bright
January 29, 2006
I'm having trouble porting a simple C program to D. This program performs the task of a basic alarm system.  At some point in the program, the current time is converted into C's broken-down time structure, the structure is modified, and finally converted back to simple calendar time to find out how many seconds have to elapse from "now" to the target time.

But I'm unable to find similar functions in Phobos. I have looked in std.date, but that seems to implement only simple calendar time functions. Do I have to write my own code?

Here is a sample of my C code:

   current_time = time(NULL);
   localtime_r(&current_time, &current_time_tm);

   // Set up the target time, using today's data.
   final_time_tm = current_time_tm;
   final_time_tm.tm_hour = some_specific_hour;
   final_time_tm.tm_min = some_specific_minute;
   final_time_tm.tm_sec = 0;

   // Check if the final time is "lower" than now, to increment it.
   if(timelocal(&final_time_tm) - current_time < 1)
      final_time_tm.tm_mday += 1;

   seconds_to_go = timelocal(&final_time_tm) - current_time;


January 29, 2006
"Grzegorz Adam Hankiewicz" <fake@dont.use> wrote in message news:pan.2006.01.29.12.29.01.350512@dont.use...
> I'm having trouble porting a simple C program to D. This program performs the task of a basic alarm system.  At some point in the program, the current time is converted into C's broken-down time structure, the structure is modified, and finally converted back to simple calendar time to find out how many seconds have to elapse from "now" to the target time.
>
> But I'm unable to find similar functions in Phobos. I have looked in std.date, but that seems to implement only simple calendar time functions. Do I have to write my own code?
>
> Here is a sample of my C code:
>
>   current_time = time(NULL);
>   localtime_r(&current_time, &current_time_tm);
>
>   // Set up the target time, using today's data.
>   final_time_tm = current_time_tm;
>   final_time_tm.tm_hour = some_specific_hour;
>   final_time_tm.tm_min = some_specific_minute;
>   final_time_tm.tm_sec = 0;
>
>   // Check if the final time is "lower" than now, to increment it.
>   if(timelocal(&final_time_tm) - current_time < 1)
>      final_time_tm.tm_mday += 1;
>
>   seconds_to_go = timelocal(&final_time_tm) - current_time;
>

Import the std.c.time module.


January 30, 2006
"Grzegorz Adam Hankiewicz" <fake@dont.use> wrote in message news:pan.2006.01.29.12.29.01.350512@dont.use...
> But I'm unable to find similar functions in Phobos. I have looked in std.date, but that seems to implement only simple calendar time functions. Do I have to write my own code?

No. You can use the C functions directly that you're already using.