Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

convert DayOfWeek to numDayOfWeek tango.text.locale.Core.DateTimeFormat

Moderators: kris

Posted: 11/30/08 00:16:42

Hello,

I've wrote 2 function to convert the DayOfWeek? to his position in the week (depending on the first day of week) and the contrary.

Does the tango team is interesting ?

Index: text/locale/Core.d
===================================================================
--- text/locale/Core.d	(revision 4135)
+++ text/locale/Core.d	(working copy)
@@ -1,4 +1,4 @@
-/*******************************************************************************
+/*******************************************************************************
 
         copyright:      Copyright (c) 2005 John Chapman. All rights reserved
 
@@ -1649,6 +1649,48 @@
   }
 
   /**
+   * $(ANCHOR DateTimeFormat_getPosDayOfWeek)
+   * Convert the DayOfWeek to his position in the week (0..6)
+   * Params: dow = A DayOfWeek value
+   * Returns: the position in the week (0..6) depending on the first day of the week
+   */
+  uint getPosDayOfWeek(Calendar.DayOfWeek dow) {
+
+    uint firstDowNum = cast(uint) firstDayOfWeek();
+
+    uint dowInt = cast(uint) dow;
+
+    if(firstDowNum < dowInt)
+      return dowInt - firstDowNum;
+    else
+      return dowInt - firstDowNum + 7;
+
+  }
+
+  /**
+   * $(ANCHOR DateTimeFormat_convertPosDayOfWeek)
+   * Convert the position of a day in a week (0..6) to the DayOfWeek
+   * Params: numDow = the position of the day in the week (0..6) depending on the first day of the week
+   * Returns: the corresponding DayOfWeek
+   */
+  Calendar.DayOfWeek convertPosDayOfWeek(uint numDow)
+  in {
+    assert(numDow < 8);
+  }
+  body {
+
+    uint firstDowNum = cast(uint) firstDayOfWeek();
+
+    uint cvt = numDow + firstDowNum;
+
+    if(cvt > 6)
+      return cast(Calendar.DayOfWeek) (cvt - 7);
+    else
+      return cast(Calendar.DayOfWeek) cvt;
+  }
+
+
+  /**
    * $(ANCHOR DateTimeFormat_getAbbreviatedMonthName)
    * Retrieves the abbreviated name of the specified month based on the culture of the instance.
    * Params: month = An integer between 1 and 13 indicating the name of the _month to return.

Author Message

Posted: 11/30/08 09:36:14

I don't know yet if this is a suitable addition, but it would be easier to discuss properly/document reasoning if you created a wishlist ticket for it.

Posted: 11/30/08 10:25:15

Thanks Larsivi.
Here it is :
http://www.dsource.org/projects/tango/ticket/1379

I was not sure on the procedure to use. So I ask here first to not "overload" the ticket list. Don't know if it's a good idea (?)