Class: BlackStack::Timezone

Inherits:
Object
  • Object
show all
Defined in:
lib/timezone.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descToFloat(s) ⇒ Object

Recibe un string con formato “+HH:MM”. Retorna la cantidad de horas como un numero real, con decimales.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/timezone.rb', line 7

def self.descToFloat(s)
  sign = s[0]
  if (sign=="+")
    n = 1.0
  else
    n = -1.0
  end
  hours = s[1..2]
  minutes = s[4..5]
  int_part = hours.to_f
  dec_part = minutes.to_f / 60.00
  ret = n*(int_part + dec_part)    
  return ret.to_f 
end

.floatToDesc(x) ⇒ Object

Recibe la cantidad de horas como un numero real, con decimales. Retorna un string con formato “+HH:MM”.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/timezone.rb', line 24

def self.floatToDesc(x)
  int_part = x.to_i
  dec_part = (x-int_part.to_f).round(2).abs
  hours = int_part
  minutes = (dec_part * 60.00).to_i    
  if (hours<0)
    desc = "%03d" % hours 
  else
    desc = "+" + "%02d" % hours
  end
  desc += ":" + "%02d" % minutes
  return desc
end

.getDatabaseOffsetObject

Example: ‘-03:00’ Example: ‘ 08:00’



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/timezone.rb', line 40

def self.getDatabaseOffset()
  dbtime = DB["select SYSDATETIMEOFFSET() AS o"].first[:o].to_s
  #puts ""
  #puts ""
  #puts "dbtime:#{dbtime.to_s}:."
  #puts ""
  #puts ""
  ret = "#{dbtime[-5..-3]}:#{dbtime[-2..-1]}" 
  ret[0] = '+' if ret[0]!='-'
  #puts ""
  #puts ""
  #puts "dbtime:#{ret.to_s}:."
  #puts ""
  #puts ""
  return ret
end

Instance Method Details

#convertFromDatabaseDateTimeToThisTimeZone(s_datetime_in_database) ⇒ Object

Convierte una fecha-hora almacenada en el servidor de bases de datos, a la hora en esta zona horaria. Recibe un string con formato “YYYY-MM-DD HH:MM:SS”. Retorna un string con formato “YYYY-MM-DD HH:MM:SS”.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/timezone.rb', line 90

def convertFromDatabaseDateTimeToThisTimeZone(s_datetime_in_database)
  s = BlackStack::Timezone.getDatabaseOffset() # Example: '-03:00'
  #puts ""
  #puts ""
  #puts "s:#{s.to_s}:."
  #puts ""
  #puts ""
  x = BlackStack::Timezone.descToFloat(s) # database offset. Number of hours, with decimals
  #puts ""
  #puts ""
  #puts "x:#{x.to_s}:."
  #puts ""
  #puts ""
  y = self.offset # client offset
  #puts ""
  #puts ""
  #puts "y:#{y.to_s}:."
  #puts ""
  #puts ""
  z = y - x 
  #puts ""
  #puts ""
  #puts "z:#{z.to_s}:."
  #puts ""
  #puts ""
  ret = DB["SELECT DATEADD(HH, #{z.to_s}, '#{s_datetime_in_database}') AS o"].first[:o].to_s[0..18]
  #puts ""
  #puts ""
  #puts "convertFromDatabaseDateTimeToThisTimeZone:#{ret}:."
  #puts ""
  #puts ""
  
  return ret
end

#convertFromDatabaseTimeToThisTimeZone(s_time_in_database) ⇒ Object

Convierte una hora almacenada en el servidor de bases de datos, a la hora en esta zona horaria. Recibe un string con formato “+HH:MM”. Retorna un string con formato “+HH:MM”.



78
79
80
81
82
83
84
85
# File 'lib/timezone.rb', line 78

def convertFromDatabaseTimeToThisTimeZone(s_time_in_database)
  f_time_in_database = BlackStack::Timezone.descToFloat(s_time_in_database)
  s = BlackStack::Timezone.getDatabaseOffset()
  x = BlackStack::Timezone.descToFloat(s) # database offset
  y = self.offset # client offset
  z = y - x 
  return BlackStack::Timezone.floatToDesc((f_time_in_database.to_f - z.to_f).modulo(24))
end

#convertFromThisTimeZoneToDatabaseTime(s_time_in_client_timezone) ⇒ Object

Convierte una hora almacenada en el servidor de bases de datos, a la hora en esta zona horaria. Recibe un string con formato “+HH:MM”. Retorna un string con formato “+HH:MM”.



66
67
68
69
70
71
72
73
# File 'lib/timezone.rb', line 66

def convertFromThisTimeZoneToDatabaseTime(s_time_in_client_timezone)
  f_time_in_client_timezone = BlackStack::Timezone.descToFloat(s_time_in_client_timezone)
  s = BlackStack::Timezone.getDatabaseOffset()
  x = BlackStack::Timezone.descToFloat(s) # database offset
  y = self.offset # client offset
  z = y - x 
  return BlackStack::Timezone.floatToDesc((f_time_in_client_timezone.to_f + z.to_f).modulo(24))
end

#get_nowObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/timezone.rb', line 126

def get_now()
  datetime1 = DB["SELECT GETDATE() AS o"].first[:o].to_s[0..18]
  datetime2 = self.convertFromDatabaseDateTimeToThisTimeZone(datetime1)
  #puts ""
  #puts ""
  #puts "timezone:#{self.large_description}:."
  #puts "datetime1:#{datetime1}:."
  #puts "datetime2:#{datetime2}:."
  #puts ""
  #puts ""
  yy = datetime2[0..3] # yyyy-mm-dd hh:mi:ss
  mm = datetime2[5..6]
  dd = datetime2[8..9]
  hh = datetime2[11..12]
  mi = datetime2[14..15]
  ss = datetime2[17..18]
  #puts ""
  #puts ""
  #puts "get_now:#{yy}-#{mm}-#{dd} #{hh}:#{mi}:#{ss}:."
  #puts ""
  #puts ""
  return Time.new(yy,mm,dd,hh,mi,ss)
end

#offsetDescriptionObject

Convierte el atributo offset en un string con formato de hora. Ejemplos: 5.0 => 05:00, 5.5 => 05:30, -5.75 => -05:45



59
60
61
# File 'lib/timezone.rb', line 59

def offsetDescription()
  return BlackStack::Timezone.floatToDesc(self.offset.to_f)
end