Convert seconds to Time()

About Monkey 2 Forums Monkey 2 Programming Help Convert seconds to Time()

This topic contains 16 replies, has 4 voices, and was last updated by  Angus 2 weeks, 1 day ago.

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #14950

    regulark
    Participant

    Hello,

    I feel like I am missing something pretty obvious here, but how can I convert the long return from GetFileTime into a new Time instance? Specifically I am looking to have the time formatted.

    I thought that maybe I can pass the seconds into new Time() but that does not work…

    </p><p>local seconds := GetFileTime("myfile.txt")<br />Print new Time(0, 0, 0, 0, 0, seconds) ' Wed 31 Dec 1969 17:59:59</p><p>

    Thank you

    #14952

    Angus
    Participant

    This is a problem I relegated to “think about later” last year when I made my file requester and I still have it on that pile.  Searching around I found this thread:

    https://www.avrfreaks.net/forum/converting-unix-time-date-stamp-human-readable-form

    In the fifth post david.prentice posts this code, which apparently works, but is it exactly what you’re looking for?  It seems to be what I’m after.  It doesn’t look hard to convert.. (?)

     

    struct tm *gmtime(timep) const time_t *timep;
    {
    static struct tm tmbuf;
    register struct tm *tp = &tmbuf;
    time_t time = *timep;
    register long day, mins, secs, year, leap;
    day = time/(24L*60*60);
    secs = time % (24L*60*60);
    tp->tm_sec = secs % 60;
    mins = secs / 60;
    tp->tm_hour = mins / 60;
    tp->tm_min = mins % 60;
    tp->tm_wday = (day + 4) % 7;
    year = (((day * 4) + 2)/1461);
    tp->tm_year = year + 70;
    leap = !(tp->tm_year & 3);
    day -= ((year * 1461) + 1) / 4;
    tp->tm_yday = day;
    day += (day > 58 + leap) ? ((leap) ? 1 : 2) : 0;
    tp->tm_mon = ((day * 12) + 6)/367;
    tp->tm_mday = day + 1 – ((tp->tm_mon * 367) + 5)/12;
    tp->tm_isdst = 0;
    return (tp);
    }

    The Mod keyword is reserved in M2, but I can’t actually find the command.  Apparently % is mod?  I know nothing about… C? (ignorance!)

    I have to go and do other work, but I’m going to try to convert it myself later today.

    #14953

    Phil
    Participant

    Mod just works as expected here:

    gives you 3 as output

    #14954

    Angus
    Participant

    Thanks! So I’m pretty unsure what I’m doing, and what it’s doing isn’t quite right, but I bet I’m making a simple error:

    It produces this output when provided with the time value for the bit of code that contains it (so should be bang on compile time)

    1 of month:6 of year:118 Hour:13 Minute:18 Second:0 Day of week:0

    Which is nearly right in a lot of ways.  It is the first of the seventh month of 2018 (only 1900 years out) and it’s 14 hours n 18 minutes etc.  The hours value being one hour out could be down to UK daylight savings?

    Edit… the above description is of how accurate it was.  Is that obvious lol?

    What’s the obvious error I’m making?  I guess this:

    leap=Not(oYear & ULong(3))

    produces something wrong because oYear is of the wrong class.  I’m not sure what the class of the variable in the example are.

    I realise you may want a different sort of output to this, but if someone knows what I’m doing wrong it looks like this could be modified to provide the correct date n time however you want it.

    #14955

    Angus
    Participant

    Well this probably makes more sense, given the output of the filetime function, but it’s still the same…

    But trying it with some other files, it’s really close to being right.  Looks like the month is one unit too early (annoying) and the year is out (very annoying), but the day of the month and the day of the week and the time all seem right… a file in January reports the correct time, suggesting it doesn’t compensate for daylight savings.

    #14956

    Angus
    Participant

    The more I play with this the more I think I’ll just fudge it myself to add one to the month at the last minute and 1900 to the years.

    I guess I could do some things to make daylight savings correct as well, but that’s more annoying.  Everything else seems ok, including the day. 0-sunday 1-monday etc…   I looked at some files for last year and they were correct, or at least predictably incorrect.

    I’d rather have an error I’m making pointed out tho, I trust his code more than my fudges and still suspect I made a mistake.

    I’ll post what I’m going to end up using if no-one can see an error.

    #14957

    Angus
    Participant

    I figured out my silly mistakes and this is the code as I’ll use it.  I made it this way as I imagine rattling through potentially thousands of files and having to make names for each…

    So if I say:

    Print UnixTime.ToString(1364485543)

    it outputs:

    Thursday, 28th March 2013 15:45:43

     

    I tested it a fair bit and it seems ok, but perhaps I’m still making a mistake.  Using the same logic you could make it convert to different things.

    #14958

    Angus
    Participant

    In fact looking at it again, having gone to the trouble to structure it that way, I may remove the local variables all together and make them permanent globals to work with.  Would I just be wasting my time?  I’m not sure…

    #14959

    Angus
    Participant

    I did that, and made a couple of other small changes, and I guess this is what I’ll use…

     

     

    If there aren’t any other functions to do this, I could add it in the code library.  Are there things about the way I did it that make it unsuitable?

    #14960

    Angus
    Participant

    Hmmm I’m so prone to tweaking.  Actually the code I’m going to use is more like this, as it makes vaguely more sense.

     
    I’m gonna stop playing with this and go back to other things 🙂

    #14961

    regulark
    Participant

    Haha wow Angus, thank you.

    Here’s my version of what you made 🙂


    Users who have liked this topic:

    • avatar
    #14962

    Angus
    Participant

    Sweet little change!

    #14963

    Angus
    Participant

    Hmm as I think about this, though, I’m sure it’ll have a problem with dates before 2000.  Like I say the doubts I had were correct in that they were with how I was interpreting what he was doing.  The month, for example is correct if January=0, as it does when being read from an array.  Worth bearing in mind when displaying the month as a number.

    So I suspect a further look at how I made his code into M2 code would be worth it, I’m not too sure about where I might be making an error.  Frustrating as it’s really close, and it’s fun to see words appear out of that opaque looking number.

    #14964

    Angus
    Participant

    Can’t check it just now, but will soon. I’ll need this to work myself so I’ll have to keep coming back til it does 🙂

    #14966

    Mark Sibly
    Keymaster

    OK, I’ve just added Time.FromFileTime:Time( fileTime:long ) which can simply be passed a GetFileTime() time, eg:

    Local time:=Time.FromFileTime( GetFileTime( filePath ) )

    Now up in develop branch.


    Users who have liked this topic:

    • avatar
Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic.