lestrrat-go / strftime

Fast strftime for Go
MIT License
110 stars 21 forks source link

It is not same with c strftime? #12

Closed Fishbubble closed 3 years ago

Fishbubble commented 4 years ago
for i := 0; i < 400; i++ {
        now := time.Now().Unix() + int64(i)*86400
        cWeek := common.GetWeek(now) // cgo

        strfWeek, _ := strftime.Format("%Y%U", time.Unix(now, 0))
        gWeek, _ := strconv.ParseInt(strfWeek, 10, 64)
        if cWeek != int(gWeek) {
            fmt.Println("failed!!!", time.Unix(now, 0).Format("2006.01.02 15:04:05"), cWeek, gWeek)
            break
        }
    }

output: failed!!! 2021.01.01 00:35:27 202052 202100 failed!!! 2021.01.02 00:35:27 202052 202100

lestrrat commented 4 years ago

I can't test your code because you have common.GetWeek in there, which I obviously don't have. Can you kindly provide a standalone reproducible sample?

Fishbubble commented 4 years ago

I can't test your code because you have common.GetWeek in there, which I obviously don't have. Can you kindly provide a standalone reproducible sample?

Thank you for your reply! I'm sorry! common.GetWeek is cgo, because our company team code in different language.

/*
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int getweek(int time_stamp) {
    time_t rawtime = time_stamp;
    char buffer[128] = {0};
    struct tm *timeinfo;
    timeinfo = localtime(&rawtime);
    strftime(buffer, sizeof(buffer), "%Y%U", timeinfo);
    return atoi(buffer);
}
*/
import "C"
func GetWeek(in_time int64) int {
    cw := C.getweek(C.int(in_time))
    return int(cw)
}

Actually, the result is when increase loop count: failed!!! 2024.01.01 20:55:39 202400 202401 failed!!! 2024.01.02 20:55:39 202400 202401

lestrrat commented 4 years ago

@Fishbubble I added a slightly modified version of your test, and can't reproduce. This is on a MacOS 10.14.6

https://github.com/lestrrat-go/strftime/blob/1043fa0443fc3a688a98c374db181639e41b00f2/internal/util/util.go#L1-L22

https://github.com/lestrrat-go/strftime/blob/1043fa0443fc3a688a98c374db181639e41b00f2/strftime_test.go#L235-L249

finch% git log -n 1
commit 1043fa0443fc3a688a98c374db181639e41b00f2 (HEAD -> topic/gh-issue-12, origin/topic/gh-issue-12)
Author: Daisuke Maki <lestrrat+github@gmail.com>
Date:   Sat Feb 15 22:12:40 2020 +0900

    use actual test
finch% go test -v .
=== RUN   TestCombine
--- PASS: TestCombine (0.00s)
=== RUN   TestExclusion
--- PASS: TestExclusion (0.00s)
=== RUN   TestInvalid
--- PASS: TestInvalid (0.00s)
=== RUN   TestFormat
--- PASS: TestFormat (0.00s)
=== RUN   TestFormatBlanks
--- PASS: TestFormatBlanks (0.00s)
=== RUN   TestFormatZeropad
--- PASS: TestFormatZeropad (0.00s)
=== RUN   TestGHIssue5
--- PASS: TestGHIssue5 (0.00s)
=== RUN   TestGHPR7
--- PASS: TestGHPR7 (0.00s)
=== RUN   TestGHIssue9
--- PASS: TestGHIssue9 (0.00s)
=== RUN   TestGHIssue12
--- PASS: TestGHIssue12 (0.00s)
=== RUN   Example_CustomSpecifications
--- PASS: Example_CustomSpecifications (0.00s)
PASS
ok      github.com/lestrrat-go/strftime (cached)
finch% 
lestrrat commented 4 years ago

@Fishbubble Is this still a problem?

lestrrat commented 3 years ago

Closing, due to lack of activity