Probleme math night 0.14

const lmdboffset : [] const u8 ="-0330"
if (lmdboffset[0] == '-') sign -= 1 else sign = 1;
        hours = std.fmt.parseInt(i32, lmdboffset[1..3], 10) catch unreachable;
        minutes = std.fmt.parseInt(i32, lmdboffset[3..5], 10) catch unreachable;
        totalminute  = (hours * 60 + minutes) * sign;

-0330 -1050 β†’ result zig please explanation ???

3*60 = 180 180 +30 = 210 210 * -1 = -210 β†’ calculette or even head

last version 0.14-dev nigth

How are you initialising sign?
Do you have a typo in the sign -= 1 expression?

the offset arrives from an lmdb file, check


    const lmdboffset: []const u8 = "-0330";
    const sign: i32 = if (lmdboffset[0] == '-') -1 else 1;
    const hours = std.fmt.parseInt(i32, lmdboffset[1..3], 10) catch unreachable;
    const minutes = std.fmt.parseInt(i32, lmdboffset[3..5], 10) catch unreachable;
    const totalminute = (hours * 60 + minutes) * sign;

    std.log.err("{}", .{totalminute});

Correctly prints -210 in my case. There must be something in the code you are not showing, that causes this behavior

I do my test
std.debug.print(β€œ{d}”,.{totalminute});

You could add further prints, logs or asserts to your intermediate steps to see where your computation is off.

    var lmdbkey :[] const u8 = undefined;
    var lmdboffset  :[] const u8 = undefined;
    // var lmdboffset2  :[] const u8 = undefined;
    var hours   : i32 = 0;
    var minutes : i32 = 0;
    var totalminute : i32 = 0;
    // traitement de la commande timedatectl
    var lines = std.mem.splitAny(u8, buffer, "\n");
    while (lines.next()) |line | {
        if (std.mem.eql(u8,line,"END")) break;

        // Separating region and offset
        var parts = std.mem.splitScalar(u8, line, '|');
        lmdbkey =  parts.first();
        lmdboffset = parts.next() .? ;
        hours = 0;
        minutes = 0;
// correction
        const sign: i32 = if(lmdboffset[0] == '-') -1 else 1;
        hours = std.fmt.parseInt(i32, lmdboffset[1..3], 10) catch unreachable;
        minutes = std.fmt.parseInt(i32, lmdboffset[3..5], 10) catch unreachable;
        totalminute  = (hours * 60 + minutes) * sign;
        // result = totalminute ;     
        std.debug.print("{s} {s} {d:>5} \n",.{lmdbkey,lmdboffset,totalminute });

        tbl_key.append(lmdbkey) catch unreachable;
        tbl_offset.append(totalminute) catch unreachable;
        totalminute=0;
    }

If you have multiple negative entries, then you will continuously subtract 1 from sign instead of setting it to -1

Edit: Meaning that it should probably be sign = -1

2 Likes

ok
I missed the boat, I’m not used to not initializing zones, I was so focused on how to enter the offset of the timedatectl and the zone link with the Europe/Paris test.
Working alone on a subject can lead to mistakes

Thanks again, as it’s not easy to do it all by myself.

To avoid issues like this I’d recommend to prefer using local constants instead of non-local variables:

const sign: i32 = if(lmdboffset[0] == '-') -1 else 1;
5 Likes