# Convert epoch to date/time within JSON.mapping

**URL:** <https://forum.crystal-lang.org/t/convert-epoch-to-date-time-within-json-mapping/974>\
**Category:** Help & Support\
**Created:** [July 22, 2019, 9:49pm UTC](https://forum.crystal-lang.org/t/convert-epoch-to-date-time-within-json-mapping/974 "2019-07-22T21:49:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![miessos](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/miessos/32/519_2.png) [@miessos](https://forum.crystal-lang.org/u/miessos)\
**Post date:** [July 22, 2019, 9:49pm UTC](https://forum.crystal-lang.org/t/convert-epoch-to-date-time-within-json-mapping/974/1 "2019-07-22T21:49:08Z")

</div>

```auto
class Test

  ::DB.mapping({

    first_time: {type: Int32?},

    })

  ::JSON.mapping({

    first_time: {type: Int32?},

    })
end

get "/" do |env|

  tested = ::DB.db.query_all("SELECT first_time FROM test_table", as: Test)

  tested.to_json
  #tested.map(&.inspect).join("\n")

end

```

First\_time from test\_table is an integer which contains an epoch. Is there anyway I can convert the value into a date/time format within the JSON.mapping? If not which other options do I have to convert before the to\_json action?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [July 23, 2019, 12:01am UTC](https://forum.crystal-lang.org/t/convert-epoch-to-date-time-within-json-mapping/974/2 "2019-07-23T00:01:56Z")

</div>

[https://crystal-lang.org/api/0.29.0/Time/EpochConverter.html](https://crystal-lang.org/api/0.29.0/Time/EpochConverter.html)

---

<div class="post-metadata">

**Author:** ![miessos](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/miessos/32/519_2.png) [@miessos](https://forum.crystal-lang.org/u/miessos)\
**Post date:** [July 23, 2019, 8:01pm UTC](https://forum.crystal-lang.org/t/convert-epoch-to-date-time-within-json-mapping/974/3 "2019-07-23T20:01:58Z")

</div>

Tried this

```auto
require "json"
require "kemal"
require "./db"

class Device

  ::DB.mapping({

    first_time: {type: Time, converter: Time::EpochConverter},

    })

  ::JSON.mapping({

   first_time: {type: Time, converter: Time::EpochConverter},

    })
end

get "/" do |env|

  devices = Devices::DB.db.query_all("SELECT first_time FROM devices", as: Device)

  devices.to_json
  #devices.map(&.inspect).join("\n")

end

Kemal.run

```

Got this in return on compiling

```auto
Error in src/devices.cr:22: instantiating 'DB::Database#query_all(String)'

  devices = Devices::DB.db.query_all("SELECT first_time FROM devices", as: Device)
                           ^ ~~~~~~~~

in lib/db/src/db/query_methods.cr:241: instantiating 'DB::ResultSet+#read(Device.class)'

        rs.read(type)
           ^~~~

in lib/db/src/db/result_set.cr:74: instantiating 'Device.class#new(DB::ResultSet+)'

      type.new(self)
           ^~~

in src/devices.cr:7: expanding macro

  ::DB.mapping({
  ^

in macro 'mapping' /root/crystal/hello-world/lib/db/src/db/mapping.cr:60, line 41:

   1. include ::DB::Mappable
   2. 
   3.     
   4.       
   5.     
   6. 
   7.     
   8.       
   9. 
  10.       
  11.     
  12. 
  13.     
  14. @first_time : Time 
  15. 
  16. def first_time=(_first_time : Time )
  17. @first_time = _first_time
  18. end
  19. 
  20. def first_time
  21. @first_time
  22. end
  23.     
  24. 
  25. def self.from_rs(__temp_52 : ::DB::ResultSet)
  26. __temp_53 = Array(self).new
  27. __temp_52.each do
  28. __temp_53 << self.new(__temp_52)
  29. end
  30. __temp_53
  31. ensure
  32. __temp_52.close
  33. end
  34. 
  35. def initialize(__temp_52 : ::DB::ResultSet)
  36.       
  37. __temp_54 = nil
  38. __temp_55 = false
  39.       
  40. 
> 41. __temp_52.each_column do |col_name|
  42. case col_name
  43.           
  44. when "first_time"
  45. __temp_55 = true
  46. __temp_54 =
  47.                 
  48. Time::EpochConverter.from_rs(__temp_52)
  49.                 
  50.           
  51. else
  52.             
  53. raise ::DB::MappingException.new("unknown result set attribute: #{col_name}")
  54.             
  55. end
  56. end
  57. 
  58.       
  59.         
  60. if __temp_54.is_a?(Nil) && !__temp_55
  61. raise ::DB::MappingException.new("missing result set attribute: first_time")
  62. end
  63.         
  64.       
  65. 
  66.       
  67.         
  68. @first_time = __temp_54.as(Time)
  69.         
  70.       
  71. end
  72.   

instantiating 'DB::ResultSet+#each_column()'
in src/devices.cr:7: expanding macro

  ::DB.mapping({
  ^

in macro 'mapping' /root/crystal/hello-world/lib/db/src/db/mapping.cr:60, line 41:

   1. include ::DB::Mappable
   2. 
   3.     
   4.       
   5.     
   6. 
   7.     
   8.       
   9. 
  10.       
  11.     
  12. 
  13.     
  14. @first_time : Time 
  15. 
  16. def first_time=(_first_time : Time )
  17. @first_time = _first_time
  18. end
  19. 
  20. def first_time
  21. @first_time
  22. end
  23.     
  24. 
  25. def self.from_rs(__temp_52 : ::DB::ResultSet)
  26. __temp_53 = Array(self).new
  27. __temp_52.each do
  28. __temp_53 << self.new(__temp_52)
  29. end
  30. __temp_53
  31. ensure
  32. __temp_52.close
  33. end
  34. 
  35. def initialize(__temp_52 : ::DB::ResultSet)
  36.       
  37. __temp_54 = nil
  38. __temp_55 = false
  39.       
  40. 
> 41. __temp_52.each_column do |col_name|
  42. case col_name
  43.           
  44. when "first_time"
  45. __temp_55 = true
  46. __temp_54 =
  47.                 
  48. Time::EpochConverter.from_rs(__temp_52)
  49.                 
  50.           
  51. else
  52.             
  53. raise ::DB::MappingException.new("unknown result set attribute: #{col_name}")
  54.             
  55. end
  56. end
  57. 
  58.       
  59.         
  60. if __temp_54.is_a?(Nil) && !__temp_55
  61. raise ::DB::MappingException.new("missing result set attribute: first_time")
  62. end
  63.         
  64.       
  65. 
  66.       
  67.         
  68. @first_time = __temp_54.as(Time)
  69.         
  70.       
  71. end
  72.   

instantiating 'DB::ResultSet+#each_column()'
in src/devices.cr:7: expanding macro

  ::DB.mapping({
  ^

in macro 'mapping' /root/crystal/hello-world/lib/db/src/db/mapping.cr:60, line 48:

   1. include ::DB::Mappable
   2. 
   3.     
   4.       
   5.     
   6. 
   7.     
   8.       
   9. 
  10.       
  11.     
  12. 
  13.     
  14. @first_time : Time 
  15. 
  16. def first_time=(_first_time : Time )
  17. @first_time = _first_time
  18. end
  19. 
  20. def first_time
  21. @first_time
  22. end
  23.     
  24. 
  25. def self.from_rs(__temp_52 : ::DB::ResultSet)
  26. __temp_53 = Array(self).new
  27. __temp_52.each do
  28. __temp_53 << self.new(__temp_52)
  29. end
  30. __temp_53
  31. ensure
  32. __temp_52.close
  33. end
  34. 
  35. def initialize(__temp_52 : ::DB::ResultSet)
  36.       
  37. __temp_54 = nil
  38. __temp_55 = false
  39.       
  40. 
  41. __temp_52.each_column do |col_name|
  42. case col_name
  43.           
  44. when "first_time"
  45. __temp_55 = true
  46. __temp_54 =
  47.                 
> 48. Time::EpochConverter.from_rs(__temp_52)
  49.                 
  50.           
  51. else
  52.             
  53. raise ::DB::MappingException.new("unknown result set attribute: #{col_name}")
  54.             
  55. end
  56. end
  57. 
  58.       
  59.         
  60. if __temp_54.is_a?(Nil) && !__temp_55
  61. raise ::DB::MappingException.new("missing result set attribute: first_time")
  62. end
  63.         
  64.       
  65. 
  66.       
  67.         
  68. @first_time = __temp_54.as(Time)
  69.         
  70.       
  71. end
  72.   

undefined method 'from_rs' for Time::EpochConverter:Module

```

Where do I go wrong?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [July 23, 2019, 8:11pm UTC](https://forum.crystal-lang.org/t/convert-epoch-to-date-time-within-json-mapping/974/4 "2019-07-23T20:11:10Z")

</div>

It’s only for `JSON/YAML.mapping`.

I’d maybe look into using an ORM that would handle this stuff for you.

EDIT: It looks like `DB.mapping` also supports converters. Should be able to add a `from_rs` method to the `Time::EpochConverter` module so it would also work for `DB.mapping`. Probably something like

```auto
module Time::EpochConverter
  def self.from_rs(result_set : DB::ResultSet) : Time
    Time.unix result_set.read(Int32)
  end
end

```

Something like that maybe? @miessos

---

<div class="post-metadata">

**Author:** ![miessos](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/miessos/32/519_2.png) [@miessos](https://forum.crystal-lang.org/u/miessos)\
**Post date:** [July 23, 2019, 8:13pm UTC](https://forum.crystal-lang.org/t/convert-epoch-to-date-time-within-json-mapping/974/5 "2019-07-23T20:13:57Z")

</div>

Will do, thank you for your assistance.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [July 26, 2019, 12:53pm UTC](https://forum.crystal-lang.org/t/convert-epoch-to-date-time-within-json-mapping/974/6 "2019-07-26T12:53:39Z")

</div>

Which database driver are you using? Maybe such a converter should be added to the driver. Or maybe it’s already there?
