How can I build/create DateString using Locale?

15 posts / 0 new
Last post
jabirulo
jabirulo's picture
Offline
Last seen: 2 weeks 4 days ago
Joined: 2013-05-30 00:53
How can I build/create DateString using Locale?

HI, I have DAY MONTH and YEAR and want to create the date string using Locale format, so it shows the correct separator character ('-' '.' '/' ...) for the language being used under AmigaOS4.

What I'm doing now is to "get" such date separator char by "parsing" 'loc_ShortDateFormat' (locale.h)

  1. struct Locale *ll = ILocale->OpenLocale(NULL);
  2. char DateSep = *(ll->loc_ShortDateFormat+IUtility->Strlen(ll->loc_ShortDateFormat)-3);
  3. IDOS->Printf("\n'%lc'\n", DateSep);

Is there a proper/nice way to get such date separator character, or to build the date string, something like:
DateString_res = ILocale->BuildDateString(Locale, ShortDateMode, Day,Month,Year)

TIA

xenic
xenic's picture
Offline
Last seen: 2 years 7 months ago
Joined: 2011-05-07 04:52
Re: How can I build/create DateString using Locale?

@jabiluro
Check the DateToStr() function in the DOS autodocs. It seems to offer a number of date format options.

X1000 - OS 4.1FE

salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
Re: How can I build/create DateString using Locale?

@jabirulo

IDOS->DateToStr() with dat_Format set to FORMAT_DEF should format according to the current system default locale.

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
Re: How can I build/create DateString using Locale?

Both IDOS->DateToStr and ILocale->FormatDate should do. But there is no way to get a proper date from day/month/year, you rather need the number of days since 1978.

salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
Re: How can I build/create DateString using Locale?

IUtility->Date2Amiga() can be used to convert day/month/year to the total number of seconds since 1.1.1978 and IDOS->SecondsToDateStamp() can then be used to convert this to a struct DateStamp for IDOS->DateToStr().

xenic
xenic's picture
Offline
Last seen: 2 years 7 months ago
Joined: 2011-05-07 04:52
Re: How can I build/create DateString using Locale?

@jabirulo

Is there a proper/nice way to get such date separator character, or to build the date string, something like:
DateString_res = ILocale->BuildDateString(Locale, ShortDateMode, Day,Month,Year)

Assuming your "Day,Month,Year" are numbers it might be simpler to use something like this:

  1. #include <proto/exec.h>
  2. #include <proto/dos.h>
  3. #include <stdio.h>
  4.  
  5. int main(int argc, char **argv)
  6. {
  7. struct DateTime dt = {{0}};
  8. char buffer[64];
  9. int16 day = 27, month = 10, year = 16;
  10.  
  11. snprintf(buffer, sizeof(buffer), "%d-%d-%d", day, month, year);
  12. IDOS->Printf("CDN_DATE: %s\n", buffer);
  13.  
  14. dt.dat_Format = FORMAT_CDN;
  15. dt.dat_StrDate = (STRPTR)&buffer;;
  16. dt.dat_StrTime = NULL;
  17.  
  18. if (IDOS->StrToDate(&dt))
  19. {
  20. dt.dat_Format = FORMAT_DEF;
  21. dt.dat_StrDate = (STRPTR)&buffer;
  22. dt.dat_StrTime = NULL;
  23. dt.dat_StrDay = NULL;
  24. if (IDOS->DateToStr(&dt))
  25. IDOS->Printf("DEF_DATE: %s\n", buffer);
  26. }
  27.  
  28. return 0;
  29. }

X1000 - OS 4.1FE

jabirulo
jabirulo's picture
Offline
Last seen: 2 weeks 4 days ago
Joined: 2013-05-30 00:53
Re: How can I build/create DateString using Locale?

THX guays for all suggestions will try'em ASAP.

Again, thx for all.

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

jabirulo
jabirulo's picture
Offline
Last seen: 2 weeks 4 days ago
Joined: 2013-05-30 00:53
Re: How can I build/create DateString using Locale?

Just started to implement using Xenic's code (after that will use other's code posted)

  1. char date_buf[16];
  2. IUtility->SNPrintf(date_buf, 16, "%s",DateLocalized(&(xfi->xfi_Date)) );
  3. //IDOS->Printf("\n'%s'\n",date_buf);
  4. ...
  5. STRPTR DateLocalized(struct xadDate *pXadDate)
  6. {
  7. struct DateTime dt;// = {{0}};
  8. char res_buf[16];
  9.  
  10. IUtility->SNPrintf(res_buf, sizeof(res_buf), "%04ld-%02ld-%02ld", pXadDate->xd_Year, pXadDate->xd_Month, pXadDate->xd_Day);
  11. //IDOS->Printf("ISO_DATE: %s\n", res_buf);
  12.  
  13. dt.dat_Format = FORMAT_ISO;
  14. dt.dat_StrDate = (STRPTR)&res_buf;
  15. dt.dat_StrTime = NULL;
  16. dt.dat_StrDay = NULL;
  17.  
  18. if( IDOS->StrToDate(&dt) )
  19. {
  20. dt.dat_Format = FORMAT_DEF;
  21. dt.dat_Flags = 0;
  22. dt.dat_StrDate = (STRPTR)&res_buf;
  23. dt.dat_StrTime = NULL;
  24. dt.dat_StrDay = NULL;
  25. }
  26. //if( IDOS->DateToStr(&dt) ) IDOS->Printf("'%s'\n",res_buf);
  27.  
  28. if( IDOS->DateToStr(&dt) ) return (STRPTR)res_buf;
  29. else return NULL;
  30. }

I get:
xadUnFileS.c: In function 'DateLocalized':
xadUnFileS.c:848: warning: function returns address of local variable

Am I missing some cast or am I doing coding it very bad?

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

xenic
xenic's picture
Offline
Last seen: 2 years 7 months ago
Joined: 2011-05-07 04:52
Re: How can I build/create DateString using Locale?

@jabirulo

Am I missing some cast or am I doing coding it very bad?

You made a mistake. The "res_buf" variable is declared within the DateLocalized() function and is 'local' to that function. When you exit the DateLocalized() function, all local variables declared within the function are gone and no longer valid or legally accessible.

X1000 - OS 4.1FE

xenic
xenic's picture
Offline
Last seen: 2 years 7 months ago
Joined: 2011-05-07 04:52
Re: How can I build/create DateString using Locale?

@jabirulo
I also noticed that you commented out the DateTime structure initialization. If you don't initialize the DateTime structure you will probably get the wrong date result. Leave it like this:
struct DateTime dt = {{0}}; or struct DateTime = {{0},0};

X1000 - OS 4.1FE

jabirulo
jabirulo's picture
Offline
Last seen: 2 weeks 4 days ago
Joined: 2013-05-30 00:53
Re: How can I build/create DateString using Locale?

Yes, you're right I was testing and found that dates were random :-P restored initialization.
Changed to "void DateLocalized(struct xadDate *pXadDate, STRPTR dest)"

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
Re: How can I build/create DateString using Locale?

Using the method I suggested:

  1. void FormatXadDate(const struct xadDate *xad_date, STRPTR datestr) {
  2. struct ClockData clock_data;
  3. uint32 seconds;
  4. struct DateTime dat;
  5.  
  6. clock_data.sec = xad_date->xd_Second;
  7. clock_data.min = xad_date->xd_Minute;
  8. clock_data.hour = xad_date->xd_Hour;
  9. clock_data.mday = xad_date->xd_Day
  10. clock_data.month = xad_date->xd_Month;
  11. clock_data.year = xad_date->xd_Year;
  12. clock_data.wday = xad_date->xd_WeekDay % 7;
  13.  
  14. seconds = IUtility->Date2Amiga(&clock_data);
  15.  
  16. IDOS->SecondsToDateStamp(seconds, &dat.dat_Stamp);
  17.  
  18. dat.dat_Format = FORMAT_DEF;
  19. dat.dat_Flags = 0;
  20. dat.dat_StrDay = NULL;
  21. dat.dat_StrDate = datestr;
  22. dat.dat_StrTime = NULL;
  23.  
  24. IDOS->DateToStr(&dat);
  25. }

IMO it is more elegant than using a string as an intermediate format and it can easily be extended to obtain a formatted time string as well.

The wday field of the ClockData structure is not used by Date2Amiga() at all so it can be left as undefined if wanted. I just set it here for no real reason.

Also if formatted time is not needed the sec, min and hour fields can be set to zero.

jabirulo
jabirulo's picture
Offline
Last seen: 2 weeks 4 days ago
Joined: 2013-05-30 00:53
Re: How can I build/create DateString using Locale?

THX. I created that one, and will updated with your psoted code (and was thinking to add time too).

  1. void DateLocalized(struct xadDate *pXadDate, STRPTR dest, int32 len)
  2. {
  3. struct DateStamp ds = {0};
  4. struct DateTime dt = {{0}};
  5. struct ClockData cd = {0,0,0, pXadDate->xd_Day, pXadDate->xd_Month, pXadDate->xd_Year, 0};
  6. uint32 secs = 0;
  7.  
  8. secs = IUtility->Date2Amiga(&cd);
  9. IDOS->SecondsToDateStamp(secs, &ds);
  10.  
  11. dt.dat_Format = FORMAT_DEF;
  12. dt.dat_Flags = 0;
  13. dt.dat_Stamp = ds;
  14. dt.dat_StrDate = dest;
  15. dt.dat_StrTime = NULL;
  16. dt.dat_StrDay = NULL;
  17.  
  18. if( !IDOS->DateToStr(&dt) ) IUtility->Strlcpy(dest, " <unknown>", len);
  19. }

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

jabirulo
jabirulo's picture
Offline
Last seen: 2 weeks 4 days ago
Joined: 2013-05-30 00:53
Re: How can I build/create DateString using Locale?

and here is mrirtter version:

  1. // Mark Ritter's version
  2. void fmtfunct(struct Hook *hook, struct Locale *locale, ULONG ch)
  3. {
  4. char *buffer = (char *)hook->h_Data;
  5. *buffer++ = ch;
  6. hook->h_Data = (APTR)buffer;
  7. }
  8.  
  9. void DateLocalized2(struct xadDate *pXadDate, STRPTR dest, struct Locale *loc)
  10. {
  11. struct DateStamp ds = {0};
  12. struct Hook *prhook;
  13. struct ClockData cd = {pXadDate->xd_Second, pXadDate->xd_Minute, pXadDate->xd_Hour,
  14. pXadDate->xd_Day, pXadDate->xd_Month, pXadDate->xd_Year, 0};
  15. uint32 secs = 0;
  16.  
  17. secs = IUtility->Date2Amiga(&cd);
  18. IDOS->SecondsToDateStamp(secs, &ds);
  19.  
  20. prhook = (struct Hook *)IExec->AllocSysObjectTags(ASOT_HOOK,
  21. ASOHOOK_Entry,fmtfunct, ASOHOOK_Data,dest,
  22. TAG_DONE);
  23. ILocale->FormatDate(NULL, loc->loc_DateTimeFormat, &ds, prhook);
  24. //IDOS->Printf("Fecha: %s\n",dest);
  25. }

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

xenic
xenic's picture
Offline
Last seen: 2 years 7 months ago
Joined: 2011-05-07 04:52
Re: How can I build/create DateString using Locale?

@jabirulo

THX. I created that one, and will updated with your psoted code (and was thinking to add time too).

I haven't had time to test it but here is another possibility based on your code:

  1. void DateLocalized(struct xadDate *pXadDate, STRPTR dest, int32 len)
  2. {
  3. struct DateStamp ds = {0};
  4. struct DateTime dt = {{0}};
  5.  
  6. if ( !IXadMaster->xadConvertDates(XAD_DATEXADDATE, (Tag)pXadDate,
  7. XAD_GETDATEDATESTAMP, (Tag)&ds,
  8. TAG_END) )
  9. {
  10. dt.dat_Format = FORMAT_DEF;
  11. dt.dat_Flags = 0;
  12. dt.dat_Stamp = ds;
  13. dt.dat_StrDate = dest;
  14. dt.dat_StrTime = NULL;
  15. dt.dat_StrDay = NULL;
  16.  
  17. if( IDOS->DateToStr(&dt) ) return;
  18. }
  19. IUtility->Strlcpy(dest, " <unknown>", len);
  20. }
  21. /* NOTE: IXadMaster->xadConvertDates() returns 0 for success. */

X1000 - OS 4.1FE

Log in or register to post comments