Why does the time stamp return differently?

Hi Guys,

I'm using Tera Term to create a log from the Serial Monitor. I need to return the epoch time stamp of the log which allows me to determine that a new log exists that I need to copy. The problem is that the Return Time is random each time it executes yet the formatted time stamp from the same code is correct.

// https://c-for-dummies.com/blog/?p=3004

#include <stdio.h>
#include <sys/stat.h>
#include <time.h>

long findTime(char nameOfFile[]);

void main(){
    long printThis = findTime("lora-meter.log.2");  // exists in the same directory as this c file
    printf("\n Returned Time: %ld",printThis);
    return 0;
}

long findTime(char nameOfFile[])
{
    struct stat filestat;
    if(stat(nameOfFile,&filestat)==0){
        printf(" File modify time %s",ctime(&filestat.st_mtime));
        //printf(" Raw time %d",&filestat.st_mtime);
        long returnThis = ctime(&filestat.st_mtime);
        return(returnThis);                         // return the raw time
    }
    else {
        printf(" File not found");
    }
}

One difference in the code is that the Return Time is trying to return the value as a long variable. I tried to return it as a char but, I couldn't get it to compile. I got the code from this post which states "The ctime() function converts each timestamp value to a date-time string. The time value is stored in Unix epoch time." Can someone tell me what's wrong or point me in the right direction? Thank you.

Did you deliberately not use the normal setup() and loop() structure for the program ?

Hey HeliBob. Correct because this code runs on my PC and not on the Nano which is sending the log. OVERVIEW BELOW:

  1. Nano sends messages to Com8
  2. Tera Term VT Monitors Com8 and logs everything to lora-meter.txt
  3. when lora-meter.txt reaches 10KB, Tera Term rotates (FIFO) the logs to 3 files before dropping the oldest (lora-meter.log.3):
    • lora-meter.txt over writes lora-meter.log.1
    • lora-meter.log.1 over writes lora-meter.log.2
    • lora-meter.log.2 over writes lora-meter.log.3
  4. The code I'm trying to fix, constantly checks the creation time of lora-meter.log.2 and when it updates, lora-meter.log.2 will be copied to logToParse.

The Problem: I can't get epoch to be returned properly. Should it be a long, char, ???

ctime returns a string. Just return filestat.st_mtime instead.

Awesome, that did the trick. I tried returning //long returnThis = &filestat.st_mtime; but it didn't work. You suggested that I drop the & which I never would have thought of. The page where I took the code from states " The ampersand & is used because stat() requires a pointer (memory address) as its second argument." I'm not understanding something (I'm a novice).
Would you mind sharing why your solution (dropping &) works?

filestat.st_mtime is the epoch value. For some strange reason (portability?) ctime requires that you pass a pointer to it rather than the epoch itself so you need the & to get the address. Your code was treating that address as if it were an epoch value itself.

When you drop the &, you're grabbing the epoch directly which is not recommended because you're assuming it's a long. It's more likely an unsigned long, but the docs tell you not to rely on how it is is currently implemented but to use the functions provided to manipulate time data.

Nonetheless, it'll likely work for your purposes. For now :wink:

1 Like

It would have helped if you had explained that in the first place, bearing in mind that this is an Arduino forum

Yes, I should have been more clear about how it was related to Arduino/Nano. I will keep this in mind on my next post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.