Page 1 of 1

datetime and strftime

Posted: Sun Mar 12, 2017 1:35 am
by icenov
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

Re: datetime and strftime

Posted: Mon Mar 13, 2017 9:06 am
by pythoncoder
Try:

Code: Select all

import pyb
rtc = pyb.RTC()
t = rtc.datetime()
name = '{:02d}_{:02d}_{:02d}_logfile.dat'.format(t[2], t[4], t[5])
Arguably the name would be more unique (if there are degrees of uniqueness ;) ) if you included year and month information.

Re: datetime and strftime

Posted: Mon Mar 27, 2017 10:27 am
by icenov
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.

Posted: Mon Mar 27, 2017 7:17 pm
by dhylands
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:

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;
}
and on 3 of my computers (all running Ubuntu 16.04) I got this output:

Code: Select all

tv.tv_sec  = 1490636674
tv.tv_usec = 498363
tz.tz_minuteswest = 0
tz.tz_dsttime     = 0
and ls -l /media/dhylands/PYBFLASH showed the micropython filesystem timestamps as 7 hours in the future.
If I then did

Code: Select all

sudo hwclock --systz
and reran my gettimeofday C program then I got:

Code: Select all

tv.tv_sec  = 1490636725
tv.tv_usec = 276087
tz.tz_minuteswest = 420
tz.tz_dsttime     = 0
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.

Re: datetime and strftime

Posted: Fri Jan 18, 2019 3:33 am
by oceanq
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

Re: datetime and strftime

Posted: Fri Jan 18, 2019 5:48 pm
by dhylands
This code:

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)
produces this result:

Code: Select all

>>> import ftime
t = (2019, 1, 18, 5, 9, 46, 13, 59)
filename = 2019_01_18_09_46_13.dat
on my pyboard.

Re: datetime and strftime

Posted: Sat Jan 19, 2019 4:26 am
by oceanq
Thank you! I almost had it! The syntax is always hard to find.
Is there a link for these format rules?
Cheers,
Q

Re: datetime and strftime

Posted: Mon Jan 21, 2019 12:22 pm
by nekomatic
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.