datetime and strftime

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Posts: 5
Joined: Sun Mar 12, 2017 1:27 am

datetime and strftime

Postby icenov » Sun Mar 12, 2017 1:35 am

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

User avatar
Posts: 1393
Joined: Fri Jul 18, 2014 8:01 am

Re: datetime and strftime

Postby pythoncoder » Mon Mar 13, 2017 9:06 am


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.
Peter Hinch

Posts: 5
Joined: Sun Mar 12, 2017 1:27 am

Re: datetime and strftime

Postby icenov » Mon Mar 27, 2017 10:27 am

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.

User avatar
Posts: 2264
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada

Postby dhylands » Mon Mar 27, 2017 7:17 pm

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

Return to “General Discussion and Questions”

Who is online

Users browsing this forum: No registered users and 3 guests