datetime and strftime
datetime and strftime
Just started with micropython and the forum and looking to log some voltages through the X19 port of PYBv1.1 board running 1.8.7 release.
I have used sync_rtc to set the board time, but I'd like to use the time to make the filename unique, like - DD_MM_HH_logfile.dat
I've used datetime strftime on (big) python to write this string, but can't find the datetime and strftime on micropython.
Can anyone suggest a way forward? Thanks
I have used sync_rtc to set the board time, but I'd like to use the time to make the filename unique, like - DD_MM_HH_logfile.dat
I've used datetime strftime on (big) python to write this string, but can't find the datetime and strftime on micropython.
Can anyone suggest a way forward? Thanks
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: datetime and strftime
Try:
Arguably the name would be more unique (if there are degrees of uniqueness ) if you included year and month information.
Code: Select all
import pyb
rtc = pyb.RTC()
t = rtc.datetime()
name = '{:02d}_{:02d}_{:02d}_logfile.dat'.format(t[2], t[4], t[5])
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: datetime and strftime
Thankyou pythoncoder, that has worked well.
Another query if I may - the data as logged to file shows the correct time, when I open it using a text editor or spreadsheet, but the actual linux "date modified" timestamp in a directory listing of the files on the SD card is actually ahead of the actual time - so that if the file was written and closed on Monday 27 March, the file listing shows it was modified Tuesday 28 March.
Looks like a time offset somewhere - perhaps a timezone parameter in the firmware?
Any suggestions appreciated.
Another query if I may - the data as logged to file shows the correct time, when I open it using a text editor or spreadsheet, but the actual linux "date modified" timestamp in a directory listing of the files on the SD card is actually ahead of the actual time - so that if the file was written and closed on Monday 27 March, the file listing shows it was modified Tuesday 28 March.
Looks like a time offset somewhere - perhaps a timezone parameter in the firmware?
Any suggestions appreciated.
The issue is that FAT filesystem stores its timestamps using localtime.
The linux kernel runs on UTC time.
Normally, the timezone is purely a userspace thing. Under linux, different users could be logged into the same computer from many different timezones, and this is why the kernel runs in UTC time and the users timezone setting determines the local time.
Most linux filesystems also keep their filesystem timestamps in UTC time as well.
However, the FAT filesystem stores its filesystem timestamps in local time.
The timestamp on the sdcard is most likely correct, and if you put the sdcard on a windows machine, then it will show correctly. In order for linux to display the time correctly, the kernel needs to be told what the current timezone is.
I verified this on 3 of my linux computers, I wrote a small C program: and on 3 of my computers (all running Ubuntu 16.04) I got this output: and ls -l /media/dhylands/PYBFLASH showed the micropython filesystem timestamps as 7 hours in the future.
If I then did and reran my gettimeofday C program then I got:and now ls -l /media/dhylands/PYBFLASH showed the correct time. Normally, the call to hwclock --systz should be in one of the system startup files, and I'm not sure why it isn't happening. So some further investigation is due.
The linux kernel runs on UTC time.
Normally, the timezone is purely a userspace thing. Under linux, different users could be logged into the same computer from many different timezones, and this is why the kernel runs in UTC time and the users timezone setting determines the local time.
Most linux filesystems also keep their filesystem timestamps in UTC time as well.
However, the FAT filesystem stores its filesystem timestamps in local time.
The timestamp on the sdcard is most likely correct, and if you put the sdcard on a windows machine, then it will show correctly. In order for linux to display the time correctly, the kernel needs to be told what the current timezone is.
I verified this on 3 of my linux computers, I wrote a small C program:
Code: Select all
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
int main(int argc, char **argv ) {
int rc;
struct timeval tv;
struct timezone tz;
memset(&tv, 0, sizeof(tv));
memset(&tz, 0, sizeof(tz));
rc = gettimeofday(&tv, &tz);
if (rc == 0) {
printf("tv.tv_sec = %ld\n", tv.tv_sec);
printf("tv.tv_usec = %ld\n", tv.tv_usec);
printf("tz.tz_minuteswest = %d\n", tz.tz_minuteswest);
printf("tz.tz_dsttime = %d\n", tz.tz_dsttime);
} else {
printf("gettimeofday failed, errno = %d\n", errno);
}
return 0;
}
Code: Select all
tv.tv_sec = 1490636674
tv.tv_usec = 498363
tz.tz_minuteswest = 0
tz.tz_dsttime = 0
If I then did
Code: Select all
sudo hwclock --systz
Code: Select all
tv.tv_sec = 1490636725
tv.tv_usec = 276087
tz.tz_minuteswest = 420
tz.tz_dsttime = 0
Re: datetime and strftime
pythoncoder,
I tried several times to rearrange the format of the filename string, with no progress.
I was wondering if you can assist with getting a date and time to show up as:
2019_01_17_hh_mm_ss.dat
thank you,
oceanq
I tried several times to rearrange the format of the filename string, with no progress.
I was wondering if you can assist with getting a date and time to show up as:
2019_01_17_hh_mm_ss.dat
thank you,
oceanq
Re: datetime and strftime
This code: produces this result: on my pyboard.
Code: Select all
import pyb
rtc = pyb.RTC()
t = rtc.datetime()
print('t =', t)
name = '{:04d}_{:02d}_{:02d}_{:02d}_{:02d}_{:02d}.dat'.format(t[0], t[1], t[2], t[4], t[5], t[6])
print('filename =', name)
Code: Select all
>>> import ftime
t = (2019, 1, 18, 5, 9, 46, 13, 59)
filename = 2019_01_18_09_46_13.dat
Re: datetime and strftime
Thank you! I almost had it! The syntax is always hard to find.
Is there a link for these format rules?
Cheers,
Q
Is there a link for these format rules?
Cheers,
Q
Re: datetime and strftime
Methods #1 ('old-style string formatting') and #2 ('new-style string formatting') described in this guide can be used in MicroPython.
Methods #3 ('String Interpolation / f-Strings') and #4 ('Template Strings') can't.
Methods #3 ('String Interpolation / f-Strings') and #4 ('Template Strings') can't.