www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - recls_time_t to d_time?

reply Carlos Santander <csantander619 gmail.com> writes:
Does anybody know how to make that conversion?

-- 
Carlos Santander Bernal
May 15 2005
parent reply David L. Davis <SpottedTiger yahoo.com> writes:
In article <d6804u$5g2$1 digitaldaemon.com>, Carlos Santander says...
Does anybody know how to make that conversion?

-- 
Carlos Santander Bernal

I'm not exactly sure if recls_time_t is the same as C's time_t format? But if it is, then this code below from one of my sub-project might do the trick: # Author : David L. 'SpottedTiger' Davis # Licence : Public Domain # # private import std.date; # private import std.string; # private import std.c.time; # # // Convert from a D time into a C time # // (d_time is a 64-bit long in D, or a long long in C/C++) # d_time toDate( in time_t dtCDateTime ) # { # time_t ct; # tm *TM; # d_time dt; # # ct = dtCDateTime; # TM = std.c.time.localtime( &ct ); # # dt = parse( std.string.toString( TM.tm_mon + 1 ) ~ "/" ~ # std.string.toString( TM.tm_mday ) ~ "/" ~ # std.string.toString( TM.tm_year + 1900 ) ~ " " ~ # std.string.toString( TM.tm_hour ) ~ ":" ~ # std.string.toString( TM.tm_min ) ~ ":" ~ # std.string.toString( TM.tm_sec ) # ); # # return dt; # # } # # // Convert from a D time into a C time # // (time_t normally 32-bit long in C/C++) # time_t toDate( in d_time dtDDateTime ) # { # time_t ct; # tm TM; # # dtDDateTime = std.date.UTCtoLocalTime( dtDDateTime ); # # TM.tm_mon = std.date.MonthFromTime( dtDDateTime ); # TM.tm_mday = std.date.DateFromTime( dtDDateTime ); # TM.tm_year = std.date.YearFromTime( dtDDateTime ) - 1900; # TM.tm_hour = std.date.HourFromTime( dtDDateTime ); # TM.tm_min = std.date.MinFromTime( dtDDateTime ); # TM.tm_sec = std.date.SecFromTime( dtDDateTime ); # // TM.tm_wday = toISO8601YearWeek( dtDDateTime ); # TM.tm_yday = std.date.DateFromTime( dtDDateTime ); # TM.tm_isdst = 0; # # // Debugging...shows the raw values # // writefln( "toDate tm_mon=%d, tm_mday=%d, tm_year=%d", # // TM.tm_mon, TM.tm_mday, TM.tm_year ); # # ct = std.c.time.mktime( &TM ); # # return ct; # } Hope this helps you out, David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
May 15 2005
parent reply Carlos Santander <csantander619 gmail.com> writes:
David L. Davis escribió:
 In article <d6804u$5g2$1 digitaldaemon.com>, Carlos Santander says...
 
Does anybody know how to make that conversion?

-- 
Carlos Santander Bernal

I'm not exactly sure if recls_time_t is the same as C's time_t format? But if it is, then this code below from one of my sub-project might do the trick: [snip] Hope this helps you out, David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html

Thanks, David. recls_time_t on Unix is just a typedef for C's time_t, so it's your help is really appreciated. On Windows, however, it's a struct { uint lowdatetime, highdatetime }, so I don't know how that'd work. But thanks again. -- Carlos Santander Bernal
May 15 2005
parent reply David L. Davis <SpottedTiger yahoo.com> writes:
In article <d68nnv$kte$1 digitaldaemon.com>, Carlos Santander says...
David L. Davis escribió:
 In article <d6804u$5g2$1 digitaldaemon.com>, Carlos Santander says...
 
Does anybody know how to make that conversion?

-- 
Carlos Santander Bernal

I'm not exactly sure if recls_time_t is the same as C's time_t format? But if it is, then this code below from one of my sub-project might do the trick: [snip] Hope this helps you out, David L.

Thanks, David. recls_time_t on Unix is just a typedef for C's time_t, so it's your help is really appreciated. On Windows, however, it's a struct { uint lowdatetime, highdatetime }, so I don't know how that'd work. But thanks again. -- Carlos Santander Bernal

Carlos, On Windows, you should take a look in C:\dmd\src\phobos\std\c\windows\windows.d (private import std.c.windows.windows) where you'll find a couple of API functions for moving datetime between the filetime struct to the systemtime struct...from there you could modify the function I wrote (that's from the same sub-project as the last two were) below to move systemtime into D's Date struct where you can then get a d_time result. //Sample of what's in std\c\windows\windows.d # struct FILETIME { # DWORD dwLowDateTime; # DWORD dwHighDateTime; # } # alias FILETIME* PFILETIME, LPFILETIME; # # struct SYSTEMTIME # { # WORD wYear; # WORD wMonth; # WORD wDayOfWeek; # WORD wDay; # WORD wHour; # WORD wMinute; # WORD wSecond; # WORD wMilliseconds; # } # # export BOOL SystemTimeToFileTime(SYSTEMTIME *lpSystemTime, # FILETIME* lpFileTime); # export BOOL FileTimeToSystemTime(FILETIME *lpFileTime, # SYSTEMTIME* lpSystemTime); // ---------------------------- # Author : David L. 'SpottedTiger' Davis # Licence : Public Domain # # private import std.date; # private import std.string; # private import std.c.windows.windows; # # // Get System Date and Time with an Windows' Win32 # // API function and return the D time value. # d_time getWin32SystemTime() # { # SYSTEMTIME st; # d_time dt; # # with ( std.c.windows.windows ) # GetSystemTime( &st ); //Windows API call # # // Debugging...shows the raw values # // writefln("Year:%d, Month:%d, Date:%d, Hour:%d, Min:%d, Second:%d", # // st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond); # # dt = parse( std.string.toString( st.wMonth ) ~ "/" ~ # std.string.toString( st.wDay ) ~ "/" ~ # std.string.toString( st.wYear ) ~ " " ~ # std.string.toString( st.wHour ) ~ ":" ~ # std.string.toString( st.wMinute ) ~ ":" ~ # std.string.toString( st.wSecond ) # ); # # dt = std.date.UTCtoLocalTime( dt ); # # return dt; # } Hopefully, this answers your question, David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
May 15 2005
parent Carlos Santander <csantander619 gmail.com> writes:
David L. Davis escribió:
 
 Carlos,
 
 On Windows, you should take a look in C:\dmd\src\phobos\std\c\windows\windows.d
 (private import std.c.windows.windows) where you'll find a couple of API
 functions for moving datetime between the filetime struct to the systemtime
 struct...from there you could modify the function I wrote (that's from the same
 sub-project as the last two were) below to move systemtime into D's Date struct
 where you can then get a d_time result.
 
 [snip]
 
 Hopefully, this answers your question,
 David L.
 
 -------------------------------------------------------------------
 "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
 -------------------------------------------------------------------
 
 MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html

Thanks David. That's what I was looking for. BTW, I think this code (or something similar) should be in Phobos. std.recls is good, and so is std.date (except that there's no std.date.format and there're problems with timezones), so there should be a way to make them work together. -- Carlos Santander Bernal
May 16 2005