Advertisement
Serafim

Untitled

Feb 11th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace Time:
  2.   class Segment
  3.     constructor: (@seconds) ->
  4.     getSeconds: ->  if (t = ((@seconds)>>0)%(60))<10 then '0' + t else t
  5.     getMinutes: ->  if (t = ((@seconds/60)>>0)%(60))<10 then '0' + t else t
  6.     getHours: ->    if (t = ((@seconds/60/60)>>0)%(60))<10 then '0' + t else t
  7.     getDays: ->     if (t = ((@seconds/60/60/24)>>0)%(60))<10 then '0' + t else t
  8.     getMonths: (daysPerMonth = 30) ->   if (t = ((@seconds/60/60/24/daysPerMonth)>>0)%(60))<10 then '0' + t else t
  9.  
  10. namespace Time:
  11.   class Timestamp
  12.     @SYNC:    0
  13.  
  14.     @SECOND:  1
  15.     @MINUTE:  60
  16.     @HOUR:    3600
  17.     @DAY:     86400
  18.     @MONTH:   2592000   # (bad idea) 30 days per month
  19.     @YEAR:    946080000 # (double bad idea) 30 days per month
  20.  
  21.     constructor: (@timestamp) ->
  22.       if currentPlayer? && currentPlayer.synchonism?
  23.         @constructor.SYNC = (((new Date()).getTime()/1000)|0) - currentPlayer.synchonism()
  24.  
  25.       unless @timestamp.toString()
  26.         @timestamp = 0
  27.         return @
  28.  
  29.       if @timestamp.toString().length > 10
  30.         @timestamp = (@timestamp / 1000)
  31.       @timestamp = @timestamp >> 0
  32.  
  33.  
  34.     toString: -> @timestamp
  35.  
  36.     mod: (date) ->
  37.       @timestamp%date
  38.  
  39.     date: (string) ->
  40.       zeroFirst = (n) ->
  41.         return if n.toString().length is 1 then '0' + n else n.toString()
  42.  
  43.       if string
  44.         date = new Date @timestamp * 1000
  45.         return string
  46.         .replace('d', zeroFirst date.getDay())
  47.         .replace('m', zeroFirst date.getMonth())
  48.         .replace('y', date.getFullYear().toString().substr(2, 2))
  49.         .replace('Y', date.getFullYear())
  50.         .replace('h', if h = date.getHours() > 12 then zeroFirst(h - 12) else zeroFirst h)
  51.         .replace('H', zeroFirst date.getHours())
  52.         .replace('i', zeroFirst date.getMinutes())
  53.         .replace('s', zeroFirst date.getSeconds())
  54.       else
  55.         return new Date @timestamp * 1000
  56.  
  57.     getNow: ->
  58.       (((new Date()).getTime()/1000)|0) - @constructor.SYNC
  59.  
  60.     # Секунды
  61.     addSecond: (num = 1) -> @timestamp += num
  62.     subSecond: (num = 1) -> @timestamp -= num
  63.  
  64.     # Минуты
  65.     addMinute: (num = 1) -> @timestamp += (num * @constructor.MINUTE)
  66.     subMinute: (num = 1) -> @timestamp -= (num * @constructor.MINUTE)
  67.  
  68.     # Часы
  69.     addHour: (num = 1) -> @timestamp += (num * @constructor.HOUR)
  70.     subHour: (num = 1) -> @timestamp -= (num * @constructor.HOUR)
  71.  
  72.     # Дни
  73.     addDay: (num = 1) -> @timestamp += (num * @constructor.DAY)
  74.     subDay: (num = 1) -> @timestamp -= (num * @constructor.DAY)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement