Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:05 11 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : HumanTime Function

Author Message
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 01:43am 02 Aug 2017
Copy link to clipboard 
Print this post

return a Human readable date and time string from a unixtime number

here

This is the counterpart to the UnixTime function posted earlier
 
SteveA
Regular Member

Joined: 10/03/2017
Location: United Kingdom
Posts: 48
Posted: 04:36am 03 Aug 2017
Copy link to clipboard 
Print this post

Thank you for taking the time to write and share this and the UnixTime function, it is very much appreciated.

There is a minor error in the documentation which states:

' take seconds since 1970-01-01 (unixtime) and converts to a human form date/time as
' "dd-mm-yyyy hh:mm:ss" so as returned by DATE$ and TIME$


The function actually returns the date year first i.e "yyyy-mm-dd hh;mm;ss"

For example:

print HumanTime$(1501770732) returns 2017-08-03 14:32:12

 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 05:00am 03 Aug 2017
Copy link to clipboard 
Print this post

The format "yyyy-MM-dd hh:mm:ss" would be preferred.
Hope the error is in the documentation, not in the program.
That format allows sorting and is very convenient to work with.

Microblocks. Build with logic.
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 05:51am 03 Aug 2017
Copy link to clipboard 
Print this post

Well spotted and well argued - I like human dates as YYYY-MM-DD as well but I wanted to maintain compatibility with the DATE$ function... however

updated... everyone is catered for with an option switch


> ? humantime$(1e6)
12-01-1970 13:46:40
> ? humantime$(1e6,1)
1970-01-12 13:46:40
>

Did the same thing with Unixtime too so you can choose how the two outer parts of the date are handled.

welcomeEdited by CaptainBoing 2017-08-04
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:26am 03 Aug 2017
Copy link to clipboard 
Print this post

Poor Yanks, they would not know if it was 12 january or 1 december.
Maybe add a "2" to make them happy. They are also humans after all.


PS: I like the Wiki website, looks very clean! I see that it is made with asp.net. Is this available for download and deploy on one of my own servers?
I am searching for something like this to keep my own documents organized.
Edited by MicroBlocks 2017-08-04
Microblocks. Build with logic.
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 07:00am 03 Aug 2017
Copy link to clipboard 
Print this post

does MMBasic support different date locales?

this is one advantage of Unixtime and yyyy-mm-dd - known formats

wiki install is available here: http://stw.codeplex.com/releases/view/108727

it can work with a DB or stand-alone files. I use it for organizing my own work docs too.Edited by CaptainBoing 2017-08-04
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 09:43am 03 Aug 2017
Copy link to clipboard 
Print this post

  MicroBlocks said   Poor Yanks, they would not know if it was 12 january or 1 december.
Maybe add a "2" to make them happy. They are also humans after all.

So what's new Jean? Keep in mind that Andrew drives on the wrong side of the road!

Some of us over here are Polacks! Is that really considered human?

Pavel Artur Jan Waclaw Lepkowski in NY

 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 10:58am 03 Aug 2017
Copy link to clipboard 
Print this post

Any chance of it also giving the day of the week~ Mon Tue wed etc?
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3466
Posted: 02:54pm 03 Aug 2017
Copy link to clipboard 
Print this post

  MicroBlocks said   Poor Yanks, they would not know if it was 12 january or 1 december.


I don't know that it's Yanks who would be confused, but Canadians often use DD-MM format, though I don't know that they are inclined to say 3rd August instead of August 3rd. If needing to write a date in Canada, if numbers are not required, I always use the 3-character month abbreviation followed by the day.

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 08:43pm 03 Aug 2017
Copy link to clipboard 
Print this post

  lew247 said   Any chance of it also giving the day of the week~ Mon Tue wed etc?


That is getting away from the core purpose, but there are loads of DoW calculators in the library (I like Quazee137's quick one near the bottom)

http://www.fruitoftheshed.com/Search.aspx?AllNamespaces=1&FilesAndAttachments=1&Query=week

you could easily modify it so you could get your humantime and then pass the LEFT$(x$,10) characters to DoW to get your day

EDIT... It's a useful addition.

hth

hEdited by CaptainBoing 2017-08-05
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 12:53pm 23 Mar 2018
Copy link to clipboard 
Print this post

I cannot get this to work
I've got Unixtime coming from an ESP8266 as a string
the relevant section of code is this
and I have the HumanTime function in the code as well



[code]sunrise = VAL(FieldArray$(22))
print HumanTime$(ut_sunrise)[/code]

[243] Print HumanTime$(ut_sunrise)
Error: Inconsistent type suffix

I've tried it various ways and I just can't get it to work for some reason
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 01:12pm 23 Mar 2018
Copy link to clipboard 
Print this post

"sunrise" or "UT_sunrise"?


while we are at it, ensure your unixtimes are 13 digits long. Some Unixtimes get reported in Milliseconds which means you get 16 digits. If so, just strip away the rightmost 3 digits either by dividing by 1000 or using LEFT$ on the stringEdited by CaptainBoing 2018-03-24
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 01:25pm 23 Mar 2018
Copy link to clipboard 
Print this post

I didn't realise it didn't have to have the UT because it was in the instructions/exampmle

Still not working but it's because the unixtime has only 10 digits
1521829684
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 01:35pm 23 Mar 2018
Copy link to clipboard 
Print this post

Sorry I do apologise
It is working BUT

print HumanTime(sunrise) " Sunrise" 'Sunrise

gives this
04-04-2018 06:02:40 Sunrise

it should be
03-03-2018 6:02:34 AM


Unixtime being converted is 1521784953Edited by lew247 2018-03-24
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 01:50pm 23 Mar 2018
Copy link to clipboard 
Print this post

sorry yes, 10 digits (doh!)

HumanTime is always 24 hour - if you want AM/PM you'll have to tweak it yourself

using the unixtime number you supplied, I get:

> ? humantime(1521784953)
23-03-2018 06:02:33
>


which is correct. Have you tweaked the function - e.g. in your string splitting?

The one I used above is:


Function HumanTime(UT As Integer,opt As Integer) As String
' take seconds since 1970-01-01 (unixtime) and converts to a human form date/
time as
' if opt=0 (or not specified) the format is dd-mm-yyyy hh:mm:ss" to match the
format of DATE$
' if opt<>0 the date part is returned as yyyy-mm-dd to aid sorting and avoid
Local integer d,m,s,y,z
If UT<0 Then HumanTime$="":Exit Function
y=1970:d=UT\86400:s=UT Mod 86400
Do
z=365+IsLeapYear(y)
If d>=z Then
d=d-z:y=y+1
Else
Exit Do
EndIf
Loop
'here, Y is the current year and d the remaining days
m=1' start at january
' days between the start of the year and the current month
Do
z=0
Select Case m
Case 1,3,5,7,8,10
If d>=31 Then d=d-31:z=1
Case 2
If IsLeapYear(y)Then
If d>=29 Then d=d-29:z=1
Else
If d>=28 Then d=d-28:z=1
EndIf
Case 4,6,9,11
If d>=30 Then d=d-30:z=1
End Select
If z=1 Then
m=m+1
Else
d=d+1:Exit Do 'first day is 1 not 0
EndIf
Loop
If opt<>0 Then
HumanTime$=Str$(y)+"-"+ZPad$(m,2)+"-"+ZPad$(d,2)
Else
HumanTime$=ZPad$(d,2)+"-"+ZPad$(m,2)+"-"+Str$(y)
EndIf
y=s\3600:s=s-y*3600
m=s\60:s=s-m*60
HumanTime$=HumanTime$+" "+ZPad$(y,2)+":"+ZPad$(m,2)+":"+ZPad$(s,2)
End Function


Edited by CaptainBoing 2018-03-25
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 05:56pm 23 Mar 2018
Copy link to clipboard 
Print this post

It's the exact same as you have above other I had to declare DIM ut,ISLEAPYEAR
Otherwise I kept getting this error

[quote][564] z=365+IsLeapYear(y)
Error: Function or array ISLEAPYEAR not declared[/quote]


The issue may be the string I'm receiving


sunrise = val(FieldArray$(22))
print sunrise
print HumanTime(sunrise)
[quote]>
1.52178e+09
04-04-2018 06:02:40
[/quote]

the esp is sending 1521784928
and if I do
print FieldArray$(22)

I get [quote]run
1521784928
1.52178e+09
04-04-2018 06:02:40[/quote]


Edited by lew247 2018-03-25
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 07:56pm 23 Mar 2018
Copy link to clipboard 
Print this post

You must have something different, you shouldn't dim IsLeapYear - it's a function. If this is missing then I reckon you've only got parts of HumanTime, not the whole story - check out the library page again and note the dependencies section.

If IsLeapYear always returns the same value (as a variable instead of a function) then that will *definitely* throw the calculations all to crap... which might go some way to explaining the errors you are getting.

http://www.fruitoftheshed.com/MMBasic.HumanTime-Function-to-return-a-human-readable-date-and-time-from-a-unixtime-number .ashx

The scientific notation shows you have the variable sunrise as a FLOAT - it needs to be an INTEGER. as you can see, much of the value is being truncated because FLOATs don't have the resolution of single values all the way down from billions to zero.
Edited by CaptainBoing 2018-03-25
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 09:37pm 23 Mar 2018
Copy link to clipboard 
Print this post

I found the problem

Dependencies....

I read it as Discrepancies and didn't realise I had to have both of those, I only had the Zpad$

Works great now thanks

btw I'm assuming the time returned is UTC/GMT?
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 10:12pm 23 Mar 2018
Copy link to clipboard 
Print this post

hmm... UTC/GMT... Unixtime is calculated based on the system time of the machine - so it is local time. However... a lot of servers use UTC as their system time (even if they are on different sides of the planet), others subtract their time zone from local time (to give UTC) and still others don't bother doing anything just using whatever the clock says at the moment of the function call.

Micromites fall into this last category. MMBasic makes it easy to forget micromites are single chip micro-controllers - with the associated limitations of them. So Unix/HumanTime only work with the local time on your micromite because they derive values from DATE$ and TIME$. If you have your local time set to EST (GMT-5) then that is what the two functions will work with. You could write code easily enough for the micromite to handle time zones etc - look in the library for DateAdd and DateDiff for really easy ways to play with datetimes. Something like the following would return UTC from EST (ignoring daylight saving for ease):


MyTimeZone=-5 ' Montreal is five hours behind UTC
utc$=DateAdd(-MyTimeZone,"hh",Now()) ' return the date time with 5 hours added


In my company's data centers all servers and network fabric run UTC - even the ones in Frankfurt and Singapore and they all ignore daylight saving. So at the moment, all the machines are within a second or two of 22:10. On Monday, right now would be 23:10, but all the clocks in our DCs will stay at 22:10. It makes more sense to do this for us but everyone's mileage varies. Systems that use Daylight saving give ambiguous logs because of the jumps in time. We don't like that and so we synchronize to UTC and no DST... it can make you think for a moment when you are crawling through logs in the summer

So long as it makes sense to you, it can be whatever you like.

Glad you found your problem.
Edited by CaptainBoing 2018-03-25
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025