Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date formatted cell output twice in CSV #879

Open
marrs opened this issue Jul 2, 2024 · 1 comment
Open

Date formatted cell output twice in CSV #879

marrs opened this issue Jul 2, 2024 · 1 comment

Comments

@marrs
Copy link

marrs commented Jul 2, 2024

To reproduce, export the following file to CSV.

# This data file was generated by the Spreadsheet Calculator Improvised (sc-im)
# You almost certainly shouldn't edit it.

newsheet "Sheet1"
movetosheet "Sheet1"
offscr_sc_cols 0
offscr_sc_rows 0
nb_frozen_rows 0
nb_frozen_cols 0
nb_frozen_screenrows 0
nb_frozen_screencols 0
format A 13 2 0
leftstring A0 = "1/2/20"
let A0 = 1580515200
fmt A0 "d%d/%m/%y"
leftstring A1 = "1/2/20"
let A1 = 1580515200
fmt A1 "d%b %Y"
goto A1

The following CSV is produced

01/02/201/2/20
Feb 20201/2/20

The following should be produced:

01/02/20
Feb 2020
@marrs
Copy link
Author

marrs commented Jul 2, 2024

The following patch fixes the issue, but I don't know the code well enough to know if it might break something elsewhere ;)

diff --git a/src/file.c b/src/file.c
index 738b9e3..40c0cfd 100644
--- a/src/file.c
+++ b/src/file.c
@@ -1884,12 +1884,12 @@ void export_delim(char * fname, char coldelim, int r0, int c0, int rn, int cn, i
             int last_valid_col = right_limit(sh, row)->col; // for issue #374
             if (col > last_valid_col) continue;
             if (*pp) {
+                char field[FBUFLEN] = "";
                 char * s;
                 if ((*pp)->flags & is_valid) {
                     if ((*pp)->cellerror) {
                         (void) fprintf (f, "%*s", sh->fwidth[col], ((*pp)->cellerror == CELLERROR ? "ERROR" : "INVALID"));
                     } else if ((*pp)->format) {
-                        char field[FBUFLEN];
                         if (*((*pp)->format) == 'd') {  // Date format
                             time_t v = (time_t) ((*pp)->v);
                             strftime(field, sizeof(field), ((*pp)->format)+1, localtime(&v));
@@ -1899,13 +1899,12 @@ void export_delim(char * fname, char coldelim, int r0, int c0, int rn, int cn, i
                         ltrim(field, ' ');
                         unspecial(f, field, coldelim);
                     } else { //eng number format
-                        char field[FBUFLEN] = "";
                         (void) engformat(sh->realfmt[col], sh->fwidth[col], sh->precision[col], (*pp)->v, field, sizeof(field));
                         ltrim(field, ' ');
                         unspecial(f, field, coldelim);
                     }
                 }
-                if ((s = (*pp)->label)) {
+                if (field[0] == '\0' && (s = (*pp)->label)) {
                     ltrim(s, ' ');
                     unspecial(f, s, coldelim);
                 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants