flat assembler
Message board for the users of flat assembler.

Index > Windows > How to convert from number of milliseconds since Jan 1, 1970

Author
Thread Post new topic Reply to topic
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 03 Aug 2007, 08:34
How to convert from number of milliseconds since Jan 1, 1970 to
Hour:Minute:Second Month/Day/Year components without using
any library or API function?

Thanks in advance to anyone who replies!

_________________
Code:
 o__=-
 )
(\
 /\  
    


Last edited by 0.1 on 03 Aug 2007, 09:32; edited 1 time in total
Post 03 Aug 2007, 08:34
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 03 Aug 2007, 09:22
Quote:
PS: and vid if you think this thread does not belong here move it at your own will!

Yes, i think your question is Windows-specific (because you mention API functions). Let me know if I am wrong.
Post 03 Aug 2007, 09:22
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 03 Aug 2007, 09:34
I did not mean Win32API although but anyway it's ok here.
I want the reply and you decide the right place for the thread. Hope it's not too
much work for you!
Post 03 Aug 2007, 09:34
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 03 Aug 2007, 09:51
nope, just few seconds.

In case you like to use libc, see http://msdn2.microsoft.com/en-us/library/z54t9z5f(VS.80).aspx
Post 03 Aug 2007, 09:51
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 04 Aug 2007, 10:15
sec = msec / 1000
min = sec / 60
hr = min / 60
day = hr / 24
mon = day / 30 or 31 or 28 or 29 Shocked
year = mon / 12

_________________
Code:
 o__=-
 )
(\
 /\  
    


Last edited by 0.1 on 04 Aug 2007, 10:44; edited 1 time in total
Post 04 Aug 2007, 10:15
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 04 Aug 2007, 10:32
don't forget overlapping years
Post 04 Aug 2007, 10:32
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 04 Aug 2007, 10:42
I know above is wrong!
Just posted it so that some one adds the correction here!

_________________
Code:
 o__=-
 )
(\
 /\  
    
Post 04 Aug 2007, 10:42
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 04 Aug 2007, 12:20
0.1 wrote:
sec = msec / 1000
min = sec / 60
hr = min / 60
day = hr / 24
mon = day / 30 or 31 or 28 or 29 Shocked
year = mon / 12
I suppose, that you want to write:
Code:
sec / 1000 = msec 
min / 60 = sec
hr /60 = min 
day / 24 = hr 
mon / (30 or 31 or 28 or 29)  = day 
year / 12 = mon    
Question
Post 04 Aug 2007, 12:20
View user's profile Send private message Visit poster's website Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 04 Aug 2007, 12:37
No MHajduk!
I am confused about my 5th line?
Post 04 Aug 2007, 12:37
View user's profile Send private message Reply with quote
zann



Joined: 12 Oct 2006
Posts: 4
zann 04 Aug 2007, 19:15
const
MsecPerDay=86400E3
MsecPerYear=MSecPerDay*365
MsecPerLeapYear=MSecPerDay*366

:IsLeapYear (Year is in eax)
mov edx,eax
and edx,$FFFFFF3
cmp eax,edx
jne @@No ;not divisible by 4
mov ecx,eax
xor edx,edx
div dword(100)
test edx,edx ;not a century
jnz @@yes
mov edx,eax
and edx,$FFFFFF3 ;year/100/4 also a leap year
cmp edx,eax
je @@yes
@@No:xor eax,eax
ret
@@Yes:Mov eax,1
Post 04 Aug 2007, 19:15
View user's profile Send private message Reply with quote
handyman



Joined: 04 Jun 2007
Posts: 40
Location: USA - KS
handyman 04 Aug 2007, 20:03
Original requst also stated a starting point of Jan 1, 1970, so there will have to be an adjustment made somewhere in logic to handle this offset so that the math works out (year 2000 a leap year). Also month and day of month calculations can be handled by lookup table or series of month length subtractions since the days of month are not all the same. And do not forget the February length leap year calculation that usually ends up in the middle of all this.
Post 04 Aug 2007, 20:03
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 05 Aug 2007, 01:09
Here's the actual code that will do what you want.
It can be improved by using ...
-A larger LUT
-Dividing the INPUT by 365.25 * 1000*60*60*24 and floor to get the year then working from their for the other values
-Unrolling the loops
-Using the proper api and not worrying about this stuff

It's untested since I wrote it in the browser, sorry if it doesnt work, but it should at least show you the algorithm

Code:
.data
DAYMS = 1000 * 60 * 60 * 24
HOURMS = 1000 * 60 * 60
MINUTEMS = 1000 * 60
SECONDMS = 1000
MON28 = DAYMS * 28
MON29 = DAYMS * 29
MON30 = DAYMS * 30
MON31 = DAYMS * 31

YRS_LUT_SIZE = 4
YRS_LUT:
dq DAYMS*366, DAYMS*365, DAYMS*365, DAYMS*365

LEAP_LUT:
dq MON31, MON29, MON31, MON30, MON31, MON30, MON31, MON31, MON30, MON31, MON30, MON31
REG_LUT:
dq MON31, MON28, MON31, MON30, MON31, MON30, MON31, MON31, MON30, MON31, MON30, MON31

INPUT dq 0 ;; put the milli seconds since 1970 here
;;Returned values
YEAR dd 0
MONTH dd 0
DAY dd 0
HOUR dd 0
MINUTE dd 0
SECOND dd 0
MILLISSECOND dd 0

.code
GET_DATE_INFO: 
PUSH ebp
MOV ebp,esp
   MOV eax,[INPUT] ;;low 32bits of 64bit ms since 1970
   MOV edx,[INPUT+4] ;;high 32bits
   MOV ecx,1970 ;;start year
   XOR ebx,ebx ;;YRS_LUT index
.GETYR:
   MOV esi, [YRS_LUT + ebx * 8] ;; low 32bit
   MOV edi, [YRS_LUT + ebx * 8 + 4] ;; high 32 bit
   CMP edx,edi  ;; add another year or not
   JA .SKIPYR
   JB .ENDYR
   CMP eax,esi
   JB .ENDYR
.SKIPYR:
   SUB eax,esi
   SBB edx,edi
   INC ecx
   INC ebx
   AND ebx,3 ;;replaces CMP ebx, YRS_LUT_SIZE
   JMP .GETYR
.ENDYR:
   MOV [YEAR], ecx
   MOV ecx,1 ;; start month
   TEST ebx,ebx ;; iF leap year
   CMOVNE ebx,REG_LUT
   CMOVE ebx,LEAP_LUT
.GETMO:
   MOV esi, [ebx] ;; low 32bit
   MOV edi, [ebx + 4] ;; high 32 bit
   CMP edx,edi  ;; add another month or not
   JA .SKIPMO
   JB .ENDMO
   CMP eax,esi
   JB .ENDMO
.SKIPMO:
   SUB eax,esi
   SBB edx,edi
   INC ecx
   ADD ebx,8
   JMP .GETMO
.ENDMO:
   MOV [MONTH], ecx
.GETDAY:
   MOV ecx,DAYMS
   DIV ecx
   INC eax ;; correct for One instead of Zero indexing of values
   MOV [DAY], eax
.GETHR:
   MOV eax,edx
   MOV ecx,HOURMS
   XOR edx,edx
   DIV ecx
   INC eax
;;
;;PUT AM/PM CODE HERE
;;
   MOV [HOUR],eax
.GETMIN:
   MOV eax,edx
   MOV ecx,MINUTEMS
   XOR edx,edx
   DIV ecx
   MOV [MINUTE],eax
.GETSEC:
   MOV eax,edx
   MOV ecx,SECONDMS
   XOR edx,edx
   DIV ecx
   MOV [SECOND],eax
.GETMS:
   MOV [MILLISECOND],edx

MOV esp,ebp
POP ebp
RET 0
    
Post 05 Aug 2007, 01:09
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 05 Aug 2007, 03:47
Thanks a lot zann, r22 and handyman!
Post 05 Aug 2007, 03:47
View user's profile Send private message Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav 10 Aug 2007, 15:36
this thing (How to convert from number of milliseconds since Jan 1, 1970) called unix_time

http://en.wikipedia.org/wiki/Unix_time
Post 10 Aug 2007, 15:36
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.