December 04, 2020, 11:56:19 AM

Author Topic: [bmx] Timestamp type by Pineapple [ 1+ years ago ]  (Read 481 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Timestamp type by Pineapple [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Timestamp type
Author : Pineapple
Posted : 1+ years ago

Description : The particularly useful timestamp.now() returns a timestamp object containing as integers the current year, month, day, hour, minute, and second.

Code :
Code: BlitzMax
  1. '   --+-----------------------------------------------------------------------------------------+--
  2. '     |   This code was originally written by Sophie Kirschner (sophiek@pineapplemachine.com)   |  
  3. '     | It is released as public domain. Please don't interpret that as liberty to claim credit |  
  4. '     |   that isn't yours, or to sell this code when it could otherwise be obtained for free   |  
  5. '     |                because that would be a really shitty thing of you to do.                |
  6. '   --+-----------------------------------------------------------------------------------------+--
  7.  
  8. SuperStrict
  9.  
  10. Import brl.stream
  11. Import brl.retro
  12.  
  13. Type timestamp
  14.     Field hour%,minute%,second%
  15.     Field day%,month%,year%
  16.     Function Create:timestamp(year%=2000,month%=1,day%=1,hour%=1,minute%=1,second%=1)
  17.         Local n:timestamp=New timestamp
  18.         n.year=year;n.month=month;n.day=day;n.hour=hour;n.minute=minute;n.second=second
  19.         Return n
  20.     End Function
  21.     Method tostring$()
  22.         Return getdate()+" - "+gettime()
  23.     End Method
  24.     Method todottedstring$()
  25.         Return year+"."+padbefore(month,"0",2)+"."+padbefore(day,"0",2)+"."+padbefore(hour,"0",2)+"."+padbefore(minute,"0",2)+"."+padbefore(second,"0",2)
  26.     End Method
  27.     Method getdate$()
  28.         Return padbefore(day,"0",2)+"/"+padbefore(month,"0",2)+"/"+Right(year,2)
  29.     End Method
  30.     Method gettime$()
  31.         Local suff$=" AM"
  32.         If hour>11 Then suff=" PM"
  33.         Return padbefore((hour Mod 12),"0",2)+":"+padbefore(minute,"0",2)+":"+padbefore(second,"0",2)+suff
  34.     End Method
  35.     Method write(f:TStream)
  36.         WriteInt f,hour
  37.         WriteInt f,minute
  38.         WriteInt f,second
  39.         WriteInt f,day
  40.         WriteInt f,month
  41.         WriteInt f,year
  42.     End Method
  43.     Function read:timestamp(f:TStream)
  44.         Local n:timestamp=New timestamp
  45.         n.hour=ReadInt(f)
  46.         n.minute=ReadInt(f)
  47.         n.second=ReadInt(f)
  48.         n.day=ReadInt(f)
  49.         n.month=ReadInt(f)
  50.         n.year=ReadInt(f)
  51.         Return n
  52.     End Function
  53.     Function now:timestamp()
  54.         Local n:timestamp=New timestamp
  55.         Local time@[256],buff:Byte[256]
  56.         time_(time)
  57.         Local localtime:Byte Ptr=localtime_( time )
  58.         strftime_(buff,256,"%d",localtime)
  59.         n.day=Int(String.FromCString(buff))
  60.         strftime_(buff,256,"%m",localtime)
  61.         n.month=Int(String.FromCString(buff))
  62.         strftime_(buff,256,"%Y",localtime)
  63.         n.year=Int(String.FromCString(buff))
  64.         strftime_(buff,256,"%H",localtime)
  65.         n.hour=Int(String.FromCString(buff))
  66.         strftime_(buff,256,"%M",localtime)
  67.         n.minute=Int(String.FromCString(buff))
  68.         strftime_(buff,256,"%S",localtime)
  69.         n.second=Int(String.FromCString(buff))
  70.         Return n
  71.     End Function
  72.     Method compare%(o1:Object)
  73.         Local o:timestamp=timestamp(o1)
  74.         If year >o.year Return 1 ElseIf year <o.year Return -1
  75.         If month >o.month Return 1 ElseIf month <o.month Return -1
  76.         If day >o.day Return 1 ElseIf day <o.day Return -1
  77.         If hour >o.hour Return 1 ElseIf hour <o.hour Return -1
  78.         If minute >o.minute Return 1 ElseIf minute <o.minute Return -1
  79.         If second >o.second Return 1 ElseIf second <o.second Return -1
  80.         Return 0
  81.     End Method
  82.     Function padbefore$(s$,char$,length%)
  83.         While Len(s)<length
  84.             s=char+s
  85.         Wend
  86.         Return s
  87.     End Function
  88. End Type


Comments :


virtlands(Posted 1+ years ago)

 Very nice work, ... now all I have to do is convert it to Blitz3D.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal