Parsing File's timestamp on a SD card

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

Parsing File's timestamp on a SD card

Postby Gfast2 » Fri Nov 03, 2017 11:45 am

Hi esp-idf,

I'd to know how to get the "UTC Time" from the timestamp that I got from a file I got on my SD card.
I've try to setup my local timezone as "Europe/Berlin", then deploy localtime() and gmtime() to read/parse the timestamp from a file on SD card. But the result is the same value, which is what I can read from my Windows machine.
Here is the code Snippet:

Code: Select all

//	read the file timestamp
	setenv("TZ", "Europe/Berlin", 1);
	tzset();
	struct stat attrib;
	stat(fToRead, &attrib);
	char fileTime[50];
//	strftime(fileTime, 50, "%Y-%m-%d %H:%M:%S", localtime(&attrib.st_mtime)); // Both function through out the same output.
	strftime(fileTime, 50, "%Y-%m-%d %H:%M:%S", gmtime(&attrib.st_mtime)); 
best regards

Gfast2

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Parsing File's timestamp on a SD card

Postby kolban » Sat Nov 04, 2017 4:58 am

Maybe look at the absolute time stamp as a large decimal number ... and plug it into ...

https://www.epochconverter.com/

It should be that the timestamp is UTC ... but this will check.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Parsing File's timestamp on a SD card

Postby tele_player » Sat Nov 04, 2017 4:07 pm

TZ environment variable is not formatted correctly.
https://github.com/espressif/newlib-esp ... me/tzset.c

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

Re: Parsing File's timestamp on a SD card

Postby Gfast2 » Sun Nov 05, 2017 4:58 pm

kolban wrote:Maybe look at the absolute time stamp as a large decimal number ... and plug it into ...

https://www.epochconverter.com/

It should be that the timestamp is UTC ... but this will check.
Hi Kolban,

Thanks for your suggestion!

I do understand unix time is a 32bit integer. If I only want to "Get the job done" there are many quick ways. Perhaps I just really want to know if I can do time calculation on ESP32 as on Linux platform. The solution on Linux let feel less "Hacky" :roll: :lol:

But thanks a a lot for your enlightling ideas! I'm one of your books audience! Its content ist awesome!

Cheers

Gfast

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

Re: Parsing File's timestamp on a SD card

Postby Gfast2 » Sun Nov 05, 2017 5:10 pm

tele_player wrote:TZ environment variable is not formatted correctly.
https://github.com/espressif/newlib-esp ... me/tzset.c
Hi tele_player,

Believe you or not. I spend literally more then ONE HOUR on this file, and the man page of this function. And till not, what really I took away is kind of... nothing. :oops:

As many other Javascript developers, I do understand what I got from Stack Overflow.
The code I posted here, is the solution from this post:
How can I set the time zone before calling strftime?

---
After thinking this problem for a while, I realize, what I really want to solve is another question:
How to parse a timestamp with "timezone" as argument to get its UTC time in a operating system that is using UTC time. :o

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Parsing File's timestamp on a SD card

Postby tele_player » Sun Nov 05, 2017 5:35 pm

This small change should produce your Berlin time, ignoring DST.

setenv("TZ", "Europe/Berlin1", 1);

To convert a time stamp with a known time zone to UTC is a bit more difficult, as you need a table of time zones and offsets, or you need to query the internet. Look at Wikipedia ‘tz database’ for more info.

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

Re: Parsing File's timestamp on a SD card

Postby Gfast2 » Sun Nov 05, 2017 6:52 pm

tele_player wrote:This small change should produce your Berlin time, ignoring DST.

setenv("TZ", "Europe/Berlin1", 1);

To convert a time stamp with a known time zone to UTC is a bit more difficult, as you need a table of time zones and offsets, or you need to query the internet. Look at Wikipedia ‘tz database’ for more info.
Hi tele_player,

Thanks for your think sharing!
I did found this timezone table
And the "Europe/Berlin" comes from there. :roll:

Perhapse I'm really really really too junior... But, How to explain "Europe/Berlin1" (with a 1 at very last?)

I think I can't test it tonight. I will tell you my result later.

Cheers

Gfast2

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Parsing File's timestamp on a SD card

Postby tele_player » Sun Nov 05, 2017 7:57 pm

It is explained here in the tzset () code:
The TZ environment variable is expected to be in the following POSIX
format:
stdoffset1[dst[offset2][,start[/time1],end[/time2]]]
where: std is the name of the standard time-zone (minimum 3 chars)
offset1 is the value to add to local time to arrive at Universal time
it has the form: hh[:mm[:ss]]
dst is the name of the alternate (daylight-savings) time-zone (min 3 chars)
offset2 is the value to add to local time to arrive at Universal time
it has the same format as the std offset
start is the day that the alternate time-zone starts
time1 is the optional time that the alternate time-zone starts
(this is in local time and defaults to 02:00:00 if not specified)
end is the day that the alternate time-zone ends
time2 is the time that the alternate time-zone ends
(it is in local time and defaults to 02:00:00 if not specified)
Note that there is no white-space padding between fields. Also note that
if TZ is null, the default is Universal GMT which has no daylight-savings
time. If TZ is empty, the default EST5EDT is used.
This requires understanding Unix-style documentation of arguments. Specifically, optional arguments are enclosed in ‘[]’.
So, when DST is ignored:
stdoffset1[dst[offset2][,start[/time1],end[/time2]]]
Is reduced to
stdoffset1

The name for the time zone:
std= "Europe/Berlin"
The offset is:
offset1=“1”

Combined, stdoffset1 is “ "Europe/Berlin1", or “Europe/Berlin+1”, but the ‘+’ should not be necessary.

User avatar
Gfast2
Posts: 182
Joined: Fri Aug 11, 2017 1:52 am

Re: Parsing File's timestamp on a SD card

Postby Gfast2 » Mon Nov 06, 2017 11:32 am

tele_player wrote:It is explained here in the tzset () code:
The TZ environment variable is expected to be in the following POSIX
format:
stdoffset1[dst[offset2][,start[/time1],end[/time2]]]
where: std is the name of the standard time-zone (minimum 3 chars)
offset1 is the value to add to local time to arrive at Universal time
it has the form: hh[:mm[:ss]]
dst is the name of the alternate (daylight-savings) time-zone (min 3 chars)
offset2 is the value to add to local time to arrive at Universal time
it has the same format as the std offset
start is the day that the alternate time-zone starts
time1 is the optional time that the alternate time-zone starts
(this is in local time and defaults to 02:00:00 if not specified)
end is the day that the alternate time-zone ends
time2 is the time that the alternate time-zone ends
(it is in local time and defaults to 02:00:00 if not specified)
Note that there is no white-space padding between fields. Also note that
if TZ is null, the default is Universal GMT which has no daylight-savings
time. If TZ is empty, the default EST5EDT is used.
This requires understanding Unix-style documentation of arguments. Specifically, optional arguments are enclosed in ‘[]’.
So, when DST is ignored:
stdoffset1[dst[offset2][,start[/time1],end[/time2]]]
Is reduced to
stdoffset1

The name for the time zone:
std= "Europe/Berlin"
The offset is:
offset1=“1”

Combined, stdoffset1 is “ "Europe/Berlin1", or “Europe/Berlin+1”, but the ‘+’ should not be necessary.
Hi tele_player,

I've just tried your suggestions, Perhaps I just need more time then I expected for this point. Because I update the time using GPS parsed information in other task too, which potentially poluting the time setting. With your advice I changed my timezone name to "Europe/Berlin1", and tryed to deploy localtime and gmtime to get the timezone specified time. Still without difference as before.
And I talked to my colleage either. They suggested me not rely on this timestamp, because the end user always has a computer that as old as they are (> 50 years old), and the Cell battery for BIOS time holding would never works. Which alway cause problems :lol:

So just let the end user specify their file editing time BY HAND.

Code: Select all

<EDIT_TIME>
2017-11-06T12:34:56ZZ
</EDIT_TIME>
Should make everyone happy at last. :lol: :D

But Thans tele_player for the information though! If I have time later. I'll figure out why the problem stay the same!

Cheers

Su

Who is online

Users browsing this forum: Google [Bot] and 80 guests