Week number...

Hi,

I'm creating a function that should generate a week number.

I can't draw it, where am I making a mistake, please?

  • This is my original code that I wanted to change:

    Hmm, that's not what I saw in your OP before. Maybe it was edited by accident?

    I saw

        var currWeek = getCurrentWeekNumber;

    But that explains some of the confusion I think.

    everything worked for me, but you wrote me that this method of solution is not very good and you recommended the solution using the link:

    https://forums.garmin.com/developer/connect-iq/f/discussion/2778/week-of-year/27615#27615)

    I copied all the code into my function, including the function:

    Sorry I didn't make things clear and sorry I misunderstood too because I didn't look closely enough at the other post in the thread. Maybe the link doesn't work properly?

    There's more than one solution in that thread.

    getWeekNumber() by easurov isn't quite right, according to the discussion..

    The solution by travis.vitek (iso_week_number()) is apparently the correct one. It's also the one that looks right to me, although I haven't tried it myself.

    I'll just paste the whole thing here after beautifying it.

    function julian_day(year, month, day) {
        var a = (14 - month) / 12;
        var y = (year + 4800 - a);
        var m = (month + 12 * a - 3);
        return day + ((153 * m + 2) / 5) + (365 * y) + (y / 4) - (y / 100) + (y / 400) - 32045;
    }
    
    function is_leap_year(year) {
        if (year % 4 != 0) {
            return false;
        } else if (year % 100 != 0) {
            return true;
        } else if (year % 400 == 0) {
            return true;
        }
    
        return false;
    }
    
    function iso_week_number(year, month, day) {
        var first_day_of_year = julian_day(year, 1, 1);
        var given_day_of_year = julian_day(year, month, day);
    
        var day_of_week = (first_day_of_year + 3) % 7; // days past thursday
        var week_of_year = (given_day_of_year - first_day_of_year + day_of_week + 4) / 7;
    
        // week is at end of this year or the beginning of next year
        if (week_of_year == 53) {
    
            if (day_of_week == 6) {
                return week_of_year;
            } else if (day_of_week == 5 && is_leap_year(year)) {
                return week_of_year;
            } else {
                return 1;
            }
        }
    
        // week is in previous year, try again under that year
        else if (week_of_year == 0) {
            first_day_of_year = julian_day(year - 1, 1, 1);
    
            day_of_week = (first_day_of_year + 3) % 7;
    
            return (given_day_of_year - first_day_of_year + day_of_week + 4) / 7;
        }
    
        // any old week of the year
        else {
            return week_of_year;
        }
    }

    Also, here is a better link describing the rules for the week date (which includes the logic for leap years):
    en.wikipedia.org/.../ISO_week_date

    I still have one last question, how can I call a week number in another language using a string file?

    You want to write the numbers out (like "one" and "fifty-three") instead of displaying them as numerals ("1" and "53")? Is there enough room in your app's display layout for that?

    1) Add the words for the numbers as strings. e.g.

    <strings>
        <string id="S1">One</string>
        <string id="S2">Two</string>
        ...
        <string id="S53">Fifty-three</string>
    </strings>

    2) Create helper function to load string resource for given week number:

        function weekNumberToString(weekNumber) {
            var stringResources = [
                Rez.Strings.S1,
                Rez.Strings.S2,
                ...
                Rez.Strings.S53
            ];
    
            if (weekNumber >= 1 && weekNumber <= 53) {
                return WatchUi.loadResource(stringResources[weekNumber-1]);
            }
            return "";
        }

    3) Use helper function in your code which draws the week number

    Note that this approach sacrifices speed for memory savings (as you'd be calling loadResource every time you display the week number, as opposed to loading all the strings at once.)

  • Hi,
    I didn't want to change the generated week numbers, only the name of the week.
    For example, in English the week number is: WeekDay (WD), but in another language there will be a "string" eg resource-DE = "WochenNummer". (WN)


    They need to display it in dc.draw always in the system language with the file STRING ...

    resources-"Lang"
    
                                           resources - "Lang" for Week ???
    dc.drawText(x, y, $.fontTest, "Week "+currentWeek.format("%1d"), Graphics.TEXT_JUSTIFY_CENTER);

    I have a total of 3. language files and I want to generate them from them.

    Please help how to call the current Language file in dc.draw.?

    Thank you

  • Okay, so define the string for week number in your resources, very similar to the above example:

    resource-eng:

    <strings>

      <string id="WeekNumber">WeekDay</string>

    </strings>

    resource-deu:

    <strings>

      <string id="WeekNumber">WochenNummer</string>

    </strings>

    Monkey C:

    var weekNumberLabel = WatchUi.loadResource(Rez.Strings.WeekNumber); // load once in initialize()

    //...

    dc.drawText(x, y, $.fontTest, weekNumberLabel + " " +currentWeek.format("%1d"), Graphics.TEXT_JUSTIFY_CENTER);

    In your app's manifest.xml, make sure the necessary languages are selected. (CTRL/CMD-P > Monkey C: Edit Languages)

    In the simulator, select the language using Settings > Language.

    As the docs mention, you can see examples of this stuff in the SDK's Strings sample.

  • That's what I needed. Thanks also for the API link. When I searched the documentation, I didn't find anything ... but I probably used the wrong keyword..

  • No problem!

    I will say that "WeekDay" is probably not the correct string in English. "Week day" means day of the week (Monday, Tuesday, etc.)

    In this case I would go with "Week". (On bigger screen, maybe "Week Number". But for a watch, I think "Week" is fine.)

  • I give "VoteUP" for help.

    Thanks, in terms of name and abbreviation, I'll think about it.Tophat